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 included from Utils::Lazy::InstanceMethods

#arguments

Attributes collapse

Instance Method Summary collapse

Methods included from Utils::Lazy::ClassMethods

attribute, attributes, set_attributes

Methods included from Utils::Lazy::InstanceMethods

#inspect, #to_s

Methods included from Utils::Lazy

included

Methods included from Utils::Logging

level=, #log

Constructor Details

#initialize(args = {}) ⇒ Http

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

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#build_ssl_contextOpenSSL::SSL::SSLContext

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

#configHash

Returns the config attribute.

Returns:

  • (Hash)

    the config attribute



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

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

#config?TrueClass, FalseClass

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 }

#create_channel(chan) ⇒ Response

Create channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



126
127
128
129
130
131
132
133
# File 'lib/krakow/producer/http.rb', line 126

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

#create_topicResponse

Create the topic

Returns:



107
108
109
110
111
# File 'lib/krakow/producer/http.rb', line 107

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

#delete_channel(chan) ⇒ Response

Delete channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



139
140
141
142
143
144
145
146
# File 'lib/krakow/producer/http.rb', line 139

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

#delete_topicResponse

Delete the topic

Returns:



116
117
118
119
120
# File 'lib/krakow/producer/http.rb', line 116

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

#empty_channel(chan) ⇒ Response

Remove all messages from given channel on topic

Parameters:

  • chan (String)

    channel name

Returns:



161
162
163
164
165
166
167
168
# File 'lib/krakow/producer/http.rb', line 161

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

#empty_topicResponse

Remove all messages from topic

Returns:



151
152
153
154
155
# File 'lib/krakow/producer/http.rb', line 151

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

#endpointString

Returns the endpoint attribute.

Returns:

  • (String)

    the endpoint attribute



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

attribute :endpoint, String, :required => true

#endpoint?TrueClass, FalseClass

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

#infoResponse

Server information

Returns:



218
219
220
# File 'lib/krakow/producer/http.rb', line 218

def info
  send_message(:get, :info)
end

#pause_channel(chan) ⇒ Response

Pause messages on given channel

Parameters:

  • chan (String)

    channel name

Returns:



174
175
176
177
178
179
180
181
# File 'lib/krakow/producer/http.rb', line 174

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

#pingResponse

Ping the server

Returns:



211
212
213
# File 'lib/krakow/producer/http.rb', line 211

def ping
  send_message(:get, :ping)
end

#send_message(method, path, args = {}) ⇒ Response

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
79
80
81
82
83
# 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,
      'response' => response.body.to_s,
      'data' => nil,
    }
  end
  Response.new(response)
end

#ssl_contextHash

Returns the ssl_context attribute.

Returns:

  • (Hash)

    the ssl_context attribute



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

attribute :ssl_context, Hash

#ssl_context?TrueClass, FalseClass

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

#stats(format = 'json') ⇒ Response

Server stats

Parameters:

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

    format of data

Returns:



200
201
202
203
204
205
206
# File 'lib/krakow/producer/http.rb', line 200

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

#topicString

Returns the topic attribute.

Returns:

  • (String)

    the topic attribute



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

attribute :topic, String, :required => true

#topic?TrueClass, FalseClass

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

#unpause_channel(chan) ⇒ Response

Resume messages on a given channel

Parameters:

  • chan (String)

    channel name

Returns:



187
188
189
190
191
192
193
194
# File 'lib/krakow/producer/http.rb', line 187

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

#write(*payload) ⇒ Response

Send messages

Parameters:

  • payload (String)

    message

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/krakow/producer/http.rb', line 89

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