Class: PR::Pin::Adapter::Handlers::JSONRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/pr/pin/adapter/handlers/json_request.rb

Constant Summary collapse

SSL_PORT =
443

Instance Method Summary collapse

Instance Method Details

#call(dataset) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pr/pin/adapter/handlers/json_request.rb', line 8

def call(dataset)
  uri = dataset.uri

  http = Net::HTTP.new(uri.host, uri.port).tap do |client|
    if SSL_PORT == client.port
      client.use_ssl = true
      client.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
  end

  request_class = Net::HTTP.const_get(
    PR::Pin::Inflector.classify(dataset.request_method)
  )

  request = request_class.new(uri.request_uri)
  request.basic_auth(dataset.secret_key, '')

  dataset.headers.each_with_object(request) do |(header, value), request|
    request[header.to_s] = value
  end

  request.body = dataset.params.to_json if dataset.params.any?

  http.request(request)
end