Class: Yodeler::Adapters::HttpAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/yodeler/adapters/http_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, port: nil, path: nil, use_ssl: false, params: {}) ⇒ HttpAdapter

Returns a new instance of HttpAdapter.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/yodeler/adapters/http_adapter.rb', line 11

def initialize(host = nil, port:nil, path:nil, use_ssl:false, params:{})
  @host = host
  @port = port
  @path = path
  @use_ssl = use_ssl
  @default_params = params
  @handlers = {}

  handle(:default) do |url, metric, default_params|
    HTTP.post(url, json: default_params.merge(metric.to_hash))
  end
end

Instance Attribute Details

#default_paramsObject

Returns the value of attribute default_params.



9
10
11
# File 'lib/yodeler/adapters/http_adapter.rb', line 9

def default_params
  @default_params
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/yodeler/adapters/http_adapter.rb', line 5

def host
  @host
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/yodeler/adapters/http_adapter.rb', line 7

def path
  @path
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/yodeler/adapters/http_adapter.rb', line 6

def port
  @port
end

#use_sslObject

Returns the value of attribute use_ssl.



8
9
10
# File 'lib/yodeler/adapters/http_adapter.rb', line 8

def use_ssl
  @use_ssl
end

Instance Method Details

#dispatch(metric) ⇒ Object



28
29
30
31
# File 'lib/yodeler/adapters/http_adapter.rb', line 28

def dispatch(metric)
  dispatcher = @handlers[metric.type] || @handlers[:default]
  dispatcher.call(url, metric, default_params)
end

#handle(type, &block) ⇒ Object



24
25
26
# File 'lib/yodeler/adapters/http_adapter.rb', line 24

def handle(type, &block)
  @handlers[type] = block
end

#urlObject



33
34
35
# File 'lib/yodeler/adapters/http_adapter.rb', line 33

def url
  "#{protocol}://#{host_with_port}#{path}"
end