Class: OracleHcm::Resource

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

Overview

Base class for all resources in Oracle HCM. Provides some sugar syntax and shared methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, client, parent = nil) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
# File 'lib/oracle_hcm/resource.rb', line 7

def initialize(data, client, parent = nil)
  @data = data
  @client = client
  @parent = parent
  @cache = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.cached_property(name, &block) ⇒ Object

Sugar syntax for defining properties that are cached to avoid making multiple requests for data that is not expected to change during execution, such as employee names or SSNs. Also defines a bang method that will skip and overwrite the cache.



33
34
35
36
37
38
39
40
41
# File 'lib/oracle_hcm/resource.rb', line 33

def self.cached_property(name, &block)
  bang = :"#{name.to_s}!"
  define_method(bang) {
    instance_variable_get(:@cache)[name] = instance_eval(&block)
  }
  define_method(name) {
    @cache[name] || self.send(bang)
  }
end

.child_resource(method, resource:) ⇒ Object

Sugar syntax for defining child resource retrieval methods



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/oracle_hcm/resource.rb', line 57

def self.child_resource(method, resource:)
  define_method(method) do |offset: 0, limit: 20, q: []|
    # Generate the URL for the request
    url = "#{resource_url}/child/#{resource.resource_name}"
    if !q.is_a?(Array)
      q = [q]
    end
    res = client.connection.get do |req|
      req.url url
      req.params["offset"] = offset
      req.params["limit"] = limit
      if q.any?
        req.params["q"] = q.join(";")
      end
    end
    if res.success?
      ResourceList.new(JSON.parse(res.body), offset, limit, method, resource, client, self)
    else
      nil
    end
  end
end

.property(name, key: nil, &block) ⇒ Object

Sugar syntax for defining methods that return data at given keys in the resource JSON. Properties are not a 1:1 match with data in the object JSON. They can be wrappers for sending requests to related resources such as names or addresses, although those are better defined as cached properties.



19
20
21
22
23
24
25
26
27
# File 'lib/oracle_hcm/resource.rb', line 19

def self.property(name, key: nil, &block)
  define_method(name) {
    if block_given?
      yield
    else
      data.fetch(key)
    end
  }
end

.resource_nameObject



80
81
82
83
# File 'lib/oracle_hcm/resource.rb', line 80

def self.resource_name
  name = self.to_s.split("::").last
  "#{name[0].downcase}#{name[1..-1]}s"
end

Instance Method Details

#canonical_idObject

Although resources have unique identifiers, the API does not use these in the URL to retrieve some resources. Instead, a longer identifier string is used. This identifier is only displayed as part of the self link for the resource.



100
101
102
103
104
# File 'lib/oracle_hcm/resource.rb', line 100

def canonical_id
  # Extract resource identifier from self link
  m = link.fetch("href").match(/\/([0-9A-F]+)\z/)
  m[1]
end

Retrieve a link by its relationship. By default, returns the self link.



90
91
92
93
94
# File 'lib/oracle_hcm/resource.rb', line 90

def link(rel: "self")
  data.fetch("links").select { |link|
    link.fetch("rel") == rel
  }.first
end

#resource_nameObject



85
86
87
# File 'lib/oracle_hcm/resource.rb', line 85

def resource_name
  self.class.resource_name
end

#resource_urlObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oracle_hcm/resource.rb', line 43

def resource_url
  parts = [
    resource_name,
    canonical_id
  ]

  if !parent.nil?
    parts = [parent.resource_url, 'child'].concat(parts)
  end

  parts.reject(&:nil?).join("/")
end

#uri(rel: "self") ⇒ Object



106
107
108
# File 'lib/oracle_hcm/resource.rb', line 106

def uri(rel: "self")
  link(rel: rel).fetch("href").gsub(client.base_url, "")
end