Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecations #128

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ tmp
# Sublime Text project files
*.sublime-project
*.sublime-workspace

# Emacs backup files
*~
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Before using the library, you must initialize it with your HubSpot API key. If y
initializer:

```ruby
Hubspot.configure(hapikey: "YOUR_API_KEY")
HubSpot.configure(hapikey: "YOUR_API_KEY")
```

If you have a HubSpot account, you can get your api key by logging in and visiting this url: https://app.hubspot.com/keys/get
Expand All @@ -34,27 +34,27 @@ Here's what you can do for now:
### Create a contact

```ruby
Hubspot::Contact.create!("email@address.com", {firstname: "First", lastname: "Last"})
HubSpot::Contact.create!("email@address.com", {firstname: "First", lastname: "Last"})
```

#### In batches

```ruby
Hubspot::Contact.create_or_update!([{email: 'smith@example.com', firstname: 'First', lastname: 'Last'}])
HubSpot::Contact.create_or_update!([{email: 'smith@example.com', firstname: 'First', lastname: 'Last'}])
```

### Find a contact

These methods will return a `Hubspot::Contact` object if successful, `nil` otherwise:
These methods will return a `HubSpot::Contact` object if successful, `nil` otherwise:

```ruby
Hubspot::Contact.find_by_email("email@address.com")
Hubspot::Contact.find_by_id(12345) # Pass the contact VID
HubSpot::Contact.find_by_email("email@address.com")
HubSpot::Contact.find_by_id(12345) # Pass the contact VID
```

### Update a contact

Given an instance of `Hubspot::Contact`, update its attributes with:
Given an instance of `HubSpot::Contact`, update its attributes with:

