Class: SimpleSegment::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_segment/request.rb

Constant Summary collapse

BASE_URL =
'https://api.segment.io'.freeze
DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
  'accept' => 'application/json'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, config) ⇒ Request

Returns a new instance of Request


11
12
13
14
# File 'lib/simple_segment/request.rb', line 11

def initialize(path, config)
  @path = path
  @write_key = config.write_key
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


9
10
11
# File 'lib/simple_segment/request.rb', line 9

def path
  @path
end

#write_keyObject (readonly)

Returns the value of attribute write_key


9
10
11
# File 'lib/simple_segment/request.rb', line 9

def write_key
  @write_key
end

Instance Method Details

#post(payload, headers: DEFAULT_HEADERS) ⇒ Object


16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_segment/request.rb', line 16

def post(payload, headers: DEFAULT_HEADERS)
  uri = URI(BASE_URL)
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    payload = JSON.generate(payload)
    request = Net::HTTP::Post.new(path, headers)
    request.basic_auth write_key, nil

    http.request(request, payload)
  end
end