Class: Openapply::Client
- Inherits:
-
Object
- Object
- Openapply::Client
- Includes:
- HTTParty, GetManyStudents, GetOneStudent, Put
- Defined in:
- lib/openapply/client.rb
Constant Summary collapse
- API_TIMEOUT =
ENV['OA_TIMEOUT'].to_i || 5
- API_URL =
Force RBENV var base_uri to https://
case when ENV['OA_BASE_URI'].start_with?('https://') "#{ENV['OA_BASE_URI']}" when ENV['OA_BASE_URI'].start_with?('http://') "#{ENV['OA_BASE_URI']}".gsub("http", "https") else "https://#{ENV['OA_BASE_URI']}" end
Instance Method Summary collapse
- #api_key ⇒ Object
- #api_path ⇒ Object
- #api_records ⇒ Object
- #api_timeout ⇒ Object
- #api_url ⇒ Object
- #get(url, options = {}) ⇒ Object (also: #oa_api_call)
-
#initialize ⇒ Client
constructor
attr_reader :api_url, :api_key.
- #oa_answer(url, value = {}, options = {}) ⇒ Object
- #put(url, value, options = {}) ⇒ Object
Methods included from GetManyStudents
#many_ids_updated_time, #many_students_details, #many_students_details_by_ids, #many_students_ids, #many_students_summaries, #url_for_many_students_summaries
Methods included from GetOneStudent
#one_student_details_by_id, #one_student_payments_by_id, #one_student_record_by_id
Methods included from Put
#update_student_id, #update_student_status
Constructor Details
#initialize ⇒ Client
attr_reader :api_url, :api_key
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/openapply/client.rb', line 31 def initialize api_url = API_URL api_key = ENV['OA_AUTH_TOKEN'] raise ArgumentError, 'OA_TIMEOUT is missing' if api_timeout.nil? or not api_timeout.is_a? Integer raise ArgumentError, 'OA_API_PATH is missing' if api_path.nil? or api_path.empty? raise ArgumentError, 'OA_BASE_URI is missing' if api_url.nil? or api_url.empty? raise ArgumentError, 'OA_AUTH_TOKEN is missing' if api_key.nil? or api_key.empty? end |
Instance Method Details
#api_key ⇒ Object
53 54 55 |
# File 'lib/openapply/client.rb', line 53 def api_key ENV['OA_AUTH_TOKEN'] end |
#api_path ⇒ Object
57 58 59 |
# File 'lib/openapply/client.rb', line 57 def api_path ENV['OA_API_PATH'] || "/api/v1/students/" end |
#api_records ⇒ Object
61 62 63 |
# File 'lib/openapply/client.rb', line 61 def api_records ENV['OA_RECORD_COUNT'] || '50' end |
#api_timeout ⇒ Object
49 50 51 |
# File 'lib/openapply/client.rb', line 49 def api_timeout ENV['OA_TIMEOUT'].to_i end |
#api_url ⇒ Object
45 46 47 |
# File 'lib/openapply/client.rb', line 45 def api_url API_URL end |
#get(url, options = {}) ⇒ Object Also known as: oa_api_call
Note:
Does the actual api call(get) to OpenApply & handles API timeouts gracefully
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/openapply/client.rb', line 69 def get(url, ={}) # add exception if ENV are not set max_retries = 3 times_retried = 0 begin self.class.get(url, ) rescue Net::ReadTimeout, Net::OpenTimeout if times_retried < max_retries times_retried += 1 retry else { error: "no response (timeout) from URL: #{url}" } end end end |
#oa_answer(url, value = {}, options = {}) ⇒ Object
Note:
checks the info for validity & unpacks the json retubed to a JS formatt
Note:
by passing in a value such as student_id or status this will automatically trigger the change form get to put
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/openapply/client.rb', line 123 def oa_answer(url, value={}, ={}) 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}" if value.empty? return { error: 'bad auth_token' } unless url&.include? "auth_token=#{api_key}" api_answer = send(:get, url, ) if value.empty? else api_answer = send(:put, url, value, ) unless value.empty? end return api_answer 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 |
#put(url, value, options = {}) ⇒ Object
Note:
Does the actual api call(put) to OpenApply & handles API timeouts gracefully
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/openapply/client.rb', line 90 def put(url, value, ={}) # add exception if ENV are not set # Per emai from Mackenzine on API call formatting, vs what is found on API site # curl -X "PUT" "https://las.openapply.com/api/v1/students/OAID" \ # -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \ # --data-urlencode "student_id=YOURID" \ # --data-urlencode "auth_token=YOURTOKEN" max_retries = 3 times_retried = 0 begin query = {auth_token: api_key}.merge(value) header = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' } self.class.put(url, query: query, headers: header ) # self.class.put(url, # headers: header ) rescue Net::ReadTimeout, Net::OpenTimeout if times_retried < max_retries times_retried += 1 retry else { error: "no response (timeout) from URL: #{url}" } end end end |