Class: SimpleSegment::Request
- Inherits:
-
Object
- Object
- SimpleSegment::Request
- 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
-
#error_handler ⇒ Object
readonly
Returns the value of attribute error_handler.
-
#write_key ⇒ Object
readonly
Returns the value of attribute write_key.
Instance Method Summary collapse
-
#initialize(client) ⇒ Request
constructor
A new instance of Request.
- #post(path, payload, headers: DEFAULT_HEADERS) ⇒ Object
Constructor Details
#initialize(client) ⇒ Request
Returns a new instance of Request.
11 12 13 14 |
# File 'lib/simple_segment/request.rb', line 11 def initialize(client) @write_key = client.config.write_key @error_handler = client.config.on_error end |
Instance Attribute Details
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler.
9 10 11 |
# File 'lib/simple_segment/request.rb', line 9 def error_handler @error_handler end |
#write_key ⇒ Object (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(path, payload, headers: DEFAULT_HEADERS) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/simple_segment/request.rb', line 16 def post(path, payload, headers: DEFAULT_HEADERS) response, status_code, response_body = nil, nil, nil uri = URI(BASE_URL) payload = JSON.generate(payload) Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| request = Net::HTTP::Post.new(path, headers) request.basic_auth write_key, nil http.request(request, payload).tap { |res| status_code = res.code response_body = res.body response = res response.value } end rescue StandardError => e error_handler.call(status_code, response_body, e, response) end |