clearbit 0.1.5 → 0.1.6.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94181dcf96160372ef82dd215b4696aba62d39fb
4
- data.tar.gz: a8752294bc5b63fe7d3a055c94429467131c9288
3
+ metadata.gz: 7680a6adda38c41c8038adba29867b6d198da4de
4
+ data.tar.gz: 850f0b423e1d1139d9837ec3a6f518e7a0952383
5
5
  SHA512:
6
- metadata.gz: f7085df982027dc0af97c63e483457c7f9e5287d9f580bee1108a9b2b13d999db394e31941ee0e44ecefb1f8ecdcad00066573db17728a01d645eef95d925d09
7
- data.tar.gz: 8b3e2cd83dabd56cf74294cbda5f2c9d46206f0774caecd7606ce048e9c175bc6e1dbc2c90266b4e92ebff27a11bd311462af8edf95ce9f4f4100366a4dba0f4
6
+ metadata.gz: ac5d711f3deed52072696fa2cc89c40c542cbf80910a23de42c7a8c008c0ac7340d9b6140fdd1b5d46a3666432963bbf5d96d060d9765d66ab3d4a75c40fb9ff
7
+ data.tar.gz: 23ee689884c397194e4b01983eb0949468d4505e2018eed63302183465d44444a0f340958f28420ebeafa5fe0a53c80ff5d4054fc8230f9583e758a1ef559e89
data/README.md CHANGED
@@ -77,3 +77,21 @@ Or to look up a company:
77
77
  "Garrett Camp"
78
78
  ],
79
79
  ...
80
+
81
+ ## Webhooks
82
+
83
+ For rack apps use the `Clearbit::Webhook` module to wrap deserialization and verify the webhook is from trusted party:
84
+
85
+ ``` ruby
86
+ post '/v1/webhooks/clearbit' do
87
+ begin
88
+ webhook = Clearbit::Webhook.new(env)
89
+ webhook.type #=> 'person'
90
+ webhook.body.name.given_name #=> 'Alex'
91
+
92
+ # ...
93
+ rescue Clearbit::Errors::InvalidWebhookSignature => e
94
+ # ...
95
+ end
96
+ end
97
+ ```
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ rescue LoadError
10
+ # no rspec available
11
+ end
@@ -19,7 +19,10 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
- spec.add_development_dependency 'rake'
23
22
  spec.add_development_dependency 'net-http-spy'
23
+ spec.add_development_dependency 'pry'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'rack'
24
27
  spec.add_dependency 'nestful', '~> 1.0.7'
25
28
  end
@@ -10,14 +10,28 @@ module Clearbit
10
10
  Base.key = value
11
11
  end
12
12
 
13
+ def self.key
14
+ Base.key
15
+ end
16
+
17
+ def self.key!
18
+ key || raise('Clearbit.key not set')
19
+ end
20
+
13
21
  autoload :Base, 'clearbit/base'
14
22
  autoload :Company, 'clearbit/company'
15
23
  autoload :Mash, 'clearbit/mash'
16
24
  autoload :Person, 'clearbit/person'
17
25
  autoload :PersonCompany, 'clearbit/person_company'
26
+ autoload :Prospector, 'clearbit/prospector'
18
27
  autoload :Resource, 'clearbit/resource'
19
28
  autoload :Streaming, 'clearbit/streaming'
20
29
  autoload :Watchlist, 'clearbit/watchlist'
30
+ autoload :Webhook, 'clearbit/webhook'
31
+
32
+ module Errors
33
+ autoload :InvalidWebhookSignature, 'clearbit/errors/invalid_webhook_signature'
34
+ end
21
35
 
22
36
  if clearbit_key = ENV['CLEARBIT_KEY']
23
37
  Clearbit.key = clearbit_key
@@ -10,6 +10,12 @@ module Clearbit
10
10
  def self.key=(value)
11
11
  add_options auth_type: :bearer,
12
12
  password: value
13
+
14
+ @key = value
15
+ end
16
+
17
+ def self.key
18
+ @key
13
19
  end
14
20
  end
15
21
  end
@@ -0,0 +1,10 @@
1
+ module Clearbit
2
+ module Errors
3
+ # Raised when the Webhook Request Signature doesn't validate.
4
+ class InvalidWebhookSignature < StandardError
5
+ def to_s
6
+ 'Clearbit Webhook Request Signature was invalid.'
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Clearbit
2
+ class Prospector < Base
3
+ endpoint 'https://prospector.clearbit.com'
4
+ path '/v1/people'
5
+
6
+ def self.search(params = {})
7
+ self.new get('search', params)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Clearbit
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6.pre"
3
3
  end
