Class: Openapply::Client
- Inherits:
-
Object
- Object
- Openapply::Client
- Defined in:
- lib/openapply/client.rb
Overview
OpenApply CLIENT is a service to manage admissions - this gem allows access to OpenApply API calls via HTTParty
Constant Summary collapse
- API_URL =
Defines OpenApply domain name from ENV-VARS
(ENV['OA_BASE_URI'] || 'demo.openapply.com')
- API_TIMEOUT =
Defines the OpenApply path from ENV-VARS - default is 5 seconds
(ENV['OA_TIMEOUT'].to_i || 5)
Instance Method Summary collapse
-
#api_key ⇒ Object
Defines & makes visible OpenApply secret access key with ENV-VARS.
-
#api_path ⇒ Object
Defines and makes visib le the OpenApply path with ENV-VARS.
-
#api_records ⇒ Object
Defines and makes visible the maximum records OpenApply should return (code default is 50 - OA default is 10 - doc says 100).
-
#api_timeout ⇒ Object
make OpenApply timeout visible.
-
#api_url ⇒ Object
Makes OpenApply domain name visible:.
-
#oa_answer(url, options = {}) ⇒ Object
Does checks the info for validity & unpacks the json retubed to a JS formatt.
-
#oa_api_call(url, options = {}) ⇒ Object
Does the actual api call to OpenApply & handles API timeouts gracefully.
Methods included from Convert
#check_header_keys_validity, #create_headers, #info_count, #known_transfer_object?, #process_key_info?, #send_data_to_remote_server, #students_array_to_csv, #students_array_to_xlsx, #students_as_array_by_status, #students_as_csv_by_status, #students_as_xlsx_by_status, #students_hash_to_array
Methods included from Get
#check_details_keys_validity, #clean_data, #flatten_array_vals, #flatten_hash_vals, #flatten_key_vals, #flatten_record, #payments_by_id, #student_by_id, #student_details_by_id, #student_ids_by_status, #students_by_status, #students_details_by_status, #students_query, #students_query_url
Instance Method Details
#api_key ⇒ Object
Defines & makes visible OpenApply secret access key with ENV-VARS
47 48 49 |
# File 'lib/openapply/client.rb', line 47 def api_key ENV['OA_AUTH_TOKEN'] || 'demo_site_api_key' end |
#api_path ⇒ Object
Defines and makes visib le the OpenApply path with ENV-VARS
52 53 54 |
# File 'lib/openapply/client.rb', line 52 def api_path ENV['OA_API_PATH'] || "/api/v1/students/" end |
#api_records ⇒ Object
Defines and makes visible the maximum records OpenApply should return (code default is 50 - OA default is 10 - doc says 100)
58 59 60 |
# File 'lib/openapply/client.rb', line 58 def api_records ENV['OA_REPLY_RECORDS'] || '50' end |
#api_timeout ⇒ Object
make OpenApply timeout visible
42 43 44 |
# File 'lib/openapply/client.rb', line 42 def api_timeout API_TIMEOUT end |
#api_url ⇒ Object
Makes OpenApply domain name visible:
37 38 39 |
# File 'lib/openapply/client.rb', line 37 def api_url API_URL end |
#oa_answer(url, options = {}) ⇒ Object
Does checks the info for validity & unpacks the json retubed to a JS formatt
Attributes
-
url- this is the url to do the call
/api/v1/students/95?auth_token=demo_site_api_key
is the url passed when wanting to do the following cli api call
curl http://demo.openapply.com/api/v1/students/95?auth_token=demo_site_api_key
-
options- see httparty options [www.rubydoc.info/github/jnunemaker/httparty]
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openapply/client.rb', line 96 def oa_answer(url, ={}) # puts # puts "GIVEN URL: #{ url.inspect }" return { error: 'no url given' } if url.nil? or url.to_s.eql? "" return { error: 'bad url - has space' } if url.include? " " return { error: 'bad api_path' } unless url.include? "#{api_path}" return { error: 'bad auth_token' } unless url.include? "auth_token=#{api_key}" api_answer = nil api_answer = oa_api_call(url, ) return api_answer unless api_answer.respond_to? "response" # and not api_answer[:error].nil? # return { error: 'no response' } unless api_answer.respond_to? "response" return { error: 'no response' } if api_answer.response.nil? return { error: 'no response' } if api_answer.response.to_s.eql? "" return JSON.parse(api_answer.response.body, symbolize_names: true) end |
#oa_api_call(url, options = {}) ⇒ Object
Does the actual api call to OpenApply & handles API timeouts gracefully
Attributes
-
url- this is the url to do the call
/api/v1/students/95?auth_token=demo_site_api_key
is the url passed when wanting to do the following cli api call
curl http://demo.openapply.com/api/v1/students/95?auth_token=demo_site_api_key
-
options- see httparty options [www.rubydoc.info/github/jnunemaker/httparty]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/openapply/client.rb', line 70 def oa_api_call(url, ={}) # https://stackoverflow.com/questions/26251422/handling-netreadtimeout-error-in-httparty max_retries = 3 times_retried = 0 begin self.class.get(url, ) rescue Net::ReadTimeout, Net::OpenTimeout => error if times_retried < max_retries times_retried += 1 # puts "TIMEOUT RETRY: #{times_retried} of #{max_retries} - USING: #{url.inspect}" retry else # puts "TIME-OUT URI FAILED: #{url.inspect}" { error: "no response (timeout) from URL: #{url}" } end end end |