Class: Monri::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/monri/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configMonri::Config

Returns:



5
6
7
# File 'lib/monri/http_client.rb', line 5

def config
  @config
end

Instance Method Details

#build_url(url) ⇒ URI

Parameters:

  • url (String)

Returns:

  • (URI)


43
44
45
# File 'lib/monri/http_client.rb', line 43

def build_url(url)
  URI.parse("#{config.base_api_url}#{url}")
end

#get(url, options = {}) ⇒ Monri::ApiHttpResponse

Parameters:

  • url (String)
  • options (Hash) (defaults to: {})

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/monri/http_client.rb', line 10

def get(url, options = {})
  # TODO: validate
  uri = build_url(url)
  headers = prepare_headers(options)
  # Create the HTTP objects
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri, headers)
  # Send the request
  response = http.request(request)
  ApiHttpResponse.new.from_net(response)
end

#post(url, body, options = {}) ⇒ Monri::ApiHttpResponse

Parameters:

  • url (String)
  • body (Hash)
  • options (Hash) (defaults to: {})

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/monri/http_client.rb', line 27

def post(url, body, options = {})
  # TODO: validate
  uri = build_url(url)
  headers = prepare_headers(options)
  # Create the HTTP objects
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = body.to_json
  # Send the request
  response = http.request(request)
  ApiHttpResponse.new.from_net(response)
end