hubspot-ruby 0.5.0 → 0.6.0

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
- SHA1:
3
- metadata.gz: 1ed5f2aa1fbcdc87f14f1197d9b78dad4380f057
4
- data.tar.gz: 2184008548937dbbbb103f30d3eb96c11f3c0d8c
2
+ SHA256:
3
+ metadata.gz: 371e11640e32bc9b2ba0bb54f3fbdfd3b84dcb5f4be69fd0d4e89163fdf544bb
4
+ data.tar.gz: 8f27026960fb8cc21c1f25c76dba6a26e4c33be192a635728a939284f52bea09
5
5
  SHA512:
6
- metadata.gz: 20f5233aa72b00ce366785e64b0ac385c8a48d86e2c79df9196958b668c573e32018cb2924e86c0216d489049535ce443dad9e7227a9b35941160e938f22023c
7
- data.tar.gz: bea00d2e9baf596c71a2fcbaeaaa89b081dbbec0b900f03226bdfbbef8fc7ebab1bb0618739a660e1ccf624199341607f79412a97877ed87d1fde0732dec2feb
6
+ metadata.gz: bed3f9319a9dc8db09aa7a050d3724055e502cece3a2b3400c19fb7125fb4a38c1cf5366deff6e22f7ff34fc9db69d380c29e4f18df69763d422941efcd47019
7
+ data.tar.gz: 64b3ddc122391b6d3fd44ec0cbe123ae92b306a632ebce366a9a43ee7acee0ef705e919c8d05661d6906e6d2454435352fc5b5c7422bd58d9320adc92ac5f4f7
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # HubSpot REST API wrappers for ruby
2
2
 
