Class: Telebugs::Sender

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

Overview

Responsible for sending HTTP requests to Telebugs.

Constant Summary collapse

CONTENT_TYPE =
"application/json"
USER_AGENT =
"telebugs-ruby/#{Telebugs::VERSION} (#{RUBY_ENGINE}/#{RUBY_VERSION})"

Instance Method Summary collapse

Constructor Details

#initializeSender

Returns a new instance of Sender.



10
11
12
13
# File 'lib/telebugs/sender.rb', line 10

def initialize
  @config = Config.instance
  @authorization = "Bearer #{@config.api_key}"
end

Instance Method Details

#send(data) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/telebugs/sender.rb', line 15

def send(data)
  req = build_request(@config.api_url, data)

  resp = build_https(@config.api_url).request(req)
  if resp.code_type == Net::HTTPCreated
    return JSON.parse(resp.body)
  end

  begin
    reason = JSON.parse(resp.body)
  rescue JSON::ParserError
    nil
  end

  raise HTTPError, "#{resp.code_type} (#{resp.code}): #{reason}"
end