Class: Krakow::Producer::Http
- Inherits:
-
Object
- Object
- Krakow::Producer::Http
- 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
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes included from Utils::Lazy::InstanceMethods
Attributes collapse
-
#config ⇒ Hash
The config attribute.
-
#config? ⇒ TrueClass, FalseClass
Truthiness of the config attribute.
-
#endpoint ⇒ String
The endpoint attribute.
-
#endpoint? ⇒ TrueClass, FalseClass
Truthiness of the endpoint attribute.
-
#ssl_context ⇒ Hash
The ssl_context attribute.
-
#ssl_context? ⇒ TrueClass, FalseClass
Truthiness of the ssl_context attribute.
-
#topic ⇒ String
The topic attribute.
-
#topic? ⇒ TrueClass, FalseClass
Truthiness of the topic attribute.
Instance Method Summary collapse
-
#build_ssl_context ⇒ OpenSSL::SSL::SSLContext
Create a new SSL context.
-
#create_channel(chan) ⇒ Response
Create channel on topic.
-
#create_topic ⇒ Response
Create the topic.
-
#delete_channel(chan) ⇒ Response
Delete channel on topic.
-
#delete_topic ⇒ Response
Delete the topic.
-
#empty_channel(chan) ⇒ Response
Remove all messages from given channel on topic.
-
#empty_topic ⇒ Response
Remove all messages from topic.
-
#info ⇒ Response
Server information.
-
#initialize(args = {}) ⇒ Http
constructor
A new instance of Http.
-
#pause_channel(chan) ⇒ Response
Pause messages on given channel.
-
#ping ⇒ Response
Ping the server.
-
#send_message(method, path, args = {}) ⇒ Response
Send a message via HTTP.
-
#stats(format = 'json') ⇒ Response
Server stats.
-
#unpause_channel(chan) ⇒ Response
Resume messages on a given channel.
-
#write(*payload) ⇒ Response
Send messages.
Methods included from Utils::Lazy::ClassMethods
attribute, attributes, set_attributes
Methods included from Utils::Lazy::InstanceMethods
Methods included from Utils::Lazy
Methods included from Utils::Logging
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
#uri ⇒ Object (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_context ⇒ OpenSSL::SSL::SSLContext
Create a new SSL context
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 |
#config ⇒ Hash
Returns 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.
40 |
# File 'lib/krakow/producer/http.rb', line 40 attribute :config, Hash, :default => ->{ Hash.new } |
#create_channel(chan) ⇒ Response
Create channel on topic
126 127 128 129 130 131 132 133 |
# File 'lib/krakow/producer/http.rb', line 126 def create_channel(chan) (:post, :create_channel, :params => { :topic => topic, :channel => chan } ) end |
#create_topic ⇒ Response
Create the topic
107 108 109 110 111 |
# File 'lib/krakow/producer/http.rb', line 107 def create_topic (:post, :create_topic, :params => {:topic => topic} ) end |
#delete_channel(chan) ⇒ Response
Delete channel on topic
139 140 141 142 143 144 145 146 |
# File 'lib/krakow/producer/http.rb', line 139 def delete_channel(chan) (:post, :delete_channel, :params => { :topic => topic, :channel => chan } ) end |
#delete_topic ⇒ Response
Delete the topic
116 117 118 119 120 |
# File 'lib/krakow/producer/http.rb', line 116 def delete_topic (:post, :delete_topic, :params => {:topic => topic} ) end |
#empty_channel(chan) ⇒ Response
Remove all messages from given channel on topic
161 162 163 164 165 166 167 168 |
# File 'lib/krakow/producer/http.rb', line 161 def empty_channel(chan) (:post, :empty_channel, :params => { :topic => topic, :channel => chan } ) end |
#empty_topic ⇒ Response
Remove all messages from topic
151 152 153 154 155 |
# File 'lib/krakow/producer/http.rb', line 151 def empty_topic (:post, :empty_topic, :params => {:topic => topic} ) end |
#endpoint ⇒ String
Returns 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.
38 |
# File 'lib/krakow/producer/http.rb', line 38 attribute :endpoint, String, :required => true |
#info ⇒ Response
Server information
218 219 220 |
# File 'lib/krakow/producer/http.rb', line 218 def info (:get, :info) end |
#pause_channel(chan) ⇒ Response
Pause messages on given channel
174 175 176 177 178 179 180 181 |
# File 'lib/krakow/producer/http.rb', line 174 def pause_channel(chan) (:post, :pause_channel, :params => { :topic => topic, :channel => chan } ) end |
#ping ⇒ Response
Ping the server
211 212 213 |
# File 'lib/krakow/producer/http.rb', line 211 def ping (:get, :ping) end |
#send_message(method, path, args = {}) ⇒ Response
Send a message via HTTP
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 (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_context ⇒ Hash
Returns 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.
41 |
# File 'lib/krakow/producer/http.rb', line 41 attribute :ssl_context, Hash |
#stats(format = 'json') ⇒ Response
Server stats
200 201 202 203 204 205 206 |
# File 'lib/krakow/producer/http.rb', line 200 def stats(format='json') (:get, :stats, :params => { :format => format } ) end |
#topic ⇒ String
Returns 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.
39 |
# File 'lib/krakow/producer/http.rb', line 39 attribute :topic, String, :required => true |
#unpause_channel(chan) ⇒ Response
Resume messages on a given channel
187 188 189 190 191 192 193 194 |
# File 'lib/krakow/producer/http.rb', line 187 def unpause_channel(chan) (:post, :unpause_channel, :params => { :topic => topic, :channel => chan } ) end |
#write(*payload) ⇒ Response
Send messages
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 (:post, :pub, :body => payload, :params => {:topic => topic} ) else (:post, :mpub, :body => payload.join("\n"), :params => {:topic => topic} ) end end |