Class: Openapply::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, GetManyStudents, GetOneStudent, Put
Defined in:
lib/openapply/client.rb

Constant Summary collapse

API_URL =
ENV['OA_BASE_URI']
API_TIMEOUT =
ENV['OA_TIMEOUT'].to_i || 5

Instance Method Summary collapse

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

Constructor Details

#initializeClient

attr_reader :api_url, :api_key

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openapply/client.rb', line 22

def initialize
  api_url     = ENV['OA_BASE_URI']
  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_keyObject



44
45
46
# File 'lib/openapply/client.rb', line 44

def api_key
  ENV['OA_AUTH_TOKEN']
end

#api_pathObject



48
49
50
# File 'lib/openapply/client.rb', line 48

def api_path
  ENV['OA_API_PATH']     || "/api/v1/students/"
end

#api_recordsObject



52
53
54
# File 'lib/openapply/client.rb', line 52

def api_records
  ENV['OA_RECORD_COUNT'] || '50'
end

#api_timeoutObject



40
41
42
# File 'lib/openapply/client.rb', line 40

def api_timeout
  ENV['OA_TIMEOUT'].to_i
end

#api_urlObject



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

def api_url
  ENV['OA_BASE_URI']
end

#oa_answer(url, options = {}) ⇒ Object

Note:

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

Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openapply/client.rb', line 78

def oa_answer(url, options={})
  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 = oa_api_call(url, options)

  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

#oa_api_call(url, options = {}) ⇒ Object

Note:

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

Parameters:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openapply/client.rb', line 59

def oa_api_call(url, options={})
  # add exception if ENV are not set
  max_retries = 3
  times_retried = 0
  begin
    self.class.get(url, options)
  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