Class: PeopleGroup::Connectors::Workday::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/peoplegroup/connectors/workday/report.rb

Overview

Report

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, password: nil) ⇒ Report

Create a new report instance

Parameters:

  • url (String)

    The report URL without any parameters.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/peoplegroup/connectors/workday/report.rb', line 25

def initialize(url:, password: nil)
  @uri = URI.parse(url)

  # Ensure URI is properly read
  raise ReportError, "Error with report URL: #{url}. Is this a valid URL?" unless @uri.scheme && @uri.host && @uri.path

  @uri.query = nil # clear any query params

  # Use provided password or search in env.
  pass = password || ENV.fetch('WORKDAY_ISU_PASSWORD', nil)

  raise ReportError, "No username provided for report: #{url}" if username.nil?
  raise ReportError, "No password provided for report: #{url}" if pass.nil?

  # Encode credentials
  auth = Base64.strict_encode64("#{username}:#{pass}")

  # Base64 Encode
  @auth_header = { Authorization: "Basic  #{auth}" }
rescue URI::InvalidURIError
  raise ReportError, "Invalid URI detected for report: '#{url}'."
end

Instance Attribute Details

#auth_headerObject (readonly)

The authorization header used for the report.



17
18
19
# File 'lib/peoplegroup/connectors/workday/report.rb', line 17

def auth_header
  @auth_header
end

#uriObject (readonly)

The parsed URI of the report URL



20
21
22
# File 'lib/peoplegroup/connectors/workday/report.rb', line 20

def uri
  @uri
end

Class Method Details

.call(url:, password: nil, prompts: {}) ⇒ Array|Hash

Returns the JSON report response.

Parameters:

  • url (String)

    The report URL without any parameters

  • prompts (Hash) (defaults to: {})

    Report input prompts/parameters.

Returns:

  • (Array|Hash)

    the JSON report response.

See Also:



52
53
54
# File 'lib/peoplegroup/connectors/workday/report.rb', line 52

def self.call(url:, password: nil, prompts: {})
  new(url: url, password: password)&.run(prompts)
end

Instance Method Details

#inspectObject

Inspect the Workday Report instance



90
91
92
# File 'lib/peoplegroup/connectors/workday/report.rb', line 90

def inspect
  "#<#{self.class.name}:#{object_id} @uri='#{@uri}' @auth_header=#{@auth_header ? 'HIDDEN' : auth_header} @username=#{username}>"
end

#report_url(prompts = {}) ⇒ String

Combine the url and extra input parameters in an encoded format.

Parameters:

  • prompts (Hash) (defaults to: {})

    The report prompts/parameters.

Returns:

  • (String)

    The @url (report url) combined with report format ‘?format=’json’‘ and any extra parameters.



82
83
84
85
86
87
# File 'lib/peoplegroup/connectors/workday/report.rb', line 82

def report_url(prompts = {})
  uri = @uri.clone # copy the URI object
  params = prompts.merge!({ format: :json }) # ensure report format is always JSON
  uri.query = URI.encode_www_form(params)
  uri.to_s
end

#run(prompts = {}) ⇒ Array|Hash

Run the report with the given prompts

Parameters:

  • prompts (Hash) (defaults to: {})

    The report key value pair of parameters or ‘prompts’ (as referred to in Workday).

Returns:

  • (Array|Hash)

    the JSON report response.

Raises:

  • (StandardError)

    if the report returns with any status other than 200.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/peoplegroup/connectors/workday/report.rb', line 60

def run(prompts = {})
  url = report_url(prompts)
  RestClient.get(url, @auth_header) do |response, _req, _result|
    # Raise a report error if we ran into any issues accessing the report
    raise ReportError, inspect, response unless response&.code == 200

    # parse the response body into JSON
    json = JSON.parse(response.body)

    # if return content of 'Report_Entry' or the whole set if key does not exist.
    json['Report_Entry'] || json
  end
end

#usernameObject

Extract username from URI path (needed for auth)



75
76
77
# File 'lib/peoplegroup/connectors/workday/report.rb', line 75

def username
  @username ||= @uri.path.split('/')[-2]
end