Class: SimpleSegment::Request
- Inherits:
-
Object
- Object
- SimpleSegment::Request
- Defined in:
- lib/simple_segment/request.rb
Constant Summary collapse
- 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.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http_options ⇒ Object
readonly
Returns the value of attribute http_options.
-
#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
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
Constructor Details
#initialize(client) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 |
# File 'lib/simple_segment/request.rb', line 12 def initialize(client) @write_key = client.config.write_key @error_handler = client.config.on_error @stub = client.config.stub @logger = client.config.logger @http_options = client.config. @host = client.config.host end |
Instance Attribute Details
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def error_handler @error_handler end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def host @host end |
#http_options ⇒ Object (readonly)
Returns the value of attribute http_options.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def @http_options end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def logger @logger end |
#stub ⇒ Object (readonly)
Returns the value of attribute stub.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def stub @stub end |
#write_key ⇒ Object (readonly)
Returns the value of attribute write_key.
10 11 12 |
# File 'lib/simple_segment/request.rb', line 10 def write_key @write_key end |
Instance Method Details
#post(path, payload, headers: DEFAULT_HEADERS) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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 46 47 48 |
# File 'lib/simple_segment/request.rb', line 21 def post(path, payload, headers: DEFAULT_HEADERS) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength response = nil status_code = nil response_body = nil uri = URI("https://#{host}#{path}") payload = JSON.generate(payload) if stub logger.debug "stubbed request to \ #{uri.path}: write key = #{write_key}, \ payload = #{payload}" { status: 200, error: nil } else Net::HTTP.start(uri.host, uri.port, :ENV, ) do |http| request = Net::HTTP::Post.new(uri.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 |