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
- #oa_api_call(url, options = {}) ⇒ Object
Methods included from Get
#many_ids_updated_time, #many_students_details, #many_students_details_by_ids, #many_students_ids, #many_students_summaries, #one_student_details_by_id, #one_student_payments_by_id, #one_student_record_by_id, #url_for_many_students_summaries
Instance Method Details
#api_key ⇒ Object
Defines & makes visible OpenApply secret access key with ENV-VARS
40 41 42 |
# File 'lib/openapply/client.rb', line 40 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
45 46 47 |
# File 'lib/openapply/client.rb', line 45 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)
51 52 53 |
# File 'lib/openapply/client.rb', line 51 def api_records ENV['OA_RECORD_COUNT'] || '50' end |
#api_timeout ⇒ Object
make OpenApply timeout visible
35 36 37 |
# File 'lib/openapply/client.rb', line 35 def api_timeout API_TIMEOUT end |
#api_url ⇒ Object
Makes OpenApply domain name visible:
30 31 32 |
# File 'lib/openapply/client.rb', line 30 def api_url API_URL end |
#oa_answer(url, options = {}) ⇒ Object
checks the info for validity & unpacks the json retubed to a JS formatt
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/openapply/client.rb', line 80 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openapply/client.rb', line 58 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 rescue Net::ReadTimeout, Net::OpenTimeout 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 |