Module: Checkr

Defined in:
lib/checkr.rb,
lib/checkr/geo.rb,
lib/checkr/util.rb,
lib/checkr/report.rb,
lib/checkr/package.rb,
lib/checkr/program.rb,
lib/checkr/version.rb,
lib/checkr/api_list.rb,
lib/checkr/document.rb,
lib/checkr/api_class.rb,
lib/checkr/candidate.rb,
lib/checkr/screening.rb,
lib/checkr/ssn_trace.rb,
lib/checkr/invitation.rb,
lib/checkr/report_list.rb,
lib/checkr/api_resource.rb,
lib/checkr/subscription.rb,
lib/checkr/api_singleton.rb,
lib/checkr/document_list.rb,
lib/checkr/eviction_search.rb,
lib/checkr/errors/api_error.rb,
lib/checkr/errors/checkr_error.rb,
lib/checkr/sex_offender_search.rb,
lib/checkr/motor_vehicle_report.rb,
lib/checkr/county_criminal_search.rb,
lib/checkr/global_watchlist_search.rb,
lib/checkr/national_criminal_search.rb,
lib/checkr/errors/api_connection_error.rb,
lib/checkr/errors/authentication_error.rb,
lib/checkr/errors/invalid_request_error.rb

Defined Under Namespace

Modules: Util Classes: APIClass, APIConnectionError, APIError, APIList, APIResource, APISingleton, AuthenticationError, Candidate, CheckrError, CountyCriminalSearch, Document, DocumentList, EvictionSearch, Geo, GlobalWatchlistSearch, InvalidRequestError, Invitation, MotorVehicleReport, NationalCriminalSearch, Package, Program, Report, ReportList, SSNTrace, Screening, SexOffenderSearch, Subscription

Constant Summary collapse

VERSION =
'1.4.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



51
52
53
# File 'lib/checkr.rb', line 51

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



51
52
53
# File 'lib/checkr.rb', line 51

def api_key
  @api_key
end

.api_testObject

Returns the value of attribute api_test.



51
52
53
# File 'lib/checkr.rb', line 51

def api_test
  @api_test
end

Class Method Details

.api_url(path = '') ⇒ Object



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

def self.api_url(path='')
  "#{@api_base}#{path}"
end

