Class: Taleo::Client

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

Overview

Base object for executing queries and wrapping HTTP requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(org_code, username, password, service_url) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/taleo/client.rb', line 14

def initialize(org_code, username, password, service_url)
  @org_code = org_code
  @username = username
  @password = password
  @service_url = service_url
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



12
13
14
# File 'lib/taleo/client.rb', line 12

def auth_token
  @auth_token
end

#org_codeObject (readonly)

Returns the value of attribute org_code.



12
13
14
# File 'lib/taleo/client.rb', line 12

def org_code
  @org_code
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/taleo/client.rb', line 12

def password
  @password
end

#service_urlObject (readonly)

Returns the value of attribute service_url.



12
13
14
# File 'lib/taleo/client.rb', line 12

def service_url
  @service_url
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/taleo/client.rb', line 12

def username
  @username
end

Instance Method Details

#activity(id) ⇒ Object



33
34
35
# File 'lib/taleo/client.rb', line 33

def activity(id)
  Activity.new(show('activity', id), self)
end

#api_urlObject



92
93
94
95
96
# File 'lib/taleo/client.rb', line 92

def api_url
  return @api_url if !@api_url.nil?

  api_url!
end

#api_url!Object



117
118
119
120
121
122
123
124
125
# File 'lib/taleo/client.rb', line 117

def api_url!
  conn = Faraday.new(url: "#{service_url}/#{org_code}") do |c|
    c.use Faraday::Response::RaiseError
  end
  res = conn.get
  data = JSON.parse(res.body)
  @api_url = data.dig('response', 'URL')
  @api_url
end

#candidate(id) ⇒ Object



29
30
31
# File 'lib/taleo/client.rb', line 29

def candidate(id)
  Candidate.new(show('candidate', id), self)
end

#connectionObject



98
99
100
101
102
103
# File 'lib/taleo/client.rb', line 98

def connection
  Faraday.new(url: api_url) do |conn|
    conn.use Faraday::Response::RaiseError
    conn.headers['Cookie'] = "authToken=#{auth_token}"
  end
end

#cursor(resource, klass, start = 1, limit = 10, params: {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/taleo/client.rb', line 43

def cursor(resource, klass, start = 1, limit = 10, params: {})
  res = connection.get do |req|
    req.url "object/#{resource}/search"
    req.params.merge!(params).merge!({
      'start' => start,
      'limit' => limit
    })
  end
  data = JSON.parse(res.body)
  pagination = data.dig('response', 'pagination')
  results = data.dig('response', 'searchResults')
  cursor = Cursor.new(pagination, results, resource, klass, self)
  cursor.self_page
end

#download(url) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/taleo/client.rb', line 105

def download(url)
  io = StringIO.new
  connection.get do |req|
    req.url url
    req.options.on_data = Proc.new do |chunk|
      io << chunk
    end
  end
  io.rewind
  io
end

#employee(id) ⇒ Object



21
22
23
# File 'lib/taleo/client.rb', line 21

def employee(id)
  Employee.new(show('employee', id), self)
end

#employees(start = 1, limit = 10, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
# File 'lib/taleo/client.rb', line 37

def employees(start = 1, limit = 10, params: {})
  raise ArgumentError, "At least one query parameter is required " \
                       "for employee search" if params.keys.size.zero?
  cursor('employee', Employee, start, limit, params: params)
end

#loginObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/taleo/client.rb', line 66

def 
  params = {
    orgCode: org_code,
    userName: username,
    password: password
  }

  conn = Faraday.new(url: api_url) do |c|
    c.use Faraday::Response::RaiseError
  end

  res = conn.post do |req|
    req.url 'login'
    req.params.merge!(params)
  end

  data = JSON.parse(res.body)
  @auth_token = data.dig('response', 'authToken')
end

#logoutObject



86
87
88
89
90
# File 'lib/taleo/client.rb', line 86

def logout
  connection.post do |req|
    req.url 'logout'
  end
end

#packet(id) ⇒ Object



25
26
27
# File 'lib/taleo/client.rb', line 25

def packet(id)
  Packet.new(show('packet', id), self)
end

#show(resource, id) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/taleo/client.rb', line 58

def show(resource, id)
  res = connection.get do |req|
    req.url "object/#{resource}/#{id}"
  end
  data = JSON.parse(res.body)
  data.dig('response', resource)
end