Class: ShoutboxClient
- Inherits:
-
Object
- Object
- ShoutboxClient
- Defined in:
- lib/shoutbox_client.rb
Constant Summary collapse
- VALID_STATUS =
{ 'red' => :update, 'yellow' => :update, 'green' => :update, 'remove' => :delete }
Class Method Summary collapse
- .configuration ⇒ Object
- .default_headers(request) ⇒ Object
- .delete_status(options) ⇒ Object
- .host ⇒ Object
- .port ⇒ Object
- .request_url(options) ⇒ Object
- .shout(options) ⇒ Object
- .update_status(options) ⇒ Object
- .valid_status?(status) ⇒ Boolean
Class Method Details
.configuration ⇒ Object
14 15 16 |
# File 'lib/shoutbox_client.rb', line 14 def self.configuration @configuration ||= Shoutbox::Configuration.new end |
.default_headers(request) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/shoutbox_client.rb', line 55 def self.default_headers( request ) request['User-Agent'] = 'Ruby shoutbox-client' request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' request['X-Shoutbox-Auth-Token'] = configuration.auth_token end |
.delete_status(options) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/shoutbox_client.rb', line 45 def self.delete_status( ) response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(self.host, self.port) do |http| req = Net::HTTP::Delete.new( request_url() ) req.body = { :name => [:name], :group => ([:group] || configuration.default_group) }.to_json default_headers(req) http.request(req) end response.body == "OK" end |
.host ⇒ Object
66 67 68 |
# File 'lib/shoutbox_client.rb', line 66 def self.host URI.parse(configuration.host).host end |
.port ⇒ Object
70 71 72 |
# File 'lib/shoutbox_client.rb', line 70 def self.port URI.parse(configuration.host).port end |
.request_url(options) ⇒ Object
62 63 64 |
# File 'lib/shoutbox_client.rb', line 62 def self.request_url( ) '/status' end |
.shout(options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/shoutbox_client.rb', line 18 def self.shout( ) if valid_status?( [:status] ) begin VALID_STATUS[[:status].to_s] == :update ? update_status( ) : delete_status( ) rescue Errno::ECONNREFUSED false end else false end end |
.update_status(options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/shoutbox_client.rb', line 30 def self.update_status( ) response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(self.host, self.port) do |http| req = Net::HTTP::Put.new( request_url() ) default_headers(req) body_data = { :name => [:name], :group => ([:group] || configuration.default_group), :status => [:status].to_s } body_data[:message] = [:message] if [:message] body_data[:expires_in] = [:expires_in] if [:expires_in] body_data[:display_name] = [:display_name] if [:display_name] raise ArgumentError if ([:status] == :red or [:status] == :yellow) and body_data[:message].to_s.empty? req.body = body_data.to_json http.request(req) end response.body == "OK" end |
.valid_status?(status) ⇒ Boolean
74 75 76 |
# File 'lib/shoutbox_client.rb', line 74 def self.valid_status?( status ) VALID_STATUS.keys.include?( status.to_s ) end |