Class: ShoutboxClient

Inherits:
Object
  • Object
show all
Defined in:
lib/shoutbox_client.rb

Constant Summary collapse

VALID_STATUS =
{ 'red'      => :update,
'yellow'   => :update,
'green'    => :update,
'remove'   => :delete  }

Class Method Summary collapse

Class Method Details

.configurationObject



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( options )
  response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(self.host, self.port) do |http|
    req = Net::HTTP::Delete.new( request_url(options) )
    req.body = { :name => options[:name], :group => (options[:group] || configuration.default_group) }.to_json
    default_headers(req)
    http.request(req)
  end
  response.body == "OK"
end

.hostObject



66
67
68
# File 'lib/shoutbox_client.rb', line 66

def self.host
  URI.parse(configuration.host).host
end

.portObject



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( options )
  '/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( options )
  if valid_status?( options[:status] )
    begin
      VALID_STATUS[options[:status].to_s] == :update ? update_status( options ) : delete_status( options ) 
    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( options )
  response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(self.host, self.port) do |http|
    req = Net::HTTP::Put.new( request_url(options) )
    default_headers(req)
    body_data = { :name => options[:name], :group => (options[:group] || configuration.default_group), :status => options[:status].to_s }
    body_data[:message]      = options[:message]      if options[:message]
    body_data[:expires_in]   = options[:expires_in]   if options[:expires_in]
    body_data[:display_name] = options[:display_name] if options[:display_name]
    raise ArgumentError if (options[:status] == :red or options[: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

Returns:

  • (Boolean)


74
75
76
# File 'lib/shoutbox_client.rb', line 74

def self.valid_status?( status )
  VALID_STATUS.keys.include?( status.to_s )
end