fullcontact-api-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +53 -0
- data/.rspec +3 -0
- data/.travis.yml +18 -0
- data/Gemfile +6 -0
- data/LICENSE.md +21 -0
- data/README.md +153 -0
- data/Rakefile +25 -0
- data/fullcontact.gemspec +34 -0
- data/lib/faraday/request/gateway.rb +18 -0
- data/lib/faraday/response/add_headers.rb +8 -0
- data/lib/faraday/response/fullcontact_errors.rb +36 -0
- data/lib/faraday/response/rubyize.rb +14 -0
- data/lib/fullcontact/api.rb +21 -0
- data/lib/fullcontact/client/company.rb +16 -0
- data/lib/fullcontact/client/person.rb +20 -0
- data/lib/fullcontact/client.rb +16 -0
- data/lib/fullcontact/configuration.rb +94 -0
- data/lib/fullcontact/connection.rb +39 -0
- data/lib/fullcontact/error.rb +47 -0
- data/lib/fullcontact/ext/hash/to_snake_keys.rb +41 -0
- data/lib/fullcontact/request.rb +28 -0
- data/lib/fullcontact/version.rb +3 -0
- data/lib/fullcontact.rb +29 -0
- data/spec/faraday/response_spec.rb +57 -0
- data/spec/fixtures/company.json +191 -0
- data/spec/fixtures/person.json +48 -0
- data/spec/fullcontact_spec.rb +145 -0
- data/spec/helper.rb +23 -0
- data/spec/ruby_fullcontact/api_spec.rb +68 -0
- data/spec/ruby_fullcontact/client/company_spec.rb +37 -0
- data/spec/ruby_fullcontact/client/person_spec.rb +93 -0
- data/spec/ruby_fullcontact/client_spec.rb +10 -0
- metadata +245 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d09d3cb3e713202cecef58ccc41d7eb20d24d44bdb3003d0f39d94ac9702ef77
|
4
|
+
data.tar.gz: d772eff4a0feb59dd6d91adfa129379e13684cedecdd0fc79c51608b13aef909
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ff2ffdaec6ba000e42c0eb25f15a55d415c4d0ece8ba36a665dcb2ae2ab1c45c6f149a5a49a3760f2c2c95c1ca82c5d2419d076eddc74377c0150b38e61864e
|
7
|
+
data.tar.gz: ed657af10f2aedddc0ed806efc1a909eaea8025ea3d23587d83bd2b42d0cbfbd03404e15a74444585d0a73fa73bdd6ef59495a2285f43730e16229543580b79e
|
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
+
#
|
19
|
+
# * Create a file at ~/.gitignore
|
20
|
+
# * Include files you want ignored
|
21
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
+
#
|
23
|
+
# After doing this, these files will be ignored in all your git projects,
|
24
|
+
# saving you from having to 'pollute' every project you touch with them
|
25
|
+
#
|
26
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
+
#
|
28
|
+
# For MacOS:
|
29
|
+
#
|
30
|
+
#.DS_Store
|
31
|
+
|
32
|
+
# For TextMate
|
33
|
+
#*.tmproj
|
34
|
+
#tmtags
|
35
|
+
|
36
|
+
# For emacs:
|
37
|
+
#*~
|
38
|
+
#\#*
|
39
|
+
#.\#*
|
40
|
+
|
41
|
+
# For vim:
|
42
|
+
#*.swp
|
43
|
+
|
44
|
+
# For redcar:
|
45
|
+
#.redcar
|
46
|
+
|
47
|
+
# For rubinius:
|
48
|
+
#*.rbc
|
49
|
+
|
50
|
+
.idea/
|
51
|
+
*.lock
|
52
|
+
|
53
|
+
*.gem
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 FullContact Inc. and contributors
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
FullContact Ruby Gem
|
2
|
+
====================
|
3
|
+
A Ruby wrapper for the [FullContact API](http://www.fullcontact.com/)
|
4
|
+
|
5
|
+
[](https://travis-ci.org/fullcontact/fullcontact-api-ruby)
|
6
|
+
[](http://badge.fury.io/rb/fullcontact)
|
7
|
+
[](https://codeclimate.com/github/fullcontact/fullcontact-api-ruby)
|
8
|
+
[](https://codeclimate.com/github/fullcontact/fullcontact-api-ruby)
|
9
|
+
|
10
|
+
Changes
|
11
|
+
-------
|
12
|
+
- 0.18.1
|
13
|
+
- Always use the token in header even when the option is given as query.
|
14
|
+
- 0.18.0
|
15
|
+
- Add ability to query Company API by company name
|
16
|
+
- Fix XML bug in Company API module
|
17
|
+
- 0.17.0 - Upgrade Faraday plugin to ~> 0.11.0
|
18
|
+
- 0.16.0 - Upgrade Faraday plugin to ~> 0.10.0.
|
19
|
+
- 0.15.0 - Add header-based auth via `config.auth_type = :header` control.
|
20
|
+
- 0.14.0 - Remove `plissken` gem to support Rails 5 (#42)
|
21
|
+
- 0.13.0
|
22
|
+
- Raise `ArgumentError` if query by Facebook ID/username is used.
|
23
|
+
- Remove deprecated messages.
|
24
|
+
- 0.12.0 - `include_headers_in_response = true` includes response headers in returned payload
|
25
|
+
- Accessible as `http_headers` in response. Also accessible on thrown errors (e.g. RateLimited)
|
26
|
+
- 0.11.0 - Plisskin transformation can be disabled by specifying a `skip_rubyize = true` in config block.
|
27
|
+
- 0.10.0 - Support for FullContact Company API
|
28
|
+
- 0.9.0 - Removed Rash gem and replaced with Mashify + Plisskin
|
29
|
+
- 0.8.2 - Fix for 0.8.0 constant resolution issue.
|
30
|
+
- 0.8.0
|
31
|
+
- Hashie now allowed from [2.2, 4.0) to support a wide range of other applications
|
32
|
+
- Default useragent includes version number for our own information
|
33
|
+
- Useless XML mode and dep on `multi_xml` removed
|
34
|
+
- Code reformatting & basic code hygiene, prep for new features in 0.9.0
|
35
|
+
- 0.7.0 - Faraday 0.9.0
|
36
|
+
- 0.6.0 - Removal of timeoutSeconds parameter. This parameter is automatically stripped from your request if included.
|
37
|
+
|
38
|
+
Installation
|
39
|
+
------------
|
40
|
+
gem install fullcontact
|
41
|
+
|
42
|
+
Documentation
|
43
|
+
-------------
|
44
|
+
[http://rdoc.info/gems/fullcontact](http://rdoc.info/gems/fullcontact)
|
45
|
+
|
46
|
+
Usage Examples
|
47
|
+
--------------
|
48
|
+
```ruby
|
49
|
+
require 'fullcontact'
|
50
|
+
|
51
|
+
# This could go in an initializer
|
52
|
+
FullContact.configure do |config|
|
53
|
+
config.api_key = 'fullcontact_api_key_goes_here'
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get information about an email address
|
57
|
+
person = FullContact.person(email: 'bart@fullcontact.com')
|
58
|
+
```
|
59
|
+
All returned values are Hashie structs. You can access fields as if they were fields:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# Get person's family_name
|
63
|
+
person.contact_info.family_name
|
64
|
+
=> "Lorang"
|
65
|
+
```
|
66
|
+
|
67
|
+
But you can also turn it into a normal hash
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# Get person's family_name
|
71
|
+
person.to_hash['contact_info']['family_name']
|
72
|
+
=> "Lorang"
|
73
|
+
```
|
74
|
+
Authentication is done through query parameters by default. If you want to use headers instead:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# This could go in an initializer
|
78
|
+
FullContact.configure do |config|
|
79
|
+
config.api_key = 'fullcontact_api_key_goes_here'
|
80
|
+
config.auth_type = :headers # :header or :query
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
There's other ways you can query the Person API:
|
85
|
+
```ruby
|
86
|
+
# Get information about an email address, organized by hashes vs. lists
|
87
|
+
person2 = FullContact.person(email: 'bart@fullcontact.com', style: 'dictionary')
|
88
|
+
|
89
|
+
# You can pass in any arbitrary parameters the Person API supports
|
90
|
+
person3 = FullContact.person(email: 'bart@fullcontact.com', style: 'dictionary', webhookUrl: 'https://...')
|
91
|
+
|
92
|
+
# Get information about a twitter handle
|
93
|
+
person4 = FullContact.person(twitter: "bartlorang")
|
94
|
+
|
95
|
+
# Get information from a phone number
|
96
|
+
person6 = FullContact.person(phone:13037170414)
|
97
|
+
|
98
|
+
# Get information about a twitter and ensure a 30s socket open timeout and a 15s socket read timeout
|
99
|
+
# Can throw a Faraday::Error::TimeoutError if timeouts are exceeded
|
100
|
+
person7 = FullContact.person({:twitter => "bartlorang"}, {:request => {:timeout => 15, :open_timeout => 30}})
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
Response formats can more closely mirror FullContact's APIs by disabling snake_case transformation:
|
105
|
+
```ruby
|
106
|
+
FullContact.configure do |config|
|
107
|
+
config.api_key = "fullcontact_api_key_goes_here"
|
108
|
+
config.skip_rubyize = true
|
109
|
+
end
|
110
|
+
|
111
|
+
person8 = FullContact.person(email: "bart@fullcontact.com")
|
112
|
+
|
113
|
+
=> #<Hashie::Mash contactInfo=#<Hashie::Mash chats=[#<Hashie::Mash client="gtalk" handle="lorangb@gmail.com">,
|
114
|
+
#<Hashie::Mash client="skype" handle="bart.lorang">] familyName="Lorang" fullName="Bart Lorang" givenName="Bart...
|
115
|
+
```
|
116
|
+
|
117
|
+
You can also query the Company API
|
118
|
+
```ruby
|
119
|
+
# Get information about a company
|
120
|
+
company1 = FullContact.company(domain: 'fullcontact.com')
|
121
|
+
|
122
|
+
company1.organization.name
|
123
|
+
=> "FullContact Inc."
|
124
|
+
```
|
125
|
+
|
126
|
+
The Company API also supports searching by company name.
|
127
|
+
Please see [our API documentation](https://www.fullcontact.com/developer/docs/company/#lookup-by-company-name) for more details.
|
128
|
+
```ruby
|
129
|
+
# Gets a list of search results for a company name ordered by relevance
|
130
|
+
companies = FullContact.company(companyName: 'FullContact')
|
131
|
+
|
132
|
+
# Get the API url for full company profile lookup (append api key to use)
|
133
|
+
# Note the array access, we are just grabbing the first (top) result
|
134
|
+
companies[0].company_api_link
|
135
|
+
=> "https://api.fullcontact.com/v2/company/lookup?domain=fullcontact.com&apiKey="
|
136
|
+
|
137
|
+
companies[0].org_name
|
138
|
+
=> "FullContact Inc."
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
Contributions
|
143
|
+
-------------
|
144
|
+
A full list of contributors can be found in
|
145
|
+
[GitHub](https://github.com/fullcontact/fullcontact-api-ruby/graphs/contributors)
|
146
|
+
|
147
|
+
License
|
148
|
+
---------
|
149
|
+
Copyright (c) 2016 FullContact Inc. and contributors
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
See [LICENSE](https://github.com/fullcontact/fullcontact-api-ruby/blob/master/LICENSE.md) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :test => :spec
|
10
|
+
task :default => :spec
|
11
|
+
|
12
|
+
namespace :doc do
|
13
|
+
require 'yard'
|
14
|
+
YARD::Rake::YardocTask.new do |task|
|
15
|
+
task.files = ['HISTORY.md', 'LICENSE.md', 'lib/**/*.rb']
|
16
|
+
task.options = [
|
17
|
+
'--protected',
|
18
|
+
'--output-dir', 'doc',
|
19
|
+
'--tag', 'format:Supported formats',
|
20
|
+
'--tag', 'authenticated:Requires Authentication',
|
21
|
+
'--tag', 'rate_limited:Rate Limited',
|
22
|
+
'--markup', 'markdown',
|
23
|
+
]
|
24
|
+
end
|
25
|
+
end
|
data/fullcontact.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'fullcontact/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.add_development_dependency 'maruku', '~> 0.7'
|
7
|
+
s.add_development_dependency 'nokogiri', '~> 1.6'
|
8
|
+
s.add_development_dependency 'rake', '~> 0.9'
|
9
|
+
s.add_development_dependency 'rspec', '~> 3.1'
|
10
|
+
s.add_development_dependency 'simplecov'
|
11
|
+
s.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
|
12
|
+
s.add_development_dependency 'webmock', '~> 1.6'
|
13
|
+
s.add_development_dependency 'yard', '~> 0.9.11'
|
14
|
+
s.add_runtime_dependency 'hashie', ['>= 2.0', '< 4.0']
|
15
|
+
s.add_runtime_dependency 'faraday', '~> 1'
|
16
|
+
s.add_runtime_dependency 'faraday_middleware', '~> 1'
|
17
|
+
|
18
|
+
s.author = 'Ratnam Yadav'
|
19
|
+
s.description = %q{A Ruby wrapper for the FullContact API}
|
20
|
+
s.email = ['support@fullcontact.com']
|
21
|
+
s.license = 'MIT'
|
22
|
+
|
23
|
+
s.post_install_message = ''
|
24
|
+
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
|
26
|
+
s.files = `git ls-files`.split("\n")
|
27
|
+
s.homepage = 'https://github.com/ratnamyadav/fullcontact-api-ruby'
|
28
|
+
s.name = 'fullcontact-api-ruby'
|
29
|
+
s.require_paths = ['lib']
|
30
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
|
31
|
+
s.summary = %q{Ruby wrapper for the FullContact API}
|
32
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
33
|
+
s.version = FullContact::VERSION
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Request::Gateway < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
url = env[:url].dup
|
9
|
+
url.host = @gateway
|
10
|
+
env[:url] = url
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app, gateway)
|
15
|
+
@app, @gateway = app, gateway
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Faraday
|
2
|
+
class Response::FullContactErrors < Response::Middleware
|
3
|
+
def on_complete(env)
|
4
|
+
case env[:status]
|
5
|
+
when 400
|
6
|
+
raise FullContact::BadRequest.new(error_message(env), env[:response_headers])
|
7
|
+
when 401
|
8
|
+
raise FullContact::Unauthorized.new(error_message(env), env[:response_headers])
|
9
|
+
when 403
|
10
|
+
raise FullContact::Forbidden.new(error_message(env), env[:response_headers])
|
11
|
+
when 404
|
12
|
+
raise FullContact::NotFound.new(error_message(env), env[:response_headers])
|
13
|
+
when 422
|
14
|
+
raise FullContact::Invalid.new(error_message(env), env[:response_headers])
|
15
|
+
when 429
|
16
|
+
# Note: not currently in use
|
17
|
+
raise FullContact::RateLimited.new(error_message(env), env[:response_headers])
|
18
|
+
when 500
|
19
|
+
raise FullContact::InternalServerError.new(error_message(env), env[:response_headers])
|
20
|
+
when 502
|
21
|
+
raise FullContact::BadGateway.new(error_message(env), env[:response_headers])
|
22
|
+
when 503
|
23
|
+
raise FullContact::ServiceUnavailable.new(error_message(env), env[:response_headers])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def error_message(env)
|
28
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(app)
|
32
|
+
super
|
33
|
+
@parser = nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'fullcontact/connection'
|
2
|
+
require 'fullcontact/request'
|
3
|
+
|
4
|
+
module FullContact
|
5
|
+
# @private
|
6
|
+
class API
|
7
|
+
# @private
|
8
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
9
|
+
|
10
|
+
# Creates a new API
|
11
|
+
def initialize(options={})
|
12
|
+
options = FullContact.options.merge(options)
|
13
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
14
|
+
send("#{key}=", options[key])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
include Connection
|
19
|
+
include Request
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FullContact
|
2
|
+
class Client
|
3
|
+
module Company
|
4
|
+
# Returns extended information for a given company (email, phone, twitter or facebook)
|
5
|
+
#
|
6
|
+
def company(options={}, faraday_options={})
|
7
|
+
url = "company/lookup"
|
8
|
+
if options[:companyName]
|
9
|
+
url = "company/search"
|
10
|
+
end
|
11
|
+
response = get(url, options, false, faraday_options)
|
12
|
+
format.to_s.downcase == 'xml' ? response['response'] : response
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module FullContact
|
2
|
+
class Client
|
3
|
+
module Person
|
4
|
+
# Returns extended information for a given person (email, phone, twitter or facebook)
|
5
|
+
#
|
6
|
+
def person(options={}, faraday_options={})
|
7
|
+
if options.is_a?(String)
|
8
|
+
raise ArgumentError, "Supplying an email address directly is not supported. Please use {email: #{options}} instead."
|
9
|
+
end
|
10
|
+
|
11
|
+
if options.has_key?(:facebookUsername) || options.has_key?(:facebookId)
|
12
|
+
raise ArgumentError, "Querying by Facebook ID or username is no longer supported. Please contact support@fullcontact.com for more information."
|
13
|
+
end
|
14
|
+
|
15
|
+
response = get('person', options, false, faraday_options)
|
16
|
+
format.to_s.downcase == 'xml' ? response['person'] : response
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FullContact
|
2
|
+
# Wrapper for the FullContact REST API
|
3
|
+
|
4
|
+
class Client < API
|
5
|
+
# Require client method modules after initializing the Client class in
|
6
|
+
# order to avoid a superclass mismatch error, allowing those modules to be
|
7
|
+
# Client-namespaced.
|
8
|
+
require 'fullcontact/client/person'
|
9
|
+
require 'fullcontact/client/company'
|
10
|
+
|
11
|
+
alias :api_endpoint :endpoint
|
12
|
+
|
13
|
+
include FullContact::Client::Person
|
14
|
+
include FullContact::Client::Company
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'fullcontact/version'
|
3
|
+
|
4
|
+
module FullContact
|
5
|
+
# Defines constants and methods related to configuration
|
6
|
+
module Configuration
|
7
|
+
# An array of valid keys in the options hash when configuring a {FullContact::API}
|
8
|
+
VALID_OPTIONS_KEYS = [
|
9
|
+
:adapter,
|
10
|
+
:api_key,
|
11
|
+
:auth_type,
|
12
|
+
:endpoint,
|
13
|
+
:format,
|
14
|
+
:skip_rubyize,
|
15
|
+
:include_headers_in_response,
|
16
|
+
:gateway,
|
17
|
+
:proxy,
|
18
|
+
:user_agent].freeze
|
19
|
+
|
20
|
+
# An array of valid request/response formats
|
21
|
+
VALID_FORMATS = [:json].freeze
|
22
|
+
|
23
|
+
# The adapter that will be used to connect if none is set
|
24
|
+
#
|
25
|
+
# @note The default faraday adapter is Net::HTTP.
|
26
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
27
|
+
|
28
|
+
# By default, don't set an application key
|
29
|
+
DEFAULT_API_KEY = nil
|
30
|
+
|
31
|
+
# By default, use query parameters
|
32
|
+
DEFAULT_AUTH_TYPE = :query
|
33
|
+
|
34
|
+
# The endpoint that will be used to connect if none is set
|
35
|
+
#
|
36
|
+
DEFAULT_ENDPOINT = 'https://api.fullcontact.com/v2/'.freeze
|
37
|
+
|
38
|
+
# The response format appended to the path and sent in the 'Accept' header if none is set
|
39
|
+
#
|
40
|
+
# @note JSON is preferred over XML because it is more concise and faster to parse.
|
41
|
+
DEFAULT_FORMAT = :json
|
42
|
+
|
43
|
+
# Default transformation done to response
|
44
|
+
DEFAULT_SKIP_RUBYIZE = false
|
45
|
+
|
46
|
+
# Includes response headers
|
47
|
+
DEFAULT_INCLUDE_HEADERS_IN_RESPONSE = false
|
48
|
+
|
49
|
+
# By default, don't use a proxy server
|
50
|
+
DEFAULT_PROXY = nil
|
51
|
+
|
52
|
+
# The user agent that will be sent to the API endpoint if none is set
|
53
|
+
DEFAULT_USER_AGENT = "FullContact Ruby Client/#{FullContact::VERSION}".freeze
|
54
|
+
|
55
|
+
DEFAULT_GATEWAY = nil
|
56
|
+
|
57
|
+
AUTH_HEADER_NAME = 'X-FullContact-APIKey'.freeze
|
58
|
+
|
59
|
+
# @private
|
60
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
61
|
+
|
62
|
+
# When this module is extended, set all configuration options to their default values
|
63
|
+
def self.extended(base)
|
64
|
+
base.reset
|
65
|
+
end
|
66
|
+
|
67
|
+
# Convenience method to allow configuration options to be set in a block
|
68
|
+
def configure
|
69
|
+
yield self
|
70
|
+
end
|
71
|
+
|
72
|
+
# Create a hash of options and their values
|
73
|
+
def options
|
74
|
+
options = {}
|
75
|
+
VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
|
76
|
+
options
|
77
|
+
end
|
78
|
+
|
79
|
+
# Reset all configuration options to defaults
|
80
|
+
def reset
|
81
|
+
self.adapter = DEFAULT_ADAPTER
|
82
|
+
self.api_key = DEFAULT_API_KEY
|
83
|
+
self.auth_type = DEFAULT_AUTH_TYPE
|
84
|
+
self.endpoint = DEFAULT_ENDPOINT
|
85
|
+
self.format = DEFAULT_FORMAT
|
86
|
+
self.skip_rubyize = DEFAULT_SKIP_RUBYIZE
|
87
|
+
self.include_headers_in_response = DEFAULT_INCLUDE_HEADERS_IN_RESPONSE
|
88
|
+
self.proxy = DEFAULT_PROXY
|
89
|
+
self.user_agent = DEFAULT_USER_AGENT
|
90
|
+
self.gateway = DEFAULT_GATEWAY
|
91
|
+
self
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/request/gateway'
|
3
|
+
require 'faraday/response/fullcontact_errors'
|
4
|
+
require 'faraday/response/add_headers'
|
5
|
+
require 'faraday/response/rubyize'
|
6
|
+
require 'faraday_middleware/response/mashify'
|
7
|
+
|
8
|
+
|
9
|
+
module FullContact
|
10
|
+
# @private
|
11
|
+
module Connection
|
12
|
+
private
|
13
|
+
|
14
|
+
def connection(raw=false, faraday_options={})
|
15
|
+
options = {
|
16
|
+
:headers => {'Accept' => "application/#{format}", 'User-Agent' => user_agent},
|
17
|
+
:proxy => proxy,
|
18
|
+
:ssl => {:verify => false},
|
19
|
+
:url => api_endpoint,
|
20
|
+
}.merge(faraday_options)
|
21
|
+
|
22
|
+
Faraday.new(options) do |builder|
|
23
|
+
builder.use Faraday::Request::UrlEncoded
|
24
|
+
builder.use Faraday::Request::Gateway, gateway if gateway
|
25
|
+
builder.use FaradayMiddleware::Mashify unless raw
|
26
|
+
builder.use Faraday::Response::AddHeaders if FullContact.include_headers_in_response
|
27
|
+
builder.use Faraday::Response::Rubyize unless raw or FullContact.skip_rubyize
|
28
|
+
unless raw
|
29
|
+
case format.to_s.downcase
|
30
|
+
when 'json'
|
31
|
+
builder.use Faraday::Response::ParseJson
|
32
|
+
end
|
33
|
+
end
|
34
|
+
builder.use Faraday::Response::FullContactErrors
|
35
|
+
builder.adapter Faraday.default_adapter
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module FullContact
|
2
|
+
# Custom error class for rescuing from all FullContact errors
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
def initialize(message, http_headers)
|
7
|
+
@http_headers = Hash[http_headers]
|
8
|
+
super message
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Raised when FullContact returns the HTTP status code 400
|
13
|
+
class BadRequest < Error;
|
14
|
+
end
|
15
|
+
|
16
|
+
# Raised when FullContact returns the HTTP status code 401
|
17
|
+
class Unauthorized < Error;
|
18
|
+
end
|
19
|
+
|
20
|
+
# Raised when FullContact returns the HTTP status code 403
|
21
|
+
class Forbidden < Error;
|
22
|
+
end
|
23
|
+
|
24
|
+
# Raised when FullContact returns the HTTP status code 404
|
25
|
+
class NotFound < Error;
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raised when FullContact returns the HTTP status code 422
|
29
|
+
class Invalid < Error;
|
30
|
+
end
|
31
|
+
|
32
|
+
# Raised when FullContact returns the HTTP status code 429
|
33
|
+
class RateLimited < Error;
|
34
|
+
end
|
35
|
+
|
36
|
+
# Raised when FullContact returns the HTTP status code 500
|
37
|
+
class InternalServerError < Error;
|
38
|
+
end
|
39
|
+
|
40
|
+
# Raised when FullContact returns the HTTP status code 502
|
41
|
+
class BadGateway < Error;
|
42
|
+
end
|
43
|
+
|
44
|
+
# Raised when FullContact returns the HTTP status code 503
|
45
|
+
class ServiceUnavailable < Error;
|
46
|
+
end
|
47
|
+
end
|