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/adverse_item.rb,
lib/checkr/api_resource.rb,
lib/checkr/subscription.rb,
lib/checkr/verification.rb,
lib/checkr/api_singleton.rb,
lib/checkr/document_list.rb,
lib/checkr/adverse_action.rb,
lib/checkr/eviction_search.rb,
lib/checkr/errors/api_error.rb,
lib/checkr/adverse_item_list.rb,
lib/checkr/verification_list.rb,
lib/checkr/errors/checkr_error.rb,
lib/checkr/sex_offender_search.rb,
lib/checkr/federal_civil_search.rb,
lib/checkr/motor_vehicle_report.rb,
lib/checkr/state_criminal_search.rb,
lib/checkr/county_criminal_search.rb,
lib/checkr/education_verification.rb,
lib/checkr/employment_verification.rb,
lib/checkr/federal_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, AdverseAction, AdverseItem, AdverseItemList, AuthenticationError, Candidate, CheckrError, CountyCriminalSearch, Document, DocumentList, EducationVerification, EmploymentVerification, EvictionSearch, FederalCivilSearch, FederalCriminalSearch, Geo, GlobalWatchlistSearch, InvalidRequestError, Invitation, MotorVehicleReport, NationalCriminalSearch, Package, Program, Report, ReportList, SSNTrace, Screening, SexOffenderSearch, StateCriminalSearch, Subscription, Verification, VerificationList

Constant Summary collapse

VERSION =
'1.8.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



61
62
63
# File 'lib/checkr.rb', line 61

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



61
62
63
# File 'lib/checkr.rb', line 61

def api_key
  @api_key
end

.api_testObject

Returns the value of attribute api_test.



61
62
63
# File 'lib/checkr.rb', line 61

def api_test
  @api_test
end

Class Method Details

.api_url(path = '') ⇒ Object



64
65
66
# File 'lib/checkr.rb', line 64

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

.basic_auth_headers(api_key = @api_key) ⇒ Object



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

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



229
230
231
232
233
# File 'lib/checkr.rb', line 229

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



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

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.



104
105
106
# File 'lib/checkr.rb', line 104

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

.get_unameObject



160
161
162
163
164
# File 'lib/checkr.rb', line 160

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/checkr.rb', line 235

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:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/checkr.rb', line 200

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/checkr.rb', line 183

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



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/checkr.rb', line 68

def self.request(method, path, params={}, headers={})
  api_key = params[:api_key] || self.api_key
  params.delete(:api_key)

  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 ? '&' : '?'
      url += 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



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/checkr.rb', line 146

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/checkr.rb', line 166

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