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.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#stub ⇒ Object
readonly
Returns the value of attribute stub.
-
#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
11 12 13 14 15 16 |
# File 'lib/simple_segment/request.rb', line 11 def initialize(client) @write_key = client.config.write_key @error_handler = client.config.on_error @stub = client.config.stub @logger = client.config.logger 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 |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/simple_segment/request.rb', line 9 def logger @logger end |
#stub ⇒ Object (readonly)
Returns the value of attribute stub.
9 10 11 |
# File 'lib/simple_segment/request.rb', line 9 def stub @stub 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/simple_segment/request.rb', line 18 def post(path, payload, headers: DEFAULT_HEADERS) response = nil status_code = nil response_body = nil uri = URI(BASE_URL) payload = JSON.generate(payload) if stub logger.debug "stubbed request to \ #{path}: write key = #{write_key}, \ payload = #{payload}" { status: 200, error: nil } else 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 do |res| status_code = res.code response_body = res.body response = res response.value end end end rescue StandardError => e error_handler.call(status_code, response_body, e, response) end |