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
# File 'lib/openapply/client.rb', line 22

def initialize
  api_url     = ENV['OA_BASE_URI']
  api_key     = ENV['OA_AUTH_TOKEN']

  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



38
39
40
# File 'lib/openapply/client.rb', line 38

def api_key
  ENV['OA_AUTH_TOKEN']
end

#api_pathObject



42
43
44
# File 'lib/openapply/client.rb', line 42

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

#api_recordsObject



46
47
48
# File 'lib/openapply/client.rb', line 46

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

#api_timeoutObject



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

def api_timeout
  ENV['OA_TIMEOUT'].to_i
end

#api_urlObject



30
31
32
# File 'lib/openapply/client.rb', line 30

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:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openapply/client.rb', line 72

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:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openapply/client.rb', line 53

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