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
# File 'lib/apisync/http_client.rb', line 10

def initialize(resource_name:, options: {})
  @resource_name = resource_name
  @options = options
end

Instance Method Details

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



33
34
35
36
37
38
39
40
# File 'lib/apisync/http_client.rb', line 33

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

  wrap_response(HTTParty.get(
    url(id: id, filters: filters),
    headers: header.merge(headers)
  ))
end

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



15
16
17
18
19
20
21
# File 'lib/apisync/http_client.rb', line 15

def post(data:, headers: {})
  wrap_response(HTTParty.post(
    url,
    body: {data: payload_from_data(data)}.to_json,
    headers: header.merge(headers)
  ))
end

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



23
24
25
26
27
28
29
30
31
# File 'lib/apisync/http_client.rb', line 23

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

  wrap_response(HTTParty.put(
    url(id: id),
    body: {data: payload_from_data(data)}.to_json,
    headers: header.merge(headers)
  ))
end