Class: Util::VSAC::VSACAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/util/vsac_api.rb

Constant Summary collapse

DEFAULT_PROGRAM =

The default program to use for get_program_details and get_program_release_names calls. This can be overriden by providing a :program in the config or by the single optional parameter for those methods.

"CMS eCQM"
TICKET_SERVICE_PARAM =

This is the value of the service parameter passed when getting a ticket. This never changes.

"http://umlsks.nlm.nih.gov"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ VSACAPI

Creates a new VSACAPI. If credentials were provided they are checked now. If no credentials are provided then the API can still be used for utility methods.

Options for the API are passed in as a hash.

  • config -

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/util/vsac_api.rb', line 87

def initialize(options)
  # check that :config exists and has needed fields
  raise VSACArgumentError.new("Required param :config is missing or empty.") if options[:config].nil?
  @config = options[:config].symbolize_keys
  unless check_config @config
    raise VSACArgumentError.new("Required param :config is missing required URLs.")
  end

  # if a ticket_granting_ticket was passed in, check it and raise errors if found
  # VSAC API Key will be ignored
  if !options[:ticket_granting_ticket].nil?
    provided_ticket_granting_ticket = options[:ticket_granting_ticket]
    if provided_ticket_granting_ticket[:ticket].nil? || provided_ticket_granting_ticket[:expires].nil?
      raise VSACArgumentError.new("Optional param :ticket_granting_ticket is missing :ticket or :expires")
    end

    raise VSACTicketExpiredError.new if Time.now > provided_ticket_granting_ticket[:expires]

    @ticket_granting_ticket = { ticket: provided_ticket_granting_ticket[:ticket],
                                expires: provided_ticket_granting_ticket[:expires] }

    # if api key was provided use it to get a ticket granting ticket
  elsif !options[:api_key].nil?
    @ticket_granting_ticket = get_ticket_granting_ticket(options[:api_key])
  end
end

Instance Attribute Details

#ticket_granting_ticketObject (readonly)

The ticket granting that will be obtained if needed. Accessible so it may be stored in user session. Is a hash of the :ticket and time it :expires.



79
80
81
# File 'lib/util/vsac_api.rb', line 79

def ticket_granting_ticket
  @ticket_granting_ticket
end

Instance Method Details

#get_latest_profile_for_program(program = nil) ⇒ Object

Gets the latest profile for a program. This is a separate call from the program details call. It returns JSON with only the name of the latest profile and the timestamp of the request. ex:

{
  "name": "eCQM Update 2018-05-04",
  "requestTime": "2018-05-21 03:39:04 PM"
}

Optional parameter program is the program to request from the API. If it is not provided it will look for a :program in the config passed in during construction. If there is no :program in the config it will use the DEFAULT_PROGRAM constant for the program.

Returns the name of the latest profile for the given program.



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/util/vsac_api.rb', line 166

def get_latest_profile_for_program(program = nil)
  # if no program was provided use the one in the config or default in constant
  program = @config.fetch(:program, DEFAULT_PROGRAM) if program.nil?

  # parse json response and return it
  parsed_response = http_get_json("#{@config[:utility_url]}/program/#{ERB::Util.url_encode(program)}/latest%20profile")

  # As of 5/17/18 VSAC does not return 404 when an invalid profile is provided. It just doesnt fill the name
  # attribute in the 200 response. We need to check this.
  raise VSACProgramNotFoundError.new(program) if parsed_response['name'].nil?
  return parsed_response['name']
end

#get_multiple_valuesets(needed_value_sets) ⇒ Object

Get multiple valuesets (executed in parallel). Requires credentials.

Parameter needed_value_sets is an array of hashes, each hash should have at least: hash = _, value_set: {oid: _ }



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/util/vsac_api.rb', line 206

def get_multiple_valuesets(needed_value_sets)
  raise VSACNoCredentialsError.new unless @ticket_granting_ticket
  raise VSACTicketExpiredError.new if Time.now > @ticket_granting_ticket[:expires]

  vs_responses = get_multiple_valueset_raw_responses(needed_value_sets)
  vs_datas = [needed_value_sets,vs_responses].transpose.map do |needed_vs,vs_response|
    expected_oid = needed_vs[:value_set][:oid]
    process_and_validate_vsac_response(vs_response, expected_oid)
  end

  return vs_datas
end

#get_profile_namesObject

Gets the list of profiles. This may be used without credentials.

Returns a list of profile names. These are kept in the order that VSAC provides them in.



118
119
120
121
122
123
124
125
# File 'lib/util/vsac_api.rb', line 118

def get_profile_names
  profiles_response = http_get("#{@config[:utility_url]}/profiles")

  # parse xml response and get text content of each profile element
  doc = Nokogiri::XML(profiles_response)
  profile_list = doc.at_xpath("/ProfileList")
  return profile_list.xpath("//profile").map(&:text)
end

#get_program_details(program = nil) ⇒ Object

Gets the details for a program. This may be used without credentials.

Optional parameter program is the program to request from the API. If it is not provided it will look for a :program in the config passed in during construction. If there is no :program in the config it will use the DEFAULT_PROGRAM constant for the program.

Returns the JSON parsed response for program details.



145
146
147
148
149
150
151
# File 'lib/util/vsac_api.rb', line 145

def get_program_details(program = nil)
  # if no program was provided use the one in the config or default in constant
  program = @config.fetch(:program, DEFAULT_PROGRAM) if program.nil?
  return http_get_json("#{@config[:utility_url]}/program/#{ERB::Util.url_encode(program)}")
rescue VSACNotFoundError
  raise VSACProgramNotFoundError.new(program)
end

#get_program_namesObject

Gets the list of programs. This may be used without credentials.

Returns a list of program names. These are kept in the order that VSAC provides them in.



131
132
133
134
135
# File 'lib/util/vsac_api.rb', line 131

def get_program_names
  programs_response = http_get_json("#{@config[:utility_url]}/programs")
  programs_info = programs_response['Program']
  return programs_info.map { |program| program['name'] }
end

#get_program_release_names(program = nil) ⇒ Object

Gets the releases for a program. This may be used without credentials.

Optional parameter program is the program to request from the API. If it is not provided it will look for a :program in the config passed in during construction. If there is no :program in the config it will use the DEFAULT_PROGRAM constant for the program.

Returns a list of release names in a program. These are kept in the order that VSAC provides them in.



187
188
189
190
# File 'lib/util/vsac_api.rb', line 187

def get_program_release_names(program = nil)
  program_details = get_program_details(program)
  return program_details['release'].map { |release| release['name'] }
end

#get_valueset(oid, options = {}) ⇒ Object

Gets a valueset. This requires credentials.



195
196
197
198
# File 'lib/util/vsac_api.rb', line 195

def get_valueset(oid, options = {})
  needed_vs = {value_set: {oid: oid}, vs_vsac_options: options}
  return get_multiple_valuesets([needed_vs])[0]
end