Class: Krakow::Producer::Http

Inherits:
Object
  • Object
show all
Extended by:
Utils::Lazy::ClassMethods
Includes:
Utils::Lazy, Utils::Lazy::InstanceMethods
Defined in:
lib/krakow/producer/http.rb

Overview

HTTP based producer

Defined Under Namespace

Classes: Response

Instance Attribute Summary (collapse)

Attributes (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Http) initialize(args = {})

Returns a new instance of Http



45
46
47
48
49
# File 'lib/krakow/producer/http.rb', line 45

def initialize(args={})
  super
  build_ssl_context if ssl_context
  @uri = URI.parse(endpoint)
end

Instance Attribute Details

- (Hash) arguments (readonly) Originally defined in module Utils::Lazy::InstanceMethods

Returns argument hash

Returns:

  • (Hash)

    argument hash

- (Object) uri (readonly)

Returns the value of attribute uri



29
30
31
# File 'lib/krakow/producer/http.rb', line 29

def uri
  @uri
end

Class Method Details

+ (nil) attribute(name, type, options = {}) Originally defined in module Utils::Lazy::ClassMethods

Add new attributes to class

Parameters:

  • name (String)
  • type (Class, Array<Class>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :required (true, false)

    must be provided on initialization

  • :default (Object, Proc)

    default value

Returns:

  • (nil)

+ (Array<Hash>) attributes(*args) Originally defined in module Utils::Lazy::ClassMethods

Return attributes

Parameters:

  • args (Symbol)

    :required or :optional

Returns:

  • (Array<Hash>)

+ (TrueClass) set_attributes(attrs) Originally defined in module Utils::Lazy::ClassMethods

TODO:

need deep dup here

Directly set attribute hash

Parameters:

  • attrs (Hash)

Returns:

  • (TrueClass)

Instance Method Details

- (OpenSSL::SSL::SSLContext) build_ssl_context

Create a new SSL context

Returns:

  • (OpenSSL::SSL::SSLContext)


54
55
56
57
58
59
60
# File 'lib/krakow/producer/http.rb', line 54

def build_ssl_context
  require 'openssl'
  context = OpenSSL::SSL::SSLContext.new
  context.cert = OpenSSL::X509::Certificate.new(File.open(ssl_context[:certificate]))
  context.key = OpenSSL::PKey::RSA.new(File.open(ssl_context[:key]))
  config[:ssl_context] = context
end

- (Hash) config

Returns the config attribute

Returns:

  • (Hash)

    the config attribute



40
# File 'lib/krakow/producer/http.rb', line 40

attribute :config, Hash, :default => ->{ Hash.new }

- (TrueClass, FalseClass) config?

Returns truthiness of the config attribute

Returns:

  • (TrueClass, FalseClass)

    truthiness of the config attribute



40
# File 'lib/krakow/producer/http.rb', line 40

attribute :config, Hash, :default => ->{ Hash.new }

- (Response) create_channel(chan)

Create channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



121
122
123
124
125
126
127
128
# File 'lib/krakow/producer/http.rb', line 121

def create_channel(chan)
  send_message(:post, :create_channel,
    :params => {
      :topic => topic,
      :channel => chan
    }
  )
end

- (Response) create_topic

Create the topic

Returns:



102
103
104
105
106
# File 'lib/krakow/producer/http.rb', line 102

def create_topic
  send_message(:post, :create_topic,
    :params => {:topic => topic}
  )
end

- (Response) delete_channel(chan)

Delete channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



134
135
136
137
138
139
140
141
# File 'lib/krakow/producer/http.rb', line 134

def delete_channel(chan)
  send_message(:post, :delete_channel,
    :params => {
      :topic => topic,
      :channel => chan
    }
  )
end

- (Response) delete_topic

Delete the topic

Returns:



111
112
113
114
115
# File 'lib/krakow/producer/http.rb', line 111

def delete_topic
  send_message(:post, :delete_topic,
    :params => {:topic => topic}
  )
end

- (Response) empty_channel(chan)

Remove all messages from given channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



156
157
158
159
160
161
162
163
# File 'lib/krakow/producer/http.rb', line 156

def empty_channel(chan)
  send_message(:post, :empty_channel,
    :params => {
      :topic => topic,
      :channel => chan
    }
  )
end

- (Response) empty_topic

Remove all messages from topic

Returns:



146
147
148
149
150
# File 'lib/krakow/producer/http.rb', line 146

def empty_topic
  send_message(:post, :empty_topic,
    :params => {:topic => topic}
  )
end

- (String) endpoint

Returns the endpoint attribute

Returns:

  • (String)

    the endpoint attribute



38
# File 'lib/krakow/producer/http.rb', line 38

attribute :endpoint, String, :required => true

- (TrueClass, FalseClass) endpoint?

Returns truthiness of the endpoint attribute

Returns:

  • (TrueClass, FalseClass)

    truthiness of the endpoint attribute



38
# File 'lib/krakow/producer/http.rb', line 38

attribute :endpoint, String, :required => true

- (Response) info

Server information

Returns:



213
214
215
# File 'lib/krakow/producer/http.rb', line 213

def info
  send_message(:get, :info)
end

- (String) inspect Originally defined in module Utils::Lazy::InstanceMethods

Returns:

  • (String)

- (Logger?) log(*args) Originally defined in module Utils::Logging

Log message

Parameters:

  • args (Array, nil)

Returns:

  • (Logger, nil)

- (Response) pause_channel(chan)

Pause messages on given channel

Parameters:

  • chan (String)

    channel name

Returns:



169
170
171
172
173
174
175
176
# File 'lib/krakow/producer/http.rb', line 169

def pause_channel(chan)
  send_message(:post, :pause_channel,
    :params => {
      :topic => topic,
      :channel => chan
    }
  )
end

- (Response) ping

Ping the server

Returns:



206
207
208
# File 'lib/krakow/producer/http.rb', line 206

def ping
  send_message(:get, :ping)
end

- (Response) send_message(method, path, args = {})

Send a message via HTTP

Parameters:

  • method (String, Symbol)

    HTTP method to use (:get, :put, etc)

  • path (String)

    URI path

  • args (Hash) (defaults to: {})

    payload hash

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/krakow/producer/http.rb', line 68

def send_message(method, path, args={})
  build = uri.dup
  build.path = "/#{path}"
  response = HTTP.send(method, build.to_s, args.merge(config))
  begin
    response = MultiJson.load(response.body.to_s)
  rescue MultiJson::LoadError
    response = {'status_code' => response.code, 'status_txt' => response.body.to_s, 'data' => nil}
  end
  Response.new(response)
end

- (Hash) ssl_context

Returns the ssl_context attribute

Returns:

  • (Hash)

    the ssl_context attribute



41
# File 'lib/krakow/producer/http.rb', line 41

attribute :ssl_context, Hash

- (TrueClass, FalseClass) ssl_context?

Returns truthiness of the ssl_context attribute

Returns:

  • (TrueClass, FalseClass)

    truthiness of the ssl_context attribute



41
# File 'lib/krakow/producer/http.rb', line 41

attribute :ssl_context, Hash

- (Response) stats(format = 'json')

Server stats

Parameters:

  • format (String) (defaults to: 'json')

    format of data

Returns:



195
196
197
198
199
200
201
# File 'lib/krakow/producer/http.rb', line 195

def stats(format='json')
  send_message(:get, :stats,
    :params => {
      :format => format
    }
  )
end

- (String) to_s Originally defined in module Utils::Lazy::InstanceMethods

Returns:

  • (String)

- (String) topic

Returns the topic attribute

Returns:

  • (String)

    the topic attribute



39
# File 'lib/krakow/producer/http.rb', line 39

attribute :topic, String, :required => true

- (TrueClass, FalseClass) topic?

Returns truthiness of the topic attribute

Returns:

  • (TrueClass, FalseClass)

    truthiness of the topic attribute



39
# File 'lib/krakow/producer/http.rb', line 39

attribute :topic, String, :required => true

- (Response) unpause_channel(chan)

Resume messages on a given channel

Parameters:

  • chan (String)

    channel name

Returns:



182
183
184
185
186
187
188
189
# File 'lib/krakow/producer/http.rb', line 182

def unpause_channel(chan)
  send_message(:post, :unpause_channel,
    :params => {
      :topic => topic,
      :channel => chan
    }
  )
end

- (Response) write(*payload)

Send messages

Parameters:

  • payload (String)

    message

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/krakow/producer/http.rb', line 84

def write(*payload)
  if(payload.size == 1)
    payload = payload.first
    send_message(:post, :pub,
      :body => payload,
      :params => {:topic => topic}
    )
  else
    send_message(:post, :mpub,
      :body => payload.join("\n"),
      :params => {:topic => topic}
    )
  end
end