Class: JsonApiToolbox::Service

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, url, body, extra_headers = {}) ⇒ Service

Returns a new instance of Service.



11
12
13
14
15
16
# File 'lib/service.rb', line 11

def initialize(http_method, url, body, extra_headers = {})
  @http_method = http_method
  @url = url
  @body = body
  @extra_headers = extra_headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#extra_headersObject (readonly)

Returns the value of attribute extra_headers.



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

def extra_headers
  @extra_headers
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



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

def http_method
  @http_method
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.build_query_string(includes, query_string) ⇒ Object



69
70
71
72
73
74
# File 'lib/service.rb', line 69

def build_query_string(includes, query_string)
  params = {}
  params[:includes] = includes if includes
  params.merge!(query_string) if query_string
  params
end

.get(url: nil, includes: nil, query_string: nil, headers: {}) ⇒ Object



47
48
49
50
# File 'lib/service.rb', line 47

def get(url: nil, includes: nil, query_string: nil, headers: {})
  body = build_query_string(includes, query_string)
  new(:get, url, body, headers).execute
end

.parse_response(response) ⇒ Object



64
65
66
67
# File 'lib/service.rb', line 64

def parse_response(response)
  response_parsed = JSON::Api::Vanilla.parse(response.body)
  response_parsed.data || response_parsed.errors
end

.patch(url: nil, body: nil, headers: {}) ⇒ Object



56
57
58
# File 'lib/service.rb', line 56

def patch(url: nil, body: nil, headers: {})
  new(:patch, url, body, headers).execute
end

.post(url: nil, body: nil, headers: {}) ⇒ Object



52
53
54
# File 'lib/service.rb', line 52

def post(url: nil, body: nil, headers: {})
  new(:post, url, body, headers).execute
end

.put(url: nil, body: nil, headers: {}) ⇒ Object



60
61
62
# File 'lib/service.rb', line 60

def put(url: nil, body: nil, headers: {})
  new(:put, url, body, headers).execute
end

Instance Method Details

#build_bodyObject



40
41
42
43
44
# File 'lib/service.rb', line 40

def build_body
  return if http_method == :get

  body.is_a?(Hash) ? body.to_json : body
end

#build_headerObject



31
32
33
34
35
36
37
38
# File 'lib/service.rb', line 31

def build_header
  headers = {
    'Authorization' => RequestStore.store[:token],
    'Content-Type' => 'application/json'
  }
  headers[:params] = body if http_method == :get
  headers.merge(extra_headers)
end

#executeObject



18
19
20
# File 'lib/service.rb', line 18

def execute
  Service.parse_response(request)
end

#requestObject



22
23
24
25
26
27
28
29
# File 'lib/service.rb', line 22

def request
  RestClient::Request.execute(
    method: http_method,
    url: url,
    payload: build_body,
    headers: build_header
  )
end