Class: MyJohnDeere::Requestable

Inherits:
Object
  • Object
show all
Defined in:
lib/myjohndeere/requestable.rb

Direct Known Subclasses

ListObject, SingleResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_object = {}, access_token = nil) ⇒ Requestable

Returns a new instance of Requestable.



5
6
7
8
9
10
11
12
# File 'lib/myjohndeere/requestable.rb', line 5

def initialize(json_object = {}, access_token = nil)
  self.links = json_object["links"] || []
  self.access_token = access_token
  approved_class = MyJohnDeere::AccessToken
  if !self.access_token.nil? && !self.access_token.is_a?(approved_class) then
    raise ArgumentError.new("Expected a #{approved_class}, do not know how to handle #{self.access_token.class}")
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/myjohndeere/requestable.rb', line 3

def access_token
  @access_token
end

Returns the value of attribute links.



3
4
5
# File 'lib/myjohndeere/requestable.rb', line 3

def links
  @links
end

Class Method Details

.get_created_id_from_response_headers(resource, response) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/myjohndeere/requestable.rb', line 23

def self.get_created_id_from_response_headers(resource, response)
  # 201 is the expected response code
  # The lowercase location shouldn't be needed but sometimes it is returned as lowercase...
  created_id = response.http_headers["Location"] || response.http_headers["location"]
  if !created_id.nil? then
    created_id = /#{resource}\/([^\/]+)\Z/.match(created_id)[1]
  else
    # didn't succeed
    MyJohnDeere.logger.info("Failed to create a #{resource}: #{response}")
    return nil
  end
end

Instance Method Details



14
15
16
17
18
19
20
21
# File 'lib/myjohndeere/requestable.rb', line 14

def extract_link_with_rel_from_list(rel_target, regex_to_capture_item)
  link = self.links.detect { |l| l["rel"] == rel_target }
  if link then
    return regex_to_capture_item.match(link["uri"])[1]
  else
    return nil
  end
end