Class: Spectator::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/spectator/http.rb

Overview

Helper for HTTP requests

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ Http

Create a new instance using the given registry to record stats for the requests performed



9
10
11
# File 'lib/spectator/http.rb', line 9

def initialize(registry)
  @registry = registry
end

Instance Method Details

#post_json(endpoint, payload) ⇒ Object

Send a JSON payload to a given endpoing



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spectator/http.rb', line 14

def post_json(endpoint, payload)
  s = payload.to_json
  uri = URI(endpoint)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  req.body = s
  begin
    res = http.request(req)
  rescue StandardError => e
    Spectator.logger.info("Cause #{e.cause} - msg=#{e.message}")
    return 400
  end

  res.value
end