Module: HubspotV3

Defined in:
lib/hubspot_v3.rb,
lib/hubspot_v3/config.rb,
lib/hubspot_v3/helpers.rb,
lib/hubspot_v3/version.rb,
lib/hubspot_v3/mock_contract.rb

Defined Under Namespace

Modules: Helpers, MockContract Classes: Config, RequestFailedError

Constant Summary collapse

HubspotRequestLimitReached =
Class.new(RequestFailedError)
API_URL =
'https://api.hubapi.com'
CONTACTS_SEARCH =
'/crm/v3/objects/contacts/search'
CONTACTS_CREATE =
'/crm/v3/objects/contacts/batch/create'
CONTACTS_UPDATE =
'/crm/v3/objects/contacts/batch/update'
COMPANIES_SEARCH =
'/crm/v3/objects/companies/search'
COMPANIES_CREATE =
'/crm/v3/objects/companies/batch/create'
COMPANIES_UPDATE =
'/crm/v3/objects/companies/batch/update'
VERSION =
"1.1.0.1"

Class Method Summary collapse

Class Method Details

._hubspot_request_failed_error(httparty_response) ⇒ Object



131
132
133
134
135
# File 'lib/hubspot_v3.rb', line 131

def self._hubspot_request_failed_error(httparty_response)
  code = httparty_response.code
  message = httparty_response.parsed_response['message']
  HubspotV3::RequestFailedError.new("#{code} - #{message}", httparty_response)
end

._hubspot_request_limit_reached_error(httparty_response) ⇒ Object



125
126
127
128
129
# File 'lib/hubspot_v3.rb', line 125

def self._hubspot_request_limit_reached_error(httparty_response)
  code = httparty_response.code
  message = httparty_response.parsed_response['message']
  HubspotV3::HubspotRequestLimitReached.new("#{code} - #{message}", httparty_response)
end

.companies_create(bodyhash) ⇒ Object



61
62
63
# File 'lib/hubspot_v3.rb', line 61

def self.companies_create(bodyhash)
  post(COMPANIES_CREATE, bodyhash)
end

.companies_search(bodyhash) ⇒ Object



69
70
71
# File 'lib/hubspot_v3.rb', line 69

def self.companies_search(bodyhash)
  post(COMPANIES_SEARCH, bodyhash)
end

.companies_search_by_ids(hubspot_object_ids_ary) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hubspot_v3.rb', line 73

def self.companies_search_by_ids(hubspot_object_ids_ary)
  filters_group_ary = hubspot_object_ids_ary.map do |e|
    {
      "filters": [
        {
          "propertyName": "hs_object_id",
          "operator": "EQ",
          "value": e
        }
      ]
    }
  end

  bodyhash = { "filterGroups": filters_group_ary }
  companies_search(bodyhash)
end

.companies_update(bodyhash) ⇒ Object



65
66
67
# File 'lib/hubspot_v3.rb', line 65

def self.companies_update(bodyhash)
  post(COMPANIES_UPDATE, bodyhash)
end

.configObject



16
17
18
# File 'lib/hubspot_v3/config.rb', line 16

def self.config
  @config ||= Config.new
end

.contacts_create(bodyhash) ⇒ Object



28
29
30
# File 'lib/hubspot_v3.rb', line 28

def self.contacts_create(bodyhash)
  post(CONTACTS_CREATE, bodyhash)
end

.contacts_search(bodyhash) ⇒ Object



36
37
38
# File 'lib/hubspot_v3.rb', line 36

def self.contacts_search(bodyhash)
  post(CONTACTS_SEARCH, bodyhash)
end

.contacts_search_by_emails(emails_ary) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hubspot_v3.rb', line 40

def self.contacts_search_by_emails(emails_ary)
  filters_group_ary = emails_ary.map do |e|
    {
      "filters": [
        {
          "propertyName": "email",
          "operator": "EQ",
          "value": e
        }
      ]
    }
  end

  bodyhash = { "filterGroups": filters_group_ary }
  contacts_search(bodyhash)
end

.contacts_search_by_emails_mapped(emails_ary) ⇒ Object



57
58
59
# File 'lib/hubspot_v3.rb', line 57

def self.contacts_search_by_emails_mapped(emails_ary)
  HubspotV3::Helpers.map_search_by_email(contacts_search_by_emails(emails_ary))
end

.contacts_update(bodyhash) ⇒ Object



32
33
34
# File 'lib/hubspot_v3.rb', line 32

def self.contacts_update(bodyhash)
  post(CONTACTS_UPDATE, bodyhash)
end

.headersObject



118
119
120
121
122
123
# File 'lib/hubspot_v3.rb', line 118

def self.headers
  {
    'Content-Type' => 'application/json',
    'Authorization': "Bearer #{config.token}"
  }
end

.post(path, bodyhash) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hubspot_v3.rb', line 94

def self.post(path, bodyhash)
  httparty_response = HTTParty.post(url(path), {
    body: bodyhash.to_json,
    headers: headers
  })
  case httparty_response.code
  when 200, 201
    httparty_response.parsed_response['results']
  when 429
    # Hubspot error 429 - You have reached your secondly limit.
    raise _hubspot_request_limit_reached_error(httparty_response)
  when 500
    if httparty_response.parsed_response["category"] == "RATE_LIMITS"
      # e.g.: {"status":"error","message":"You have reached your secondly limit.","category":"RATE_LIMITS"}
      # Yes, Hubspot will sometimes give 429 or 500 when limit reached
      raise _hubspot_request_limit_reached_error(httparty_response)
    else
      raise _hubspot_request_failed_error(httparty_response)
    end
  else
    raise _hubspot_request_failed_error(httparty_response)
  end
end

.url(path) ⇒ Object



90
91
92
# File 'lib/hubspot_v3.rb', line 90

def self.url(path)
  "#{API_URL}#{path}"
end