Skip to content

Commit

Permalink
Merge pull request #2 from hinthealth/feature/oauth
Browse files Browse the repository at this point in the history
Added an OAuth class for refreshing/creating.
  • Loading branch information
blakewest committed Mar 31, 2017
2 parents 3a8a09d + 15d8c78 commit 17ccbb0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'hubspot/deal_properties'
require 'hubspot/owner'
require 'hubspot/engagement'
require 'hubspot/oauth'

module Hubspot
def self.configure(config={})
Expand Down
9 changes: 8 additions & 1 deletion lib/hubspot/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
module Hubspot
class Config

CONFIG_KEYS = [:hapikey, :base_url, :portal_id, :logger, :access_token]
CONFIG_KEYS = [
:hapikey, :base_url, :portal_id, :logger, :access_token, :client_id,
:client_secret, :redirect_uri
]
DEFAULT_LOGGER = Logger.new('/dev/null')

class << self
Expand All @@ -17,6 +20,10 @@ def configure(config)
@portal_id = config["portal_id"]
@logger = config["logger"] || DEFAULT_LOGGER
@access_token = config["access_token"]
@client_id = config["client_id"] if config["client_id"].present?
@client_secret = config["client_secret"] if config["client_secret"].present?
@redirect_uri = config["redirect_uri"] if config["redirect_uri"].present?

unless access_token.present? ^ hapikey.present?
Hubspot::ConfigurationError.new("You must provide either an access_token or an hapikey")
end
Expand Down
46 changes: 46 additions & 0 deletions lib/hubspot/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'httparty'
module Hubspot
class OAuth < Connection
include HTTParty
DEFAULT_OAUTH_HEADERS = {"Content-Type" => "application/x-www-form-urlencoded;charset=utf-8"}
class << self
def refresh(params)
params.stringify_keys!
no_parse = params.delete("no_parse") { false }
body = {
client_id: params["client_id"] || Hubspot::Config.client_id,
grant_type: "refresh_token",
client_secret: params["client_secret"] || Hubspot::Config.client_secret,
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri,
refresh_token: params["refresh_token"]
}
response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS)
log_request_and_response oauth_url, response, body
raise(Hubspot::RequestError.new(response)) unless response.success?

no_parse ? response : response.parsed_response
end

def create(params)
params.stringify_keys!
no_parse = params.delete("no_parse") { false }
body = {
client_id: params["client_id"] || Hubspot::Config.client_id,
grant_type: "authorization_code",
client_secret: params["client_secret"] || Hubspot::Config.client_secret,
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri,
code: params["code"]
}
response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS)
log_request_and_response oauth_url, response, body
raise(Hubspot::RequestError.new(response)) unless response.success?

no_parse ? response : response.parsed_response
end

def oauth_url
oauth_url = Hubspot::Config.base_url + "/oauth/v1/token"
end
end
end
end

0 comments on commit 17ccbb0

Please sign in to comment.