.basic_auth_headers(api_key = @api_key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/checkr.rb', line 120

def self.basic_auth_headers(api_key=@api_key)
  api_key ||= @api_key
  unless api_key
    raise ArgumentError.new('No API key provided. Set your API key using "Checkr.api_key = <API-KEY>".')
  end

  base_64_key = Base64.encode64("#{api_key}:")
  {
    "Authorization" => "Basic #{base_64_key}",
  }
end

.connection_messageObject



215
216
217
218
219
# File 'lib/checkr.rb', line 215

def self.connection_message
  "Please check your internet connection and try again. " \
  "If this problem persists, you should check Checkr's service status at " \
  "https://twitter.com/checkrstatus, or let us know at [email protected]."
end

.default_headersObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/checkr.rb', line 105

def self.default_headers
  headers = {
    :user_agent => "Checkr/::API_VERSION:: RubyBindings/#{Checkr::VERSION}",
    :content_type => 'application/x-www-form-urlencoded'
  }

  begin
    headers.update(:x_checkr_client_user_agent => JSON.generate(user_agent))
  rescue => e
    headers.update(:x_checkr_client_raw_user_agent => user_agent.inspect,
                   :error => "#{e} (#{e.class})")
  end
  headers
end

.execute_request(opts) ⇒ Object

Mostly here for stubbing out during tests.



90
91
92
# File 'lib/checkr.rb', line 90

def self.execute_request(opts)
  RestClient::Request.execute(opts)
end

.get_unameObject



146
147
148
149
150
# File 'lib/checkr.rb', line 146

def self.get_uname
  `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM => ex # couldn't create subprocess
  "uname lookup failed"
end

.handle_api_error(rcode, rbody) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/checkr.rb', line 221

def self.handle_api_error(rcode, rbody)
  begin
    error_obj = JSON.parse(rbody)
  rescue JSON::ParserError
    raise APIError.generic(rcode, rbody)
  end
  error_obj = Util.symbolize_keys(error_obj)
  raise APIError.generic(rcode, rbody) unless error = error_obj[:error]

  case rcode
  when 400, 404
    raise InvalidRequestError.new(error, "", rcode, rbody, error_obj)
  when 401
    raise AuthenticationError.new(error, rcode, rbody, error_obj)
  else
    raise APIError.new(error, rcode, rbody, error_obj)
  end
end

.handle_connection_error(error, url) ⇒ Object

Raises:



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/checkr.rb', line 186

def self.handle_connection_error(error, url)
  message = "An error occurred while connecting to Checkr at #{url}."

  case error
  when RestClient::RequestTimeout
    message +=  connection_message

  when RestClient::ServerBrokeConnection
    message = "The connection to the server at (#{url}) broke before the " \
      "request completed. #{connection_message}"

  when RestClient::SSLCertificateNotVerified
    message = "Could not verify Checkr's SSL certificate. " \
      "Please make sure that your network is not intercepting certificates. " \
      "If this problem persists, let us know at [email protected]."

  when SocketError
    message = "Unexpected error when trying to connect to Checkr. " \
      "You may be seeing this message because your DNS is not working. " \
      "To check, try running 'host api.checkr.com' from the command line."

  else
    message = "Unexpected error communicating with Checkr. " \
      "If this problem persists, let us know at [email protected]. #{connection_message}"
  end

  raise APIConnectionError.new(message + "\n\n(Network error: #{error.message}")
end

.handle_request_error(error, url) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/checkr.rb', line 169

def self.handle_request_error(error, url)
  # First we see if this is an error with a response, and if it is
  # we check to see if there is an http code and body to work with.
  if error.is_a?(RestClient::ExceptionWithResponse)
    if error.http_code && error.http_body
      handle_api_error(error.http_code, error.http_body)
    end
  end

  # If we got here then the error hasn't been handled yet.
  # Handle it as a connection error.
  handle_connection_error(error, url)

  # Finally if we get here we don't know what type of error it is, so just raise it.
  raise error
end

.parse(response) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/checkr.rb', line 94

def self.parse(response)
  begin
    json = JSON.parse(response.body)
  rescue JSON::ParserError
    raise APIError.generic(response.code, response.body)
  end

  json = Util.symbolize_keys(json)
  json
end

.request(method, path, params = {}, headers = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/checkr.rb', line 58

def self.request(method, path, params={}, headers={})
  verify_api_key(api_key)

  url = api_url(path)

  request_opts = { :verify_ssl => false }

  if [:get, :head, :delete].include?(method.to_s.downcase.to_sym)
    unless params.empty?
      url += URI.parse(url).query ? '&' : '?' + Util.query_string(params)
    end
    params = nil
  end

  headers = default_headers.update(basic_auth_headers(api_key)).update(headers)
  request_opts.update(:headers => headers,
                      :method => method,
                      :open_timeout => 30,
                      :payload => params,
                      :url => url,
                      :timeout => 60)

  begin
    response = execute_request(request_opts)
  rescue Exception => e
    handle_request_error(e, url)
  end

  parse(response)
end

.user_agentObject



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/checkr.rb', line 132

def self.user_agent
  @uname ||= get_uname
  lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"

  {
    :bindings_version => Checkr::VERSION,
    :lang => 'ruby',
    :lang_version => lang_version,
    :platform => RUBY_PLATFORM,
    :publisher => 'checkr',
    :uname => @uname
  }
end

.verify_api_key(api_key) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/checkr.rb', line 152

def self.verify_api_key(api_key)
  unless api_key
    raise AuthenticationError.new('No API key provided. ' +
      'Set your API key using "Checkr.api_key = <API-KEY>". ' +
      'You can generate API keys from the Checkr web interface. ' +
      'See https://docs.checkr.com/#authentication for details, ' +
      'or email [email protected] if you have any questions.')
  end

  if api_key =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' +
      'whitespace. (HINT: You can double-check your API key from the ' +
      'Checkr web interface. See https://docs.checkr.com/#authentication for details, or ' +
      'email [email protected] if you have any questions.)')
  end
end