Class: Openapply::Client

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

#check_header_keys_validity, #create_headers, #info_count, #known_transfer_object?, #process_key_info?, #send_data_to_remote_server, #students_array_to_csv, #students_as_array_by_status, #students_as_csv_by_status, #students_hash_to_array

Methods included from Get

#check_details_keys_validity, #clean_data, #flatten_array_vals, #flatten_hash_vals, #flatten_key_vals, #flatten_record, #payments_by_id, #student_by_id, #student_details_by_id, #student_ids_by_status, #students_by_status, #students_details_by_status, #students_query, #students_query_url

Instance Method Details

#api_keyObject

Defines & makes visible OpenApply secret access key with ENV-VARS



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

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



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

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)



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

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

#api_timeoutObject

make OpenApply timeout visible



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

def api_timeout
  API_TIMEOUT
end

#api_urlObject

Makes OpenApply domain name visible:



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

def api_url
  API_URL
end

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

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

Attributes

  • url - this is the url to do the call

/api/v1/students/95?auth_token=demo_site_api_key

is the url passed when wanting to do the following cli api call

curl http://demo.openapply.com/api/v1/students/95?auth_token=demo_site_api_key


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

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

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

Attributes

  • url - this is the url to do the call

/api/v1/students/95?auth_token=demo_site_api_key

is the url passed when wanting to do the following cli api call

curl http://demo.openapply.com/api/v1/students/95?auth_token=demo_site_api_key


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

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
    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