3
+ [![Build Status](https://travis-ci.org/adimichele/hubspot-ruby.svg?branch=v0-stable)](https://travis-ci.org/adimichele/hubspot-ruby)
4
+
3
5
  Wraps the HubSpot REST API for convenient access from ruby applications.
4
6
 
5
7
  Documentation for the HubSpot REST API can be found here: https://developers.hubspot.com/docs/endpoints
@@ -132,13 +134,23 @@ Hubspot::Deal.create!(nil, [company.vid], [contact.vid], pipeline: 'default', de
132
134
 
133
135
  ### Testing
134
136
 
135
- All tests can be run with `rake spec`. Isolate fast-running tests with `rake spec:quick`.
137
+ This project uses [VCR] to test interactions with the HubSpot API.
138
+ VCR records HTTP requests and replays them during future tests.
139
+
140
+ To run the tests, run `bundle exec rake` or `bundle exec rspec`.
141
+
142
+ By default, the VCR recording mode is set to `:none`, which allows recorded
143
+ requests to be re-played but raises for any new request. This prevents the test
144
+ suite from issuing unexpected HTTP requests.
136
145
 
137
- GET requests are pretty easy to test with VCR, but for POST/PUT requests, you probably want to update verify the state
138
- of a live HubSpot instance. To do this, please add "live" tests to `spec/live/`, using the rspec label `live: true` in
139
- order to disable VCR.
146
+ To add a new test or update a VCR recording, run the test with the `VCR_RECORD`
147
+ environment variable:
148
+
149
+ ```sh
150
+ VCR_RECORD=1 bundle exec rspec spec
151
+ ```
140
152
 
141
- "Live" tests can be isolated with `rake spec:live`.
153
+ [VCR]: https://github.com/vcr/vcr
142
154
 
143
155
  ## Disclaimer
144
156
 
data/Rakefile CHANGED
@@ -14,20 +14,10 @@ require 'rake'
14
14
  require 'rspec/core'
15
15
  require 'rspec/core/rake_task'
16
16
 
17
- namespace :spec do
18
- RSpec::Core::RakeTask.new(:quick) do |spec|
19
- spec.pattern = FileList['spec/**/*_spec.rb'].select{ |s| !s.match("/live/") }
20
- end
21
- RSpec::Core::RakeTask.new(:live) do |spec|
22
- spec.pattern = FileList['spec/live/*_spec.rb']
23
- end
24
- end
25
-
26
- RSpec::Core::RakeTask.new(:spec) do |spec|
27
- spec.pattern = FileList['spec/**/*_spec.rb']
28
- end
17
+ RSpec::Core::RakeTask.new(:spec)
29
18
 
30
- task :default => :spec
19
+ desc "Default: runs the full test suite."
20
+ task default: :spec
31
21
 
32
22
  require 'rdoc/task'
33
23
  Rake::RDocTask.new do |rdoc|
data/hubspot-ruby.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "hubspot-ruby"
3
- s.version = "0.5.0"
3
+ s.version = "0.6.0"
4
4
  s.require_paths = ["lib"]
5
5
  s.authors = ["Andrew DiMichele", "Chris Bisnett"]
6
6
  s.description = "hubspot-ruby is a wrapper for the HubSpot REST API"
data/lib/hubspot-ruby.rb CHANGED
@@ -28,3 +28,6 @@ module Hubspot
28
28
 
29
29
  require 'hubspot/railtie' if defined?(Rails)
30
30
  end
31
+
32
+ # Alias the module for those looking to use the stylized name HubSpot
33
+ HubSpot = Hubspot
@@ -40,6 +40,33 @@ module Hubspot
40
40
  response['results'].map { |c| new(c) }
41
41
  end
42
42
 
43
+ # Find all companies by created date (descending)
44
+ # recently_updated [boolean] (for querying all accounts by modified time)
45
+ # count [Integer] for pagination
46
+ # offset [Integer] for pagination
47
+ # {http://developers.hubspot.com/docs/methods/companies/get_companies_created}
48
+ # {http://developers.hubspot.com/docs/methods/companies/get_companies_modified}
49
+ # @return [Object], you can get:
50
+ # response.results for [Array]
51
+ # response.hasMore for [Boolean]
52
+ # response.offset for [Integer]
53
+ def all_with_offset(opts = {})
54
+ recently_updated = opts.delete(:recently_updated) { false }
55
+
56
+ path = if recently_updated
57
+ RECENTLY_MODIFIED_COMPANIES_PATH
58
+ else
59
+ RECENTLY_CREATED_COMPANIES_PATH
60
+ end
61
+
62
+ response = Hubspot::Connection.get_json(path, opts)
63
+ response_with_offset = {}
64
+ response_with_offset['results'] = response['results'].map { |c| new(c) }
65
+ response_with_offset['hasMore'] = response['hasMore']
66
+ response_with_offset['offset'] = response['offset']
67
+ response_with_offset
68
+ end
69
+
43
70
  # Finds a list of companies by domain
44
71
  # {https://developers.hubspot.com/docs/methods/companies/search_companies_by_domain}
45
72
  # @param domain [String] company domain to search by
@@ -4,7 +4,6 @@ module Hubspot
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
6
  spec = Gem::Specification.find_by_name('hubspot-ruby')
7
- import "#{spec.gem_dir}/lib/tasks/properties.rake"
8
7
  end
9
8
  end
10
9
  end
File without changes
@@ -1,18 +1,9 @@
1
1
  require 'timecop'
2
2
 
3
3
  describe Hubspot do
4
- let(:example_blog_hash) do
5
- VCR.use_cassette("blog_list", record: :none) do
6
- url = Hubspot::Connection.send(:generate_url, Hubspot::Blog::BLOG_LIST_PATH)
7
- resp = HTTParty.get(url, format: :json)
8
- resp.parsed_response["objects"].first
9
- end
10
- end
11
- let(:created_range_params) { { created__gt: false, created__range: (Time.now..Time.now + 2.years) } }
12
-
13
4
  before do
14
5
  Hubspot.configure(hapikey: "demo")
15
- Timecop.freeze(Time.local(2012, 'Oct', 10))
6
+ Timecop.freeze(Time.utc(2012, 'Oct', 10))
16
7
  end
17
8
 
18
9
  after do
@@ -20,97 +11,140 @@ describe Hubspot do
20
11
  end
21
12
 
22
13
  describe Hubspot::Blog do
23
-
24
14
  describe ".list" do
25
- cassette "blog_list"
26
- let(:blog_list) { Hubspot::Blog.list }
15
+ it "returns a list of blogs" do
16
+ VCR.use_cassette("blog_list") do
17
+ result = Hubspot::Blog.list
27
18
 
28
- it "should have a list of blogs" do
29
- blog_list.count.should be(1)
19
+ assert_requested :get, hubspot_api_url("/content/api/v2/blogs?hapikey=demo")
20
+ expect(result).to be_kind_of(Array)
21
+ expect(result.first).to be_a(Hubspot::Blog)
22
+ end
30
23
  end
31
24
  end
32
25
 
33
26
  describe ".find_by_id" do
34
- cassette "blog_list"
27
+ it "retrieves a blog by id" do
28
+ VCR.use_cassette("blog_list") do
29
+ id = 351076997
30
+ result = Hubspot::Blog.find_by_id(id)
35
31
 
36
- it "should have a list of blogs" do
37
- blog = Hubspot::Blog.find_by_id(351076997)
38
- blog["id"].should eq(351076997)
32
+ assert_requested :get, hubspot_api_url("/content/api/v2/blogs/#{id}?hapikey=demo")
33
+ expect(result).to be_a(Hubspot::Blog)
34
+ end
39
35
  end
40
36
  end
41
37
 
42
- describe "#initialize" do
43
- subject{ Hubspot::Blog.new(example_blog_hash) }
44
- its(["name"]) { should == "API Demonstration Blog" }
45
- its(["id"]) { should == 351076997 }
46
- end
38
+ describe "#[]" do
39
+ it "returns the value for the given key" do
40
+ data = {
41
+ "id" => 123,
42
+ "name" => "Demo",
43
+ }
44
+ blog = Hubspot::Blog.new(data)
47
45
 
48
- describe "#posts" do
49
- cassette "one_month_blog_posts_filter_state"
50
- let(:blog) { Hubspot::Blog.new(example_blog_hash) }
46
+ expect(blog["id"]).to eq(data["id"])
47
+ expect(blog["name"]).to eq(data["name"])
48
+ end
51
49
 
52
- describe "can be filtered by state" do
50
+ context "when the value is unknown" do
51
+ it "returns nil" do
52
+ blog = Hubspot::Blog.new({})
53
53
 
54
- it "should filter the posts to published by default" do
55
- pending 'This test does not pass reliably'
56
- blog.posts.length.should be(14)
54
+ expect(blog["nope"]).to be_nil
57
55
  end
56
+ end
57
+ end
58
58
 
59
- it "should validate the state is a valid one" do
60
- expect { blog.posts('invalid') }.to raise_error(Hubspot::InvalidParams)
61
- end
59
+ describe "#posts" do
60
+ it "returns published blog posts created in the last 2 months" do
61
+ VCR.use_cassette("blog_posts/all_blog_posts") do
62
+ blog_id = 123
63
+ created_gt = timestamp_in_milliseconds(Time.now - 2.months)
64
+ blog = Hubspot::Blog.new({ "id" => blog_id })
65
+
66
+ result = blog.posts
62
67
 
63
- it "should allow draft posts if specified" do
64
- blog.posts({ state: false }.merge(created_range_params)).length.should be > 0
68
+ assert_requested :get, hubspot_api_url("/content/api/v2/blog-posts?content_group_id=#{blog_id}&created__gt=#{created_gt}&hapikey=demo&order_by=-created&state=PUBLISHED")
69
+ expect(result).to be_kind_of(Array)
65
70
  end
66
71
  end
67
72
 
68
- describe "can be ordered" do
69
- it "created at descending is default" do
70
- created_timestamps = blog.posts(created_range_params).map { |post| post['created'] }
71
- expect(created_timestamps.sort.reverse).to eq(created_timestamps)
72
- end
73
+ it "includes given parameters in the request" do
74
+ VCR.use_cassette("blog_posts/filter_blog_posts") do
75
+ blog_id = 123
76
+ created_gt = timestamp_in_milliseconds(Time.now - 2.months)
77
+ blog = Hubspot::Blog.new({ "id" => 123 })
78
+
79
+ result = blog.posts({ state: "DRAFT" })
73
80
 
74
- it "by created ascending" do
75
- pending
76
- created_timestamps = blog.posts({order_by: '+created'}.merge(created_range_params)).map { |post| post['created'] }
77
- expect(created_timestamps.sort).to eq(created_timestamps)
81
+ assert_requested :get, hubspot_api_url("/content/api/v2/blog-posts?content_group_id=#{blog_id}&created__gt=#{created_gt}&hapikey=demo&order_by=-created&state=DRAFT")
82
+ expect(result).to be_kind_of(Array)
78
83
  end
79
84
  end
80
85
 
81
- it "can set a page size" do
82
- pending 'Not working'
83
- blog.posts({limit: 10}.merge(created_range_params)).length.should be(10)
86
+ it "raises when given an unknown state" do
87
+ blog = Hubspot::Blog.new({})
88
+
89
+ expect {
90
+ blog.posts({ state: "unknown" })
91
+ }.to raise_error(Hubspot::InvalidParams, "State parameter was invalid")
84
92
  end
85
93
  end
86
94
  end
87
95
 
88
96
  describe Hubspot::BlogPost do
89
- cassette "blog_posts"
97
+ describe "#created_at" do
98
+ it "returns the created timestamp as a Time" do
99
+ timestamp = timestamp_in_milliseconds(Time.now)
100
+ blog_post = Hubspot::BlogPost.new({ "created" => timestamp })
90
101
 
91
- let(:example_blog_post) do
92
- VCR.use_cassette("one_month_blog_posts_filter_state", record: :none) do
93
- blog = Hubspot::Blog.new(example_blog_hash)
94
- blog.posts(created_range_params).first
102
+ expect(blog_post.created_at).to eq(Time.at(timestamp/1000))
95
103
  end
96
104
  end
97
105
 
98
- it "should have a created_at value specific method" do
99
- expect(example_blog_post.created_at).to eq(Time.at(example_blog_post['created'] / 1000))
100
- end
106
+ describe ".find_by_blog_post_id" do
107
+ it "retrieves a blog post by id" do
108
+ VCR.use_cassette "blog_posts" do
109
+ blog_post_id = 422192866
101
110
 
102
- it "can find by blog_post_id" do
103
- blog = Hubspot::BlogPost.find_by_blog_post_id(422192866)
104
- expect(blog['id']).to eq(422192866)
111
+ result = Hubspot::BlogPost.find_by_blog_post_id(blog_post_id)
112
+
113
+ assert_requested :get, hubspot_api_url("/content/api/v2/blog-posts/#{blog_post_id}?hapikey=demo")
114
+ expect(result).to be_a(Hubspot::BlogPost)
115
+ end
116
+ end
105
117
  end
106
118
 
107
- context 'containing a topic' do
108
- # 422192866 contains a topic
109
- let(:blog_with_topic) { Hubspot::BlogPost.find_by_blog_post_id(422192866) }
119
+ describe "#topics" do
120
+ it "returns the list of topics" do
121
+ VCR.use_cassette "blog_posts" do
122
+ blog_post = Hubspot::BlogPost.find_by_blog_post_id(422192866)
123
+
124
+ topics = blog_post.topics
125
+
126
+ expect(topics).to be_kind_of(Array)
127
+ expect(topics.first).to be_a(Hubspot::Topic)
128
+ end
129
+ end
130
+
131
+ context "when the blog post does not have topics" do
132
+ it "returns an empty list" do
133
+ blog_post = Hubspot::BlogPost.new({ "topic_ids" => [] })
110
134
 
111
- it "should return topic objects" do
112
- expect(blog_with_topic.topics.first.is_a?(Hubspot::Topic)).to be(true)
135
+ topics = blog_post.topics
136
+
137
+ expect(topics).to be_empty
138
+ end
113
139
  end
114
140
  end
115
141
  end
142
+
143
+ def hubspot_api_url(path)
144
+ URI.join(Hubspot::Config.base_url, path)
145
+ end
146
+
147
+ def timestamp_in_milliseconds(time)
148
+ time.to_i * 1000
149
+ end
116
150
  end
@@ -15,13 +15,13 @@ describe Hubspot::CompanyProperties do
15
15
  end
16
16
 
17
17
  let(:example_groups) do
18
- VCR.use_cassette('groups_example', record: :once) do
18
+ VCR.use_cassette('groups_example') do
19
19
  HTTParty.get('https://api.hubapi.com/companies/v2/groups?hapikey=demo').parsed_response
20
20
  end
21
21
  end
22
22
 
23
23
  let(:example_properties) do
24
- VCR.use_cassette('properties_example', record: :once) do
24
+ VCR.use_cassette('properties_example') do
25
25
  HTTParty.get('https://api.hubapi.com/companies/v2/properties?hapikey=demo').parsed_response
26
26
  end
27
27
  end
@@ -1,11 +1,11 @@
1
1
  describe Hubspot::Contact do
2
2
  let(:example_company_hash) do
3
- VCR.use_cassette("company_example", record: :none) do
3
+ VCR.use_cassette("company_example") do
4
4
  HTTParty.get("https://api.hubapi.com/companies/v2/companies/21827084?hapikey=demo").parsed_response
5
5
  end
6
6
  end
7
7
  let(:company_with_contacts_hash) do
8
- VCR.use_cassette("company_with_contacts", record: :none) do
8
+ VCR.use_cassette("company_with_contacts") do
9
9
  HTTParty.get("https://api.hubapi.com/companies/v2/companies/115200636?hapikey=demo").parsed_response
10
10
  end
11
11
  end
@@ -132,10 +132,34 @@ describe Hubspot::Contact do
132
132
  expect(last['name']).to eql 'Xge5rbdt2zm'
133
133
  end
134
134
 
135
- it 'must filter only 2 copmanies' do
135
+ it 'must filter only 2 companies' do
136
136
  copmanies = Hubspot::Company.all(count: 2)
137
137
  expect(copmanies.size).to eql 2
138
138
  end
139
+
140
+ context 'all_with_offset' do
141
+ it 'should return companies with offset and hasMore' do
142
+ response = Hubspot::Company.all_with_offset
143
+ expect(response['results'].size).to eq(20)
144
+
145
+ first = response['results'].first
146
+ last = response['results'].last
147
+
148
+ expect(first).to be_a Hubspot::Company
149
+ expect(first.vid).to eq(42866817)
150
+ expect(first['name']).to eql 'name'
151
+ expect(last).to be_a Hubspot::Company
152
+ expect(last.vid).to eql 42861017
153
+ expect(last['name']).to eql 'Xge5rbdt2zm'
154
+ end
155
+
156
+ it 'must filter only 2 companies' do
157
+ response = Hubspot::Company.all_with_offset(count: 2)
158
+ expect(response['results'].size).to eq(2)
159
+ expect(response['hasMore']).to be_truthy
160
+ expect(response['offset']).to eq(2)
161
+ end
162
+ end
139
163
  end
140
164
 
141
165
  context 'recent companies' do
@@ -1,6 +1,6 @@
1
1
  describe Hubspot::ContactList do
2
2
  let(:example_contact_list_hash) do
3
- VCR.use_cassette("contact_list_example", record: :none) do
3
+ VCR.use_cassette("contact_list_example") do
4
4
  HTTParty.get("https://api.hubapi.com/contacts/v1/lists/1?hapikey=demo").parsed_response
5
5
  end
6
6
  end
@@ -9,7 +9,7 @@ describe Hubspot::ContactList do
9
9
  let(:dynamic_list) { Hubspot::ContactList.all(dynamic: true, count: 1).first }
10
10
 
11
11
  let(:example_contact_hash) do
12
- VCR.use_cassette("contact_example", record: :none) do
12
+ VCR.use_cassette("contact_example") do
13
13
  HTTParty.get("https://api.hubapi.com/contacts/v1/contact/email/testingapis@hubspot.com/profile?hapikey=demo").parsed_response
14
14
  end
15
15
  end
@@ -15,13 +15,13 @@ describe Hubspot::ContactProperties do
15
15
  end
16
16
 
17
17
  let(:example_groups) do
18
- VCR.use_cassette('groups_example', record: :once) do
18
+ VCR.use_cassette('groups_example') do
19
19
  HTTParty.get('https://api.hubapi.com/contacts/v2/groups?hapikey=demo').parsed_response
20
20
  end
21
21
  end
22
22
 
23
23
  let(:example_properties) do
24
- VCR.use_cassette('properties_example', record: :once) do
24
+ VCR.use_cassette('properties_example') do
25
25
  HTTParty.get('https://api.hubapi.com/contacts/v2/properties?hapikey=demo').parsed_response
26
26
  end
27
27
  end