Class: Openapply::Client

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

Constant Summary collapse

API_TIMEOUT =
ENV['OA_TIMEOUT'].to_i == 0 ? 5 : ENV['OA_TIMEOUT'].to_i

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

Methods included from Put

#update_student_id

Constructor Details

#initialize(url: nil, client_id: nil, client_secret: nil, token: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • (defaults to: nil)

    Base uri

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

    if present will not generate an auth token

Raises:



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

def initialize(url: nil, client_id: nil, client_secret: nil, token: nil)
  @api_url     = format_api_url(url || ENV['OA_BASE_URI'])
  @api_client_id     = client_id || ENV['OA_CLIENT_ID']
  @api_client_secret     = client_secret || ENV['OA_CLIENT_SECRET']
  @api_key             = token

  raise ArgumentError, 'OA_BASE_URI is missing'   if api_url.nil? or
                                                      api_url.empty?
  raise ArgumentError, 'OA_CLIENT_ID is missing' if api_client_id.nil? or
                                                      api_client_id.empty?
  raise ArgumentError, 'OA_CLIENT_SECRET is missing' if api_client_secret.nil? or
                                                      api_client_secret.empty?

  self.class.base_uri api_url
end

Instance Method Details

#api_client_idObject



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

def api_client_id
  @api_client_id
end

#api_client_secretObject



50
51
52
# File 'lib/openapply/client.rb', line 50

def api_client_secret
  @api_client_secret
end

#api_keyObject



54
55
56
# File 'lib/openapply/client.rb', line 54

def api_key
  @api_key || authentificate.token
end

#api_pathObject



58
59
60
# File 'lib/openapply/client.rb', line 58

def api_path
  "/api/v3"
end

#api_recordsObject



62
63
64
# File 'lib/openapply/client.rb', line 62

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

#api_timeoutObject



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

def api_timeout
  API_TIMEOUT
end

#api_urlObject



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

def api_url
  @api_url
end

#authentificateOAuth2::AccessToken

Note:

authentificate using oauth2

Returns:



137
138
139
140
# File 'lib/openapply/client.rb', line 137

def authentificate
  client = OAuth2::Client.new(api_client_id, api_client_secret, site: api_url)
  return client.client_credentials.get_token
end

#get(url, options = {}) ⇒ Object Also known as: oa_api_call

Note:

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

Parameters:



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

def get(url, options={})
  max_retries = 3
  times_retried = 0
  begin
    options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', "Authorization" => auth_token}
    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

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

Note:

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

Note:

by passing in a value such as student_id or status this will automatically trigger the change form get to put

Parameters:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/openapply/client.rb', line 119

def oa_answer(url, value={}, 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}"
  if value.empty?
    api_answer = send(:get, url, options) if value.empty?
  else
    api_answer = send(:put, url, value, options) 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

Parameters:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openapply/client.rb', line 91

def put(url, value, options={})
  # 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
    options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', "Authorization" => auth_token}
    options[:body] = value
    self.class.put(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