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.
-
#method ⇒ Object
readonly
Returns the value of attribute 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) ⇒ Object
- .parse_response(response) ⇒ Object
- .patch(url: nil, body: nil) ⇒ Object
- .post(url: nil, body: nil) ⇒ Object
Instance Method Summary collapse
- #build_body ⇒ Object
- #build_header ⇒ Object
- #execute ⇒ Object
-
#initialize(method, url, body) ⇒ Service
constructor
A new instance of Service.
- #request ⇒ Object
Constructor Details
#initialize(method, url, body) ⇒ Service
Returns a new instance of Service.
11 12 13 14 15 |
# File 'lib/service.rb', line 11 def initialize(method, url, body) @method = method @url = url @body = body 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 |
#method ⇒ Object (readonly)
Returns the value of attribute method.
9 10 11 |
# File 'lib/service.rb', line 9 def method @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
66 67 68 69 70 71 |
# File 'lib/service.rb', line 66 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) ⇒ Object
48 49 50 51 |
# File 'lib/service.rb', line 48 def get(url: nil, includes: nil, query_string: nil) body = build_query_string(includes, query_string) new(:get, url, body).execute end |
.parse_response(response) ⇒ Object
61 62 63 64 |
# File 'lib/service.rb', line 61 def parse_response(response) response_parsed = JSON::Api::Vanilla.parse(response.body) response_parsed.data || response_parsed.errors end |
.patch(url: nil, body: nil) ⇒ Object
57 58 59 |
# File 'lib/service.rb', line 57 def patch(url: nil, body: nil) new(:patch, url, body).execute end |
.post(url: nil, body: nil) ⇒ Object
53 54 55 |
# File 'lib/service.rb', line 53 def post(url: nil, body: nil) new(:post, url, body).execute end |
Instance Method Details
#build_body ⇒ Object
43 44 45 |
# File 'lib/service.rb', line 43 def build_body return body if method != :get end |
#build_header ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/service.rb', line 34 def build_header headers = { 'Authorization' => RequestStore.store[:token], 'Content-Type' => 'application/json' } headers[:params] = body if method == :get headers end |
#execute ⇒ Object
17 18 19 20 21 |
# File 'lib/service.rb', line 17 def execute response = request return if response.code == 404 Service.parse_response(response) end |
#request ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/service.rb', line 23 def request RestClient::Request.execute( method: method, url: url, payload: build_body, headers: build_header ) rescue RestClient::ExceptionWithResponse => e e.response end |