Module: MyJohnDeere::RESTMethods::ClassMethods
- Defined in:
- lib/myjohndeere/rest_methods.rb
Instance Attribute Summary collapse
-
#base_jd_resource ⇒ Object
Returns the value of attribute base_jd_resource.
-
#list_resource_path ⇒ Object
Returns the value of attribute list_resource_path.
-
#retrieve_resource_path ⇒ Object
Returns the value of attribute retrieve_resource_path.
-
#supports_delete ⇒ Object
Returns the value of attribute supports_delete.
Instance Method Summary collapse
- #build_resource_base_path!(resource_path, options = {}) ⇒ Object
- #delete(access_token, id) ⇒ Object
-
#list(access_token, options = {}) ⇒ Object
If the resource requires a base resource, specify it in the format of: <resource_singular_name_id>: <ID>.
- #retrieve(access_token, id, options = {}) ⇒ Object
- #send_create(access_token, body, path_builder_options = {}) ⇒ Object
- #validate_access_token(access_token) ⇒ Object
Instance Attribute Details
#base_jd_resource ⇒ Object
Returns the value of attribute base_jd_resource.
5 6 7 |
# File 'lib/myjohndeere/rest_methods.rb', line 5 def base_jd_resource @base_jd_resource end |
#list_resource_path ⇒ Object
Returns the value of attribute list_resource_path.
7 8 9 |
# File 'lib/myjohndeere/rest_methods.rb', line 7 def list_resource_path @list_resource_path end |
#retrieve_resource_path ⇒ Object
Returns the value of attribute retrieve_resource_path.
6 7 8 |
# File 'lib/myjohndeere/rest_methods.rb', line 6 def retrieve_resource_path @retrieve_resource_path end |
#supports_delete ⇒ Object
Returns the value of attribute supports_delete.
4 5 6 |
# File 'lib/myjohndeere/rest_methods.rb', line 4 def supports_delete @supports_delete end |
Instance Method Details
#build_resource_base_path!(resource_path, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/myjohndeere/rest_methods.rb', line 48 def build_resource_base_path!(resource_path, = {}) expected_definitions = resource_path.scan(/%{(.+?)}/) return resource_path if expected_definitions.empty? base_resources = {} .each do |key, val| base_resources[key] = .delete(key) if key.match(/_id\Z/) end MyJohnDeere.logger.info("Building resource path: #{resource_path}, with ids: #{base_resources}") begin return resource_path % base_resources rescue KeyError raise ArgumentError.new("You must specify #{expected_definitions.join(", ")} as part of this request path") end end |
#delete(access_token, id) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/myjohndeere/rest_methods.rb', line 41 def delete(access_token, id) raise UnsupportedRequestError.new("Delete is not supported by this resource") if !self.supports_delete response = access_token.execute_request(:delete, "#{self.base_jd_resource}/#{id}") return response.code == 204 end |
#list(access_token, options = {}) ⇒ Object
If the resource requires a base resource, specify it in the format of: <resource_singular_name_id>: <ID>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/myjohndeere/rest_methods.rb', line 10 def list(access_token, = {}) validate_access_token(access_token) = {count: 10, start: 0, etag: nil}.merge() [:body] ||= {} # The count and start are in this list,so move them into the body SPECIAL_BODY_PARAMETERS.each do |sbp| [:body][sbp] = [sbp] end response = access_token.execute_request(:get, build_resource_base_path!(self.list_resource_path, ), ) return ListObject.new( self, access_token, response.data, options: .merge( etag: response.http_headers[MyJohnDeere::ETAG_HEADER_KEY] ) ) end |
#retrieve(access_token, id, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/myjohndeere/rest_methods.rb', line 32 def retrieve(access_token, id, ={}) validate_access_token(access_token) response = access_token.execute_request(:get, "#{build_resource_base_path!(self.retrieve_resource_path, )}/#{id}", ) return new(response.data, access_token) end |
#send_create(access_token, body, path_builder_options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/myjohndeere/rest_methods.rb', line 67 def send_create(access_token, body, = {}) response = access_token.execute_request(:post, build_resource_base_path!(self.list_resource_path, ), body: body ) #{"Content-Type"=>"text/plain", "X-Deere-Handling-Server"=>"ldxtc3", "X-Frame-Options"=>"SAMEORIGIN", "Location"=>"https://sandboxapi.deere.com/platform/mapLayers/e2711205-c5df-445e-aad5-81eaf9090e6c", "X-Deere-Elapsed-Ms"=>"162", "Vary"=>"Accept-Encoding", "Expires"=>"Thu, 14 Sep 2017 15:52:24 GMT", "Cache-Control"=>"max-age=0, no-cache", "Pragma"=>"no-cache", "Date"=>"Thu, 14 Sep 2017 15:52:24 GMT", "Transfer-Encoding"=>"chunked", "Connection"=>"close, Transfer-Encoding"} id = get_created_id_from_response_headers(self.base_jd_resource, response) if id.nil? then return nil else return self.new(HashUtils.deep_stringify_keys({"id" => id}.merge(body))) end end |
#validate_access_token(access_token) ⇒ Object
63 64 65 |
# File 'lib/myjohndeere/rest_methods.rb', line 63 def validate_access_token(access_token) raise ArgumentError.new("The first argument must be an #{AccessToken}") if !access_token.is_a?(AccessToken) end |