Class: JsonApiToolbox::Service
- Inherits:
-
Object
- Object
- JsonApiToolbox::Service
- Defined in:
- lib/service.rb
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.
-
#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, headers: {}) ⇒ Object
- .parse_response(response) ⇒ Object
- .patch(url: nil, body: nil, headers: {}) ⇒ Object
- .post(url: nil, body: nil, headers: {}) ⇒ Object
- .put(url: nil, body: nil, headers: {}) ⇒ Object
Instance Method Summary collapse
- #build_body ⇒ Object
- #build_header ⇒ Object
- #execute ⇒ Object
-
#initialize(http_method, url, body, extra_headers = {}) ⇒ Service
constructor
A new instance of Service.
- #request ⇒ Object
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
#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 |
#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
74 75 76 77 78 79 |
# File 'lib/service.rb', line 74 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
52 53 54 55 |
# File 'lib/service.rb', line 52 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
69 70 71 72 |
# File 'lib/service.rb', line 69 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
61 62 63 |
# File 'lib/service.rb', line 61 def patch(url: nil, body: nil, headers: {}) new(:patch, url, body, headers).execute end |
.post(url: nil, body: nil, headers: {}) ⇒ Object
57 58 59 |
# File 'lib/service.rb', line 57 def post(url: nil, body: nil, headers: {}) new(:post, url, body, headers).execute end |
.put(url: nil, body: nil, headers: {}) ⇒ Object
65 66 67 |
# File 'lib/service.rb', line 65 def put(url: nil, body: nil, headers: {}) new(:put, url, body, headers).execute end |
Instance Method Details
#build_body ⇒ Object
45 46 47 48 49 |
# File 'lib/service.rb', line 45 def build_body return if http_method == :get body.is_a?(Hash) ? body.to_json : body end |
#build_header ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/service.rb', line 36 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
18 19 20 21 22 23 |
# File 'lib/service.rb', line 18 def execute response = request return if response.code == 404 Service.parse_response(response) end |
#request ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/service.rb', line 25 def request RestClient::Request.execute( method: http_method, url: url, payload: build_body, headers: build_header ) rescue RestClient::ExceptionWithResponse => e e.response end |