Class: DripDrop::HTTPClientHandler

Inherits:
BaseHandler show all
Defined in:
lib/dripdrop/handlers/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, opts = {}) ⇒ HTTPClientHandler

Returns a new instance of HTTPClientHandler.



89
90
91
92
93
# File 'lib/dripdrop/handlers/http.rb', line 89

def initialize(uri, opts={})
  @uri     = uri
  @address = @uri.to_s
  @opts    = opts
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



87
88
89
# File 'lib/dripdrop/handlers/http.rb', line 87

def address
  @address
end

#optsObject (readonly)

Returns the value of attribute opts.



87
88
89
# File 'lib/dripdrop/handlers/http.rb', line 87

def opts
  @opts
end

Instance Method Details

#send_message(message, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dripdrop/handlers/http.rb', line 95

def send_message(message,&block)
  dd_message = dd_messagify(message)
  if dd_message.class == DripDrop::Message
    uri_path = @uri.path.empty? ? '/' : @uri.path
    
    req = EM::Protocols::HttpClient.request(
      :host => @uri.host, :port => @uri.port,
      :request => uri_path, :verb => 'POST',
      :contenttype => 'application/json',
      :content => dd_message.encode_json
    )
    req.callback do |response|
      block.call(DripDrop::Message.decode_json(response[:content]))
    end
  else
    raise "Unsupported message type '#{dd_message.class}'"
  end
end