Class: Justlogging::Transmitter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, action, api_key) ⇒ Transmitter

Returns a new instance of Transmitter.



10
11
12
13
14
# File 'lib/justlogging/transmitter.rb', line 10

def initialize(endpoint, action, api_key)
  @endpoint = endpoint
  @action = action
  @api_key = api_key
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



8
9
10
# File 'lib/justlogging/transmitter.rb', line 8

def action
  @action
end

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/justlogging/transmitter.rb', line 8

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/justlogging/transmitter.rb', line 8

def endpoint
  @endpoint
end

Instance Method Details

#encoded_message(payload) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/justlogging/transmitter.rb', line 29

def encoded_message(payload)
  encoded_payload = {}.tap do |hsh|
    payload.each do |key, val|
      hsh[key] = ActiveSupport::JSON.encode(val)
    end
  end
  message(encoded_payload)
end

#message(encoded_hash) ⇒ Object



38
39
40
41
42
# File 'lib/justlogging/transmitter.rb', line 38

def message(encoded_hash)
  Net::HTTP::Post.new(uri.request_uri).tap do |post|
    post.set_form_data({'api_key' => api_key}.merge(encoded_hash))
  end
end

#transmit(payload) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/justlogging/transmitter.rb', line 20

def transmit(payload)
  begin
    result = http_client.request(encoded_message(payload))
    result.code
  rescue
    nil
  end
end

#uriObject



16
17
18
# File 'lib/justlogging/transmitter.rb', line 16

def uri
  URI("#{@endpoint}/#{@action}")
end