Class: Taleo::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/taleo/resource.rb

Direct Known Subclasses

Activity, Attachment, Candidate, Employee, Location, Packet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/taleo/resource.rb', line 5

def client
  @client
end

#dataObject (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
    related_resources(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
    related_resource(name.to_s, through, identifier, type)
  end
end

Instance Method Details

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 related_resource(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

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 related_resources(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_urlsObject

Get URLs for related resources



65
66
67
# File 'lib/taleo/resource.rb', line 65

def relationship_urls
  data.fetch('relationshipUrls')
end