Class: IntellisenseRuby::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/intellisense-ruby/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

public: Creates a new request object to send analytics batch



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/intellisense-ruby/request.rb', line 14

def initialize(options = {})

  options[:url] ||= IntellisenseRuby::Defaults::Request::BASE_URL
  options[:ssl] ||= IntellisenseRuby::Defaults::Request::SSL
  options[:headers] ||= IntellisenseRuby::Defaults::Request::HEADERS
  @path = options[:path] || IntellisenseRuby::Defaults::Request::PATH

  @conn = Faraday.new(options) do |faraday|
    faraday.request :json
    faraday.response :json, :content_type => /\bjson$/
    faraday.adapter Faraday.default_adapter
  end
end

Instance Method Details

#post(secret, batch) ⇒ Object

public: Posts the secret and batch of messages to the API.

returns - Response of the status and error if it exists



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/intellisense-ruby/request.rb', line 31

def post(secret, batch)

  status, error = nil, nil

  begin
    res = @conn.post do |req|
      req.options[:timeout] = 8
      req.options[:open_timeout] = 3
      req.url(@path)
      req.body = IntellisenseRuby::JSON::dump(secret: secret, batch: batch)
    end
    status = res.status
    error  = res.body["error"]

  rescue Exception => err
    status = -1
    error = "Connection error: #{err}"
  end

  IntellisenseRuby::Response.new(status, error)
end