hubbah 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05171a5c92b62887259db1cb3aed55942a2d8561
4
- data.tar.gz: baabae2c2bc72b5ba066723da97bfd809263d191
3
+ metadata.gz: e8095294a475a4801dc9d40821b10b4dd69b24e2
4
+ data.tar.gz: 8196cc58e67641d3302c2cd298e5182d5f71c99f
5
5
  SHA512:
6
- metadata.gz: 6053412e3bfb5a8d368c8bfda8f34f25fb5d2bc4a3dd79e6fd2bdbaa8e5df904eef9d48bc165bc7efcd47749ab5ef85c31dba01e075b47a8b12ace72f05a6139
7
- data.tar.gz: 02ea341d96da561688a4a4b10854a2191f195be52edaf6860208d94f618db343c5ccac25c0948222494e0fab0f059642ed8e75050aef858062747e8edf8420c5
6
+ metadata.gz: 38cc0aa4cd2703afe7356faf837b817a986555db836630f84fba35f33bfed9951b8468d1d3433093e77945c6eae811158b086d119b790cf7bd65cb4faba58321
7
+ data.tar.gz: da33303148af998ca7025caf358ebb38f2940159575d0c3fdbc29df15c7f16661b707686a7ab1f988a1c45aef90fc5e887b8e34cc9a6c84a9756932af4b58792
@@ -1,6 +1,8 @@
1
1
  require 'hubbah/version'
2
2
  require 'hubbah/configuration'
3
+ require 'hubbah/model'
3
4
  require 'hubbah/form_submission'
5
+ require 'hubbah/contact'
4
6
 
5
7
  require 'hubbah/middleware'
6
8
  require 'hubbah/payload'
@@ -29,4 +31,5 @@ module Hubbah
29
31
  class Error < Exception; end
30
32
  class HubIdNotConfigured < Error; end
31
33
  class ServerErrorEncountered < Error; end
34
+ class AccessDenied < Error; end
32
35
  end
@@ -0,0 +1,75 @@
1
+ require 'uri'
2
+ require 'json'
3
+
4
+ module Hubbah
5
+ class Contact < Hubbah::Model
6
+ attr_reader :attributes, :vid
7
+
8
+ def initialize(attributes)
9
+ @attributes = attributes
10
+ end
11
+
12
+ def email
13
+ attributes['email']
14
+ end
15
+
16
+ def save
17
+ resp = client.post do |req|
18
+ req.url "/contacts/v1/contact/createOrUpdate/email/#{URI.encode(email)}/"
19
+ req.body = { properties: property_collection }.to_json
20
+ req.headers[:content_type] = 'application/json'
21
+ end
22
+
23
+ if resp.status == 200
24
+ @vid = JSON.parse(resp.body)['vid']
25
+ return true
26
+ elsif resp.status == 401
27
+ raise Hubbah::AccessDenied.new(resp.body)
28
+ elsif resp.status == 500
29
+ raise Hubbah::ServerErrorEncountered.new(resp.body)
30
+ else
31
+ raise Hubbah::Error.new(resp.body)
32
+ end
33
+ end
34
+
35
+ def property_collection
36
+ @attributes.map do |field, value|
37
+ {
38
+ property: field,
39
+ value: value
40
+ }
41
+ end
42
+ end
43
+
44
+ class << self
45
+ def create_or_update(email, attributes)
46
+ new(attributes.merge('email' => email)).tap do |contact|
47
+ contact.save
48
+ end
49
+ end
50
+ end
51
+
52
+ protected
53
+ def client
54
+ @client ||= Faraday.new({
55
+ url: 'http://api.hubapi.com/',
56
+ params: {
57
+ hapikey: api_key,
58
+ portalId: hub_id
59
+ }
60
+ })
61
+ end
62
+
63
+ def hub_id
64
+ configuration.hub_id
65
+ end
66
+
67
+ def api_key
68
+ configuration.api_key
69
+ end
70
+
71
+ def configuration
72
+ Hubbah.configuration
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module Hubbah
2
+ class Model
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Hubbah
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,41 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/user@example.com/?hapikey=<HUBBAH_API_KEY>&portalId=<HUBBAH_HUB_ID>
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"properties":[{"property":"firstname","value":"user"},{"property":"email","value":"user@example.com"}]}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - '*/*'
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Access-Control-Allow-Credentials:
24
+ - 'false'
25
+ Content-Type:
26
+ - application/json; charset=UTF-8
27
+ Date:
28
+ - Fri, 30 May 2014 21:07:49 GMT
29
+ Vary:
30
+ - Accept-Encoding
31
+ - Accept-Encoding
32
+ Content-Length:
33
+ - '47'
34
+ Connection:
35
+ - keep-alive
36
+ body:
37
+ encoding: UTF-8
38
+ string: '{"vid":21823,"isNew":false}'
39
+ http_version:
40
+ recorded_at: Fri, 30 May 2014 21:07:49 GMT
41
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hubbah::Contact, vcr: true do
4
+ let(:email) { 'user@example.com' }
5
+ it 'updates a relevant contact' do
6
+ contact = Hubbah::Contact.create_or_update(email, {
7
+ 'firstname' => 'user'
8
+ })
9
+ expect(contact.vid).to_not be_nil
10
+ end
11
+
12
+ it 'raises an error when I encounter a 500' do
13
+ contact = Hubbah::Contact.new({
14
+ 'email' => email,
15
+ 'firstname' => 'user'
16
+ })
17
+
18
+ stub_response(contact, [500, {}, 'something bad happened'])
19
+ expect(lambda { contact.save }).to raise_error(Hubbah::ServerErrorEncountered)
20
+ expect(contact.vid).to be_nil
21
+ end
22
+
23
+ it 'raises an error when I encounter a 401' do
24
+ contact = Hubbah::Contact.new({
25
+ 'email' => email,
26
+ 'firstname' => 'user'
27
+ })
28
+
29
+ stub_response(contact, [401, {}, 'something bad happened'])
30
+ expect(lambda { contact.save }).to raise_error(Hubbah::AccessDenied)
31
+ expect(contact.vid).to be_nil
32
+ end
33
+
34
+ protected
35
+ def stub_response(contact, response)
36
+ path = "/contacts/v1/contact/createOrUpdate/email/#{URI.encode(contact.email)}/"
37
+ mock_client = Faraday.new do |builder|
38
+ builder.adapter :test do |stubs|
39
+ stubs.post(path) { response }
40
+ end
41
+ end
42
+
43
+ contact.stubs(:client).returns(mock_client)
44
+ end
45
+ end
@@ -33,6 +33,7 @@ describe Hubbah::FormSubmission, vcr: true do
33
33
 
