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

Feature/adding multi tenant support #19

Merged
Merged
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
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: RSpec with SimpleCov
on:
pull_request:
pull_request_target:
types:
- opened
- edited
push:
branches:
- main
Expand Down Expand Up @@ -29,4 +32,4 @@ jobs:
- name: Publish code coverage
run: |
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r ${{secrets.CC_TEST_REPORTER_ID}}
./cc-test-reporter after-build -r ${{secrets.CC_TEST_REPORTER_ID}}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## [Official Release]
- [1.0.2] - 2024-09-05 https://github.com/oroth8/easy_hubspot/pull/19
- Adding `access_token` to class methods to allow for overriding the initializer
- [1.0.0] - 2023-02-22 https://github.com/oroth8/easy_hubspot/pull/10

## [Unreleased]
Expand Down
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,14 @@ Please refrence the [hubspot docs](https://developers.hubspot.com/docs/api/crm/l
EasyHubspot::LineItem.delete_line_item(123)
```

## Error Handling
## Multiple Access Tokens

If you need to choose your access token at call time, you can pass the access token as an argument to class methods as
follows:

```ruby
def call
begin
EasyHubspot::Contact.create_contact(body)
rescue EasyHubspot::HubspotApiError => e
# handle error code
# e.message = 'Contact already exists. Existing ID: 801'
Rails.logger.info(e.message)
end
end
# Finds the contact using the access token provided at call time instead of the one set during initialization
EasyHubspot::Contact.get_contact(123, different_access_token)
```

## Development
Expand Down
4 changes: 2 additions & 2 deletions lib/easy_hubspot/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module EasyHubspot
# class EasyHubspot::Base
class Base
class << self
def headers
def headers(access_token = nil)
{ "Content-Type" => 'application/json',
"Authorization" => "Bearer #{EasyHubspot.configuration.access_token}" }
"Authorization" => "Bearer #{access_token || EasyHubspot.configuration.access_token}" }
end

def email?(string)
Expand Down
2 changes: 2 additions & 0 deletions lib/easy_hubspot/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'pry'

module EasyHubspot
# EasyHubspot::Client
class Client
Expand Down
28 changes: 14 additions & 14 deletions lib/easy_hubspot/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ class Contact < EasyHubspot::Base
class << self
CONTACT_ENDPOINT = 'crm/v3/objects/contacts'

def get_contact(contact_id)
Client.do_get(determine_endpoint(contact_id), headers)
def get_contact(contact_id, access_token = nil)
Client.do_get(determine_endpoint(contact_id), headers(access_token))
end

def get_contacts
Client.do_get(CONTACT_ENDPOINT, headers)
def get_contacts(access_token = nil)
Client.do_get(CONTACT_ENDPOINT, headers(access_token))
end

def create_contact(body)
Client.do_post(CONTACT_ENDPOINT, body, headers)
def create_contact(body, access_token = nil)
Client.do_post(CONTACT_ENDPOINT, body, headers(access_token))
end

def update_contact(contact_id, body)
Client.do_patch(determine_endpoint(contact_id), body, headers)
def update_contact(contact_id, body, access_token = nil)
Client.do_patch(determine_endpoint(contact_id), body, headers(access_token))
end

def delete_contact(contact_id)
Client.do_delete(determine_endpoint(contact_id), headers)
def delete_contact(contact_id, access_token = nil)
Client.do_delete(determine_endpoint(contact_id), headers(access_token))
end

def update_or_create_contact(email, body)
res = get_contact(email)
def update_or_create_contact(email, body, access_token = nil)
res = get_contact(email, access_token)
if res && res[:id]
update_contact(email, body)
update_contact(email, body, access_token)
else
create_contact(body)
create_contact(body, access_token)
end
end

Expand Down
20 changes: 10 additions & 10 deletions lib/easy_hubspot/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class Deal < EasyHubspot::Base
class << self
DEAL_ENDPOINT = 'crm/v3/objects/deals'

def get_deal(deal_id)
Client.do_get(deal_id_endpoint(deal_id), headers)
def get_deal(deal_id, access_token = nil)
Client.do_get(deal_id_endpoint(deal_id), headers(access_token))
end

def get_deals
Client.do_get(DEAL_ENDPOINT, headers)
def get_deals(access_token = nil)
Client.do_get(DEAL_ENDPOINT, headers(access_token))
end

def create_deal(body)
Client.do_post(DEAL_ENDPOINT, body, headers)
def create_deal(body, access_token = nil)
Client.do_post(DEAL_ENDPOINT, body, headers(access_token))
end

def update_deal(deal_id, body)
Client.do_patch(deal_id_endpoint(deal_id), body, headers)
def update_deal(deal_id, body, access_token = nil)
Client.do_patch(deal_id_endpoint(deal_id), body, headers(access_token))
end

def delete_deal(deal_id)
Client.do_delete(deal_id_endpoint(deal_id), headers)
def delete_deal(deal_id, access_token = nil)
Client.do_delete(deal_id_endpoint(deal_id), headers(access_token))
end

private
Expand Down
20 changes: 10 additions & 10 deletions lib/easy_hubspot/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class LineItem < EasyHubspot::Base
class << self
LINE_ITEM_ENDPOINT = 'crm/v3/objects/line_items'

def get_line_item(line_item_id)
Client.do_get(line_item_id_endpoint(line_item_id), headers)
def get_line_item(line_item_id, access_token = nil)
Client.do_get(line_item_id_endpoint(line_item_id), headers(access_token))
end

def get_line_items
Client.do_get(LINE_ITEM_ENDPOINT, headers)
def get_line_items(access_token = nil)
Client.do_get(LINE_ITEM_ENDPOINT, headers(access_token))
end

def create_line_item(body)
Client.do_post(LINE_ITEM_ENDPOINT, body, headers)
def create_line_item(body, access_token = nil)
Client.do_post(LINE_ITEM_ENDPOINT, body, headers(access_token))
end

def update_line_item(line_item_id, body)
Client.do_patch(line_item_id_endpoint(line_item_id), body, headers)
def update_line_item(line_item_id, body, access_token = nil)
Client.do_patch(line_item_id_endpoint(line_item_id), body, headers(access_token))
end

def delete_line_item(line_item_id)
Client.do_delete(line_item_id_endpoint(line_item_id), headers)
def delete_line_item(line_item_id, access_token = nil)
Client.do_delete(line_item_id_endpoint(line_item_id), headers(access_token))
end

private
Expand Down
20 changes: 10 additions & 10 deletions lib/easy_hubspot/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class Product < EasyHubspot::Base
class << self
PRODUCT_ENDPOINT = 'crm/v3/objects/products'

def get_product(product_id)
Client.do_get(product_id_endpoint(product_id), headers)
def get_product(product_id, access_token = nil)
Client.do_get(product_id_endpoint(product_id), headers(access_token))
end

def get_products
Client.do_get(PRODUCT_ENDPOINT, headers)
def get_products(access_token = nil)
Client.do_get(PRODUCT_ENDPOINT, headers(access_token))
end

def create_product(body)
Client.do_post(PRODUCT_ENDPOINT, body, headers)
def create_product(body, access_token = nil)
Client.do_post(PRODUCT_ENDPOINT, body, headers(access_token))
end

def update_product(product_id, body)
Client.do_patch(product_id_endpoint(product_id), body, headers)
def update_product(product_id, body, access_token = nil)
Client.do_patch(product_id_endpoint(product_id), body, headers(access_token))
end

def delete_product(product_id)
Client.do_delete(product_id_endpoint(product_id), headers)
def delete_product(product_id, access_token = nil)
Client.do_delete(product_id_endpoint(product_id), headers(access_token))
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/easy_hubspot/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module EasyHubspot
VERSION = '1.0.1'
VERSION = '1.0.2'
end
36 changes: 36 additions & 0 deletions spec/easy_hubspot/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe EasyHubspot::Base do
global_access_token = 'YOUR-GLOBAL-ACCESS-TOKEN'

before :all do
EasyHubspot.config do |config|
config.access_token = global_access_token
end
end

describe '#headers' do
context 'when using the global access_token' do
let(:expected_headers) do
{ 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{global_access_token}" }
end

it 'returns the expected Authorization Header' do
expect(described_class.headers).to eq(expected_headers)
end
end

context 'when overriding the global access_token' do
let(:custom_access_token) { 'CUSTOM-PER-CALL-ACCESS-TOKEN' }
let(:expected_headers) do
{ 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{custom_access_token}" }
end

it 'returns the expected Authorization Header' do
expect(described_class.headers(custom_access_token)).to eq(expected_headers)
end
end
end
end
Loading
Loading