Class: Apisync::HttpClient

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

Constant Summary collapse

VERSION_PREFIX =
"".freeze
HEADER =
{
  "Content-Type" => "application/vnd.api+json",
  "Accept"       => "application/vnd.api+json"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(resource_name:, options: {}) ⇒ HttpClient

Returns a new instance of HttpClient.



10
11
12
13
14
# File 'lib/apisync/http_client.rb', line 10

def initialize(resource_name:, options: {})
  @resource_name = resource_name
  @options = options
  @logger = options[:logger] || ::Logger.new(IO::NULL)
end

Instance Method Details

#get(id: nil, filters: nil, headers: {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/apisync/http_client.rb', line 46

def get(id: nil, filters: nil, headers: {})
  raise Apisync::InvalidFilter if !filters.nil? && !filters.is_a?(Hash)

  url = request_url(id: id, filters: filters)
  output_verbose_request(:get, url)

  wrap_response(HTTParty.get(
    url,
    headers: request_header.merge(headers)
  ))
end

#post(data:, headers: {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/apisync/http_client.rb', line 16

def post(data:, headers: {})
  request_body = {data: payload_from_data(data)}
  url = request_url
  header = request_header.merge(headers)

  output_verbose_request(:post, url, request_body, header)

  wrap_response(HTTParty.post(
    request_url,
    body: request_body.to_json,
    headers: header
  ))
end

#put(id:, data:, headers: {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apisync/http_client.rb', line 30

def put(id:, data:, headers: {})
  raise Apisync::UrlAndPayloadIdMismatch unless id == data[:id]

  request_body = {data: payload_from_data(data)}
  url = request_url(id: id)
  header = request_header.merge(headers)

  output_verbose_request(:put, url, request_body, header)

  wrap_response(HTTParty.put(
    url,
    body: request_body.to_json,
    headers: header
  ))
end