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.
-
#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) ⇒ Object
- .parse_response(response) ⇒ Object
- .patch(url: nil, body: nil) ⇒ Object
- .post(url: nil, body: nil) ⇒ Object
- .put(url: nil, body: nil) ⇒ Object
Instance Method Summary collapse
- #build_body ⇒ Object
- #build_header ⇒ Object
- #execute ⇒ Object
-
#initialize(http_method, url, body) ⇒ Service
constructor
A new instance of Service.
- #request ⇒ Object
Constructor Details
#initialize(http_method, url, body) ⇒ Service
Returns a new instance of Service.
11 12 13 14 15 |
# File 'lib/service.rb', line 11 def initialize(http_method, url, body) @http_method = http_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 |
#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
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) ⇒ Object
51 52 53 54 |
# File 'lib/service.rb', line 51 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
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) ⇒ Object
60 61 62 |
# File 'lib/service.rb', line 60 def patch(url: nil, body: nil) new(:patch, url, body).execute end |
.post(url: nil, body: nil) ⇒ Object
56 57 58 |
# File 'lib/service.rb', line 56 def post(url: nil, body: nil) new(:post, url, body).execute end |
.put(url: nil, body: nil) ⇒ Object
64 65 66 |
# File 'lib/service.rb', line 64 def put(url: nil, body: nil) new(:put, url, body).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 end |
#execute ⇒ Object
17 18 19 20 21 22 |
# File 'lib/service.rb', line 17 def execute response = request return if response.code == 404 Service.parse_response(response) end |
#request ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/service.rb', line 24 def request RestClient::Request.execute( method: http_method, url: url, payload: build_body, headers: build_header ) rescue RestClient::ExceptionWithResponse => e e.response end |