Class: Openapply::Client

Inherits:
Object
  • Object
show all
Includes:
Get, HTTParty, Put
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

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_keyObject

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_pathObject

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_recordsObject

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_timeoutObject

make OpenApply timeout visible



35
36
37
# File 'lib/openapply/client.rb', line 35

def api_timeout
  API_TIMEOUT
end

#api_urlObject

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

Note:

checks the info for validity & unpacks the json retubed to a JS formatt

Parameters:



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, options={})
  # 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, options)

  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

Note:

Does the actual api call to OpenApply & handles API timeouts gracefully

Parameters:



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, options={})
  # https://stackoverflow.com/questions/26251422/handling-netreadtimeout-error-in-httparty
  max_retries = 3
  times_retried = 0
  begin
    self.class.get(url, options)
  # 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