Class: Taleo::Resource
- Inherits:
-
Object
- Object
- Taleo::Resource
- Defined in:
- lib/taleo/resource.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.has_many(name, type, through: name, singular: name, plural: name) ⇒ Object
Define a has-many relationship with another resource.
-
.has_one(name, type, through: name, identifier: name) ⇒ Object
Define a has-one relationship with another resource.
Instance Method Summary collapse
-
#initialize(data, client) ⇒ Resource
constructor
A new instance of Resource.
-
#related_resource(name, through, identifier, type) ⇒ Object
Retrieve a related resource’s data.
-
#related_resources(name, type, through, singular, plural) ⇒ Object
Retrieve a set of related resources’ data.
-
#relationship_urls ⇒ Object
Get URLs for related resources.
Constructor Details
#initialize(data, client) ⇒ Resource
Returns a new instance of Resource.
7 8 9 10 |
# File 'lib/taleo/resource.rb', line 7 def initialize(data, client) @data = data @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/taleo/resource.rb', line 5 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/taleo/resource.rb', line 5 def data @data end |
Class Method Details
.has_many(name, type, through: name, singular: name, plural: name) ⇒ Object
Define a has-many relationship with another resource
24 25 26 27 28 29 30 31 32 |
# File 'lib/taleo/resource.rb', line 24 def self.has_many(name, type, through: name, singular: name, plural: name) define_method(:"has_#{name}?") do relationship_urls.key?(through.to_s) end define_method(name) do (name.to_s, type, through, singular, plural) end end |
.has_one(name, type, through: name, identifier: name) ⇒ Object
Define a has-one relationship with another resource
13 14 15 16 17 18 19 20 21 |
# File 'lib/taleo/resource.rb', line 13 def self.has_one(name, type, through: name, identifier: name) define_method(:"has_#{name}?") do relationship_urls.key?(through.to_s) end define_method(name) do (name.to_s, through, identifier, type) end end |
Instance Method Details
#related_resource(name, through, identifier, type) ⇒ Object
Retrieve a related resource’s data
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/taleo/resource.rb', line 35 def (name, through, identifier, type) unless self.send(:"has_#{name}?") raise Taleo::Error.new("No such relationship: #{name}") end res = client.connection.get do |req| req.url relationship_urls.fetch(through.to_s) end data = JSON.parse(res.body) type.new(data.dig('response', identifier.to_s), client) end |
#related_resources(name, type, through, singular, plural) ⇒ Object
Retrieve a set of related resources’ data
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/taleo/resource.rb', line 49 def (name, type, through, singular, plural) unless self.send(:"has_#{name}?") raise Taleo::Error.new("No such relationship: #{name}") end res = client.connection.get do |req| req.url relationship_urls.fetch(through.to_s) end data = JSON.parse(res.body) data.dig('response', plural.to_s).map do |item| type.new(item.fetch(singular.to_s), client) end end |
#relationship_urls ⇒ Object
Get URLs for related resources
65 66 67 |
# File 'lib/taleo/resource.rb', line 65 def relationship_urls data.fetch('relationshipUrls') end |