34
34
  submission.stubs(:client).returns(mock_client)
35
35
  end
36
+
36
37
  def form_guid
37
38
  ENV['HUBBAH_FORM_GUID']
38
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubbah
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Pickett
@@ -224,16 +224,20 @@ files:
224
224
  - hubbah.gemspec
225
225
  - lib/hubbah.rb
226
226
  - lib/hubbah/configuration.rb
227
+ - lib/hubbah/contact.rb
227
228
  - lib/hubbah/form_submission.rb
228
229
  - lib/hubbah/middleware.rb
230
+ - lib/hubbah/model.rb
229
231
  - lib/hubbah/payload.rb
230
232
  - lib/hubbah/version.rb
233
+ - spec/fixtures/vcr_cassettes/Hubbah_Contact/updates_a_relevant_contact.yml
231
234
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/does_not_submit_a_form_if_required_data_is_not_supplied.yml
232
235
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/raises_an_exception_when_a_404_is_encountered.yml
233
236
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/raises_an_exception_when_a_500_is_encountered.yml
234
237
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/submits_a_form_with_required_data_successfully.yml
235
238
  - spec/fixtures/vcr_cassettes/Hubbah_Payload/submits_successfully_to_hubspot.yml
236
239
  - spec/hubbah/configuration_spec.rb
240
+ - spec/hubbah/contact_spec.rb
237
241
  - spec/hubbah/form_submission_spec.rb
238
242
  - spec/hubbah/middleware_spec.rb
239
243
  - spec/hubbah/payload_spec.rb
@@ -265,12 +269,14 @@ signing_key:
265
269
  specification_version: 4
266
270
  summary: Make hubspot form submissions a snap
267
271
  test_files:
272
+ - spec/fixtures/vcr_cassettes/Hubbah_Contact/updates_a_relevant_contact.yml
268
273
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/does_not_submit_a_form_if_required_data_is_not_supplied.yml
269
274
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/raises_an_exception_when_a_404_is_encountered.yml
270
275
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/raises_an_exception_when_a_500_is_encountered.yml
271
276
  - spec/fixtures/vcr_cassettes/Hubbah_FormSubmission/submits_a_form_with_required_data_successfully.yml
272
277
  - spec/fixtures/vcr_cassettes/Hubbah_Payload/submits_successfully_to_hubspot.yml
273
278
  - spec/hubbah/configuration_spec.rb
279
+ - spec/hubbah/contact_spec.rb
274
280
  - spec/hubbah/form_submission_spec.rb
275
281
  - spec/hubbah/middleware_spec.rb
276
282
  - spec/hubbah/payload_spec.rb