Class: JsonApiToolbox::Service
- Inherits:
-
Object
- Object
- JsonApiToolbox::Service
- Defined in:
- lib/service.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
ENV.fetch('JSON_API_TOOLBOX_TIMEOUT', 60).to_i
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#extra_headers ⇒ Object
readonly
Returns the value of attribute extra_headers.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .build_query_string(includes, query_string) ⇒ Object
- .get(url: nil, includes: nil, query_string: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
- .parse_response(response) ⇒ Object
- .patch(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
- .post(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
- .put(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
Instance Method Summary collapse
- #build_body ⇒ Object
- #build_header ⇒ Object
- #execute ⇒ Object
-
#initialize(http_method, url, body, timeout, extra_headers = {}) ⇒ Service
constructor
A new instance of Service.
- #request ⇒ Object
Constructor Details
#initialize(http_method, url, body, timeout, extra_headers = {}) ⇒ Service
Returns a new instance of Service.
13 14 15 16 17 18 19 |
# File 'lib/service.rb', line 13 def initialize(http_method, url, body, timeout, extra_headers = {}) @http_method = http_method @url = url @body = body @timeout = timeout @extra_headers = extra_headers end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/service.rb', line 9 def body @body end |
#extra_headers ⇒ Object (readonly)
Returns the value of attribute extra_headers.
9 10 11 |
# File 'lib/service.rb', line 9 def extra_headers @extra_headers end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
9 10 11 |
# File 'lib/service.rb', line 9 def http_method @http_method end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/service.rb', line 9 def timeout @timeout end |
#url ⇒ Object (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
73 74 75 76 77 78 |
# File 'lib/service.rb', line 73 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, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
51 52 53 54 |
# File 'lib/service.rb', line 51 def get(url: nil, includes: nil, query_string: nil, timeout: DEFAULT_TIMEOUT, headers: {}) body = build_query_string(includes, query_string) new(:get, url, body, timeout, headers).execute end |
.parse_response(response) ⇒ Object
68 69 70 71 |
# File 'lib/service.rb', line 68 def parse_response(response) response_parsed = JSON::Api::Vanilla.parse(response.body) response_parsed.data || response_parsed.errors end |
.patch(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
60 61 62 |
# File 'lib/service.rb', line 60 def patch(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) new(:patch, url, body, timeout, headers).execute end |
.post(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
56 57 58 |
# File 'lib/service.rb', line 56 def post(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) new(:post, url, body, timeout, headers).execute end |
.put(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ Object
64 65 66 |
# File 'lib/service.rb', line 64 def put(url: nil, body: nil, timeout: DEFAULT_TIMEOUT, headers: {}) new(:put, url, body, timeout, headers).execute end |
Instance Method Details
#build_body ⇒ Object
44 45 46 47 48 |
# File 'lib/service.rb', line 44 def build_body return if http_method == :get body.is_a?(Hash) ? body.to_json : body end |
#build_header ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/service.rb', line 35 def build_header headers = { 'Authorization' => RequestStore.store[:token], 'Content-Type' => 'application/json' } headers[:params] = body if http_method == :get headers.merge(extra_headers) end |
#execute ⇒ Object
21 22 23 |
# File 'lib/service.rb', line 21 def execute Service.parse_response(request) end |
#request ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/service.rb', line 25 def request RestClient::Request.execute( method: http_method, url: url, payload: build_body, headers: build_header, timeout: timeout ) end |