```ruby
contact.update!({firstname: "First", lastname: "Last"})
Expand All @@ -63,13 +63,13 @@ contact.update!({firstname: "First", lastname: "Last"})
#### In batches

```ruby
Hubspot::Contact.create_or_update!([{vid: '12345', firstname: 'First', lastname: 'Last'}])
HubSpot::Contact.create_or_update!([{vid: '12345', firstname: 'First', lastname: 'Last'}])
```

### Create a deal

```ruby
Hubspot::Deal.create!(nil, [company.vid], [contact.vid], pipeline: 'default', dealstage: 'initial_contact')
HubSpot::Deal.create!(nil, [company.vid], [contact.vid], pipeline: 'default', dealstage: 'initial_contact')
```

## Contributing to hubspot-ruby
Expand Down
8 changes: 5 additions & 3 deletions lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
require 'hubspot/owner'
require 'hubspot/engagement'
require 'hubspot/subscription'
require 'hubspot/deprecations'

module Hubspot

module HubSpot
def self.configure(config={})
Hubspot::Config.configure(config)
HubSpot::Config.configure(config)
end

require 'hubspot/railtie' if defined?(Rails)
require 'hubspot/railtie' if defined?(Rails)
end
24 changes: 12 additions & 12 deletions lib/hubspot/blog.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Hubspot
module HubSpot
#
# HubSpot Contacts API
#
Expand All @@ -11,17 +11,17 @@ class << self
# Lists the blogs
# {https://developers.hubspot.com/docs/methods/blogv2/get_blogs}
# No param filtering is currently implemented
# @return [Hubspot::Blog] the first 20 blogs or empty_array
# @return [HubSpot::Blog] the first 20 blogs or empty_array
def list
response = Hubspot::Connection.get_json(BLOG_LIST_PATH, {})
response = HubSpot::Connection.get_json(BLOG_LIST_PATH, {})
response['objects'].map { |b| new(b) }
end

# Finds a specific blog by its ID
# {https://developers.hubspot.com/docs/methods/blogv2/get_blogs_blog_id}
# @return Hubspot::Blog
# @return HubSpot::Blog
def find_by_id(id)
response = Hubspot::Connection.get_json(GET_BLOG_BY_ID_PATH, { blog_id: id })
response = HubSpot::Connection.get_json(GET_BLOG_BY_ID_PATH, { blog_id: id })
new(response)
end
end
Expand All @@ -41,21 +41,21 @@ def [](property)
# defaults to returning the last 2 months worth of published blog posts
# in date descending order (i.e. most recent first)
# {https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts}
# @return [Hubspot::BlogPost]
# @return [HubSpot::BlogPost]
def posts(params = {})
default_params = {
content_group_id: self["id"],
order_by: '-created',
created__gt: Time.now - 2.month,
state: 'PUBLISHED'
}
raise Hubspot::InvalidParams.new('params must be passed as a hash') unless params.is_a?(Hash)
raise HubSpot::InvalidParams.new('params must be passed as a hash') unless params.is_a?(Hash)
params = default_params.merge(params)

raise Hubspot::InvalidParams.new('State parameter was invalid') unless [false, 'PUBLISHED', 'DRAFT'].include?(params[:state])
raise HubSpot::InvalidParams.new('State parameter was invalid') unless [false, 'PUBLISHED', 'DRAFT'].include?(params[:state])
params.each { |k, v| params.delete(k) if v == false }

response = Hubspot::Connection.get_json(BLOG_POSTS_PATH, params)
response = HubSpot::Connection.get_json(BLOG_POSTS_PATH, params)
response['objects'].map { |p| BlogPost.new(p) }
end
end
Expand All @@ -65,9 +65,9 @@ class BlogPost

# Returns a specific blog post by ID
# {https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts_blog_post_id}
# @return Hubspot::BlogPost
# @return HubSpot::BlogPost
def self.find_by_blog_post_id(id)
response = Hubspot::Connection.get_json(GET_BLOG_POST_BY_ID_PATH, { blog_post_id: id })
response = HubSpot::Connection.get_json(GET_BLOG_POST_BY_ID_PATH, { blog_post_id: id })
new(response)
end

Expand All @@ -89,7 +89,7 @@ def topics
[]
else
@properties['topic_ids'].map do |topic_id|
Hubspot::Topic.find_by_topic_id(topic_id)
HubSpot::Topic.find_by_topic_id(topic_id)
end
end
end
Expand Down
49 changes: 25 additions & 24 deletions lib/hubspot/company.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Hubspot
module HubSpot
#
# HubSpot Companies API
#
Expand All @@ -23,7 +23,7 @@ class << self
# offset [Integer] for pagination
# {http://developers.hubspot.com/docs/methods/companies/get_companies_created}
# {http://developers.hubspot.com/docs/methods/companies/get_companies_modified}
# @return [Array] Array of Hubspot::Company records
# @return [Array] Array of HubSpot::Company records
def all(opts={})
recently_updated = opts.delete(:recently_updated) { false }
# limit = opts.delete(:limit) { 20 }
Expand All @@ -34,7 +34,7 @@ def all(opts={})
RECENTLY_CREATED_COMPANIES_PATH
end

response = Hubspot::Connection.get_json(path, opts)
response = HubSpot::Connection.get_json(path, opts)
response['results'].map { |c| new(c) }
end

Expand All @@ -45,12 +45,12 @@ def all(opts={})
# limit [Integer] for pagination
# properties [Array] list of company properties to recieve
# offset_company_id [Integer] for pagination (should be company ID)
# @return [Array] Array of Hubspot::Company records
# @return [Array] Array of HubSpot::Company records
def find_by_domain(domain, options = {})
raise Hubspot::InvalidParams, 'expecting String parameter' unless domain.try(:is_a?, String)
raise HubSpot::InvalidParams, 'expecting String parameter' unless domain.try(:is_a?, String)

limit = options.fetch(:limit, 100)
properties = options.fetch(:properties) { Hubspot::CompanyProperties.all.map { |property| property["name"] } }
properties = options.fetch(:properties) { HubSpot::CompanyProperties.all.map { |property| property["name"] } }
offset_company_id = options.fetch(:offset_company_id, nil)

post_data = {
Expand All @@ -66,7 +66,7 @@ def find_by_domain(domain, options = {})

companies = []
begin
response = Hubspot::Connection.post_json(GET_COMPANY_BY_DOMAIN_PATH, params: { domain: domain }, body: post_data )
response = HubSpot::Connection.post_json(GET_COMPANY_BY_DOMAIN_PATH, params: { domain: domain }, body: post_data )
companies = response["results"].try(:map) { |company| new(company) }
rescue => e
raise e unless e.message =~ /not found/ # 404 / hanle the error and kindly return an empty array
Expand All @@ -77,23 +77,23 @@ def find_by_domain(domain, options = {})
# Finds a company by domain
# {http://developers.hubspot.com/docs/methods/companies/get_company}
# @param id [Integer] company id to search by
# @return [Hubspot::Company] Company record
# @return [HubSpot::Company] Company record
def find_by_id(id)
path = GET_COMPANY_BY_ID_PATH
params = { company_id: id }
raise Hubspot::InvalidParams, 'expecting Integer parameter' unless id.try(:is_a?, Integer)
response = Hubspot::Connection.get_json(path, params)
raise HubSpot::InvalidParams, 'expecting Integer parameter' unless id.try(:is_a?, Integer)
response = HubSpot::Connection.get_json(path, params)
new(response)
end

# Creates a company with a name
# {http://developers.hubspot.com/docs/methods/companies/create_company}
# @param name [String]
# @return [Hubspot::Company] Company record
# @return [HubSpot::Company] Company record
def create!(name, params={})
params_with_name = params.stringify_keys.merge("name" => name)
post_data = {properties: Hubspot::Utils.hash_to_properties(params_with_name, key_name: "name")}
response = Hubspot::Connection.post_json(CREATE_COMPANY_PATH, params: {}, body: post_data )
post_data = {properties: HubSpot::Utils.hash_to_properties(params_with_name, key_name: "name")}
response = HubSpot::Connection.post_json(CREATE_COMPANY_PATH, params: {}, body: post_data )
new(response)
end
end
Expand All @@ -102,7 +102,7 @@ def create!(name, params={})
attr_reader :vid, :name

def initialize(response_hash)
@properties = Hubspot::Utils.properties_to_hash(response_hash["properties"])
@properties = HubSpot::Utils.properties_to_hash(response_hash["properties"])
@vid = response_hash["companyId"]
@name = @properties.try(:[], "name")
end
Expand All @@ -114,25 +114,25 @@ def [](property)
# Updates the properties of a company
# {http://developers.hubspot.com/docs/methods/companies/update_company}
# @param params [Hash] hash of properties to update
# @return [Hubspot::Company] self
# @return [HubSpot::Company] self
def update!(params)
query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: "name")}
Hubspot::Connection.put_json(UPDATE_COMPANY_PATH, params: { company_id: vid }, body: query)
query = {"properties" => HubSpot::Utils.hash_to_properties(params.stringify_keys!, key_name: "name")}
HubSpot::Connection.put_json(UPDATE_COMPANY_PATH, params: { company_id: vid }, body: query)
@properties.merge!(params)
self
end

# Adds contact to a company
# {http://developers.hubspot.com/docs/methods/companies/add_contact_to_company}
# @param id [Integer] contact id to add
# @return [Hubspot::Company] self
# @return [HubSpot::Company] self
def add_contact(contact_or_vid)
contact_vid = if contact_or_vid.is_a?(Hubspot::Contact)
contact_vid = if contact_or_vid.is_a?(HubSpot::Contact)
contact_or_vid.vid
else
contact_or_vid
end
Hubspot::Connection.put_json(ADD_CONTACT_TO_COMPANY_PATH,
HubSpot::Connection.put_json(ADD_CONTACT_TO_COMPANY_PATH,
params: {
company_id: vid,
vid: contact_vid,
Expand All @@ -145,7 +145,7 @@ def add_contact(contact_or_vid)
# {http://developers.hubspot.com/docs/methods/companies/delete_company}
# @return [TrueClass] true
def destroy!
Hubspot::Connection.delete_json(DESTROY_COMPANY_PATH, { company_id: vid })
HubSpot::Connection.delete_json(DESTROY_COMPANY_PATH, { company_id: vid })
@destroyed = true
end

Expand All @@ -155,12 +155,13 @@ def destroyed?

# Finds company contacts
# {http://developers.hubspot.com/docs/methods/companies/get_company_contacts}
# @return [Array] Array of Hubspot::Contact records
# @return [Array] Array of HubSpot::Contact records
def contacts
response = Hubspot::Connection.get_json(GET_COMPANY_CONTACTS_PATH, company_id: vid)
response = HubSpot::Connection.get_json(GET_COMPANY_CONTACTS_PATH, company_id: vid)
response['contacts'].each_with_object([]) do |contact, memo|
memo << Hubspot::Contact.find_by_id(contact['vid'])
memo << HubSpot::Contact.find_by_id(contact['vid'])
end
end
end
end

2 changes: 1 addition & 1 deletion lib/hubspot/company_properties.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Hubspot
module HubSpot
class CompanyProperties < Properties

ALL_PROPERTIES_PATH = '/companies/v2/properties'
Expand Down
4 changes: 2 additions & 2 deletions lib/hubspot/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'logger'

module Hubspot
module HubSpot
class Config

CONFIG_KEYS = [:hapikey, :base_url, :portal_id, :logger]
Expand All @@ -27,7 +27,7 @@ def reset!

def ensure!(*params)
params.each do |p|
raise Hubspot::ConfigurationError.new("'#{p}' not configured") unless instance_variable_get "@#{p}"
raise HubSpot::ConfigurationError.new("'#{p}' not configured") unless instance_variable_get "@#{p}"
end
end
end
Expand Down
Loading