@@ -0,0 +1,35 @@
1
+ require 'openssl'
2
+ require 'rack'
3
+ require 'json'
4
+
5
+ module Clearbit
6
+ class Webhook < Mash
7
+ def self.clearbit_key
8
+ Clearbit.key!
9
+ end
10
+
11
+ def self.valid?(request_signature, body)
12
+ return false unless request_signature && body
13
+
14
+ signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), clearbit_key, body)
15
+ Rack::Utils.secure_compare(request_signature, signature)
16
+ end
17
+
18
+ def self.valid!(signature, body)
19
+ valid?(signature, body) ? true : raise(Errors::InvalidWebhookSignature.new)
20
+ end
21
+
22
+ def initialize(env)
23
+ request = Rack::Request.new(env)
24
+
25
+ request.body.rewind
26
+
27
+ signature = request.env['HTTP_X_REQUEST_SIGNATURE']
28
+ body = request.body.read
29
+
30
+ self.class.valid!(signature, body)
31
+
32
+ merge!(JSON.parse(body))
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Clearbit::Webhook do
4
+ before do |example|
5
+ Clearbit.key = 'clearbit_key'
6
+ end
7
+
8
+ context 'valid signature' do
9
+ it 'returns true' do
10
+ signature = generate_signature('A-OK')
11
+
12
+ result = Clearbit::Webhook.valid!(signature, 'A-OK')
13
+
14
+ expect(result).to eq true
15
+ end
16
+ end
17
+
18
+ context 'invalid signature' do
19
+ it 'returns false' do
20
+ signature = generate_signature('A-OK')
21
+
22
+ expect {
23
+ Clearbit::Webhook.valid!(signature, 'TAMPERED_WITH_BODY_BEWARE!')
24
+ }.to raise_error(Clearbit::Errors::InvalidWebhookSignature)
25
+ end
26
+ end
27
+
28
+ context 'initialize' do
29
+ it 'returns a mash' do
30
+ request_body = JSON.dump({
31
+ id: '123',
32
+ type: 'person',
33
+ body: nil,
34
+ status: 404
35
+ })
36
+
37
+ request_signature = generate_signature(request_body)
38
+
39
+ env = Rack::MockRequest.env_for('/webhook',
40
+ method: 'POST',
41
+ input: request_body,
42
+ 'HTTP_X_REQUEST_SIGNATURE' => request_signature
43
+ )
44
+
45
+ webhook = Clearbit::Webhook.new(env)
46
+
47
+ expect(webhook.status).to eq 404
48
+ end
49
+ end
50
+
51
+ def generate_signature(webhook_body)
52
+ 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), 'clearbit_key', webhook_body)
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
+
4
+ # External
5
+ require 'rubygems'
6
+ require 'rspec'
7
+ require 'pry'
8
+
9
+ # Library
10
+ require 'clearbit'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clearbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex MacCaw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-http-spy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +67,21 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
- name: net-http-spy
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
43
85
  requirement: !ruby/object:Gem::Requirement
44
86
  requirements:
45
87
  - - ">="
@@ -88,9 +130,11 @@ files:
88
130
  - lib/clearbit.rb
89
131
  - lib/clearbit/base.rb
90
132
  - lib/clearbit/company.rb
133
+ - lib/clearbit/errors/invalid_webhook_signature.rb
91
134
  - lib/clearbit/mash.rb
92
135
  - lib/clearbit/person.rb
93
136
  - lib/clearbit/person_company.rb
137
+ - lib/clearbit/prospector.rb
94
138
  - lib/clearbit/resource.rb
95
139
  - lib/clearbit/streaming.rb
96
140
  - lib/clearbit/streaming/company.rb
@@ -98,6 +142,9 @@ files:
98
142
  - lib/clearbit/streaming/person_company.rb
99
143
  - lib/clearbit/version.rb
100
144
  - lib/clearbit/watchlist.rb
145
+ - lib/clearbit/webhook.rb
146
+ - spec/lib/clearbit/webhook_spec.rb
147
+ - spec/spec_helper.rb
101
148
  homepage: https://github.com/maccman/clearbit-ruby
102
149
  licenses:
103
150
  - MIT
@@ -113,14 +160,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
160
  version: '0'
114
161
  required_rubygems_version: !ruby/object:Gem::Requirement
115
162
  requirements:
116
- - - ">="
163
+ - - ">"
117
164
  - !ruby/object:Gem::Version
118
- version: '0'
165
+ version: 1.3.1
119
166
  requirements: []
120
167
  rubyforge_project:
121
168
  rubygems_version: 2.2.2
122
169
  signing_key:
123
170
  specification_version: 4
124
171
  summary: API client for clearbit.com
125
- test_files: []
172
+ test_files:
173
+ - spec/lib/clearbit/webhook_spec.rb
174
+ - spec/spec_helper.rb
126
175
  has_rdoc: