Module: HubspotV3::MockContract

Extended by:
MockContract
Included in:
MockContract
Defined in:
lib/hubspot_v3/mock_contract.rb

Instance Method Summary collapse

Instance Method Details

#_calculate_id(whatever) ⇒ Object



181
182
183
# File 'lib/hubspot_v3/mock_contract.rb', line 181

def _calculate_id(whatever)
  whatever.bytes.sum  # sum of asci values of the email string
end

#_fetch_input_id(input) ⇒ Object



198
199
200
# File 'lib/hubspot_v3/mock_contract.rb', line 198

def _fetch_input_id(input)
  input.fetch('id') { raise KeyError.new("Item in Inputs hash must contain key 'id' - hash['inputs'][0]['id']") }
end

#_fetch_input_properties(input) ⇒ Object



194
195
196
# File 'lib/hubspot_v3/mock_contract.rb', line 194

def _fetch_input_properties(input)
  input.fetch('properties') { raise KeyError.new("Item in Inputs hash must contain key 'properties' - hash['inputs'][0]['properties']") }
end

#_fetch_inputs(bodyhash) ⇒ Object



190
191
192
# File 'lib/hubspot_v3/mock_contract.rb', line 190

def _fetch_inputs(bodyhash)
  bodyhash.fetch('inputs') { raise KeyError.new("Inputs hash must contain key 'inputs' - hash['inputs']") }
end

#_general_batch_search_should_be_overridden_msg(name) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/hubspot_v3/mock_contract.rb', line 202

def _general_batch_search_should_be_overridden_msg(name)
  "General search queries are not covered by test contract and should be customly" +
  "\noverridden based on your usecase.  E.g.:" +
  "\n    expect(HubspotV3::MockContract)" +
  "\n      .to receive(:#{name})" +
  "\n      .and_return([{'id'=>'12345', 'properties'=>{  }])\n\n"
end

#_sanitize_email_as_hubspot_would(email) ⇒ Object



185
186
187
188
# File 'lib/hubspot_v3/mock_contract.rb', line 185

def _sanitize_email_as_hubspot_would(email)
  # Hubspot downcase all emails e.g. [email protected] would be [email protected]
  email.downcase.strip
end

#companies_create(bodyhash) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hubspot_v3/mock_contract.rb', line 91

def companies_create(bodyhash)
  inputs = _fetch_inputs(bodyhash)
  inputs.map do |input|
    properties = _fetch_input_properties(input)

    #note: Hubspot API doesn't really require name, but for sake of this
    # contract functionality we need properties.name
    name = properties.fetch('name') { raise KeyError.new("Item in Inputs hash must contain key 'properties.name' - hash['inputs'][0]['properties']['name']") }

    id = _calculate_id(name) + 1_000_000

    default_properties = {
      "createdate"=>"2022-09-13T15:06:03.116Z",
      "hs_lastmodifieddate"=>"2022-09-13T15:06:03.116Z",
      "hs_object_id"=>id.to_s,
      "hs_pipeline"=>"companies-lifecycle-pipeline",
      "lifecyclestage"=>"lead",
      "name"=>name
    }

    properties = default_properties.merge(properties)

    {
      "id"=>id.to_s,
      "properties"=> properties,
      "createdAt"=>"2022-09-13T15:06:03.116Z",
      "updatedAt"=>"2022-09-13T15:06:03.116Z",
      "archived"=>false
    }
  end
end

#companies_searchObject



176
177
178
179
# File 'lib/hubspot_v3/mock_contract.rb', line 176

def companies_search(*)
  puts _general_batch_search_should_be_overridden_msg('companies_search')
  raise "HubspotV3::MockContract.companies_search should be stubbed or overridden"
end

#companies_search_by_ids(ids) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hubspot_v3/mock_contract.rb', line 149

def companies_search_by_ids(ids)
  raise 'argument must be an Array' unless ids.is_a?(Array)
  raise 'Array must include only String ids' if ids.select { |x| ! x.is_a?(String) }.any?

  resp = ids.map do |id|
    if id.match(/666666/)
      # this represents not found records
      nil
    else
      {
        "id"=>id,
        "properties"=>{
          "createdate"=>"2022-09-13T15:06:03.116Z",
          "domain"=>nil,
          "hs_lastmodifieddate"=>"2022-09-13T15:20:48.331Z",
          "hs_object_id"=>id,
          "name"=>"ACME Company #{id}"},
        "createdAt"=>"2022-09-13T15:06:03.116Z",
        "updatedAt"=>"2022-09-13T15:20:48.331Z",
        "archived"=>false
      }
    end
  end

  resp.compact
end

#companies_update(bodyhash) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hubspot_v3/mock_contract.rb', line 123

def companies_update(bodyhash)
  inputs = _fetch_inputs(bodyhash)

  inputs.map do |input|
    id = _fetch_input_id(input)
    properties = _fetch_input_properties(input)

    default_properties = {
      "hs_lastmodifieddate"=>"2022-09-13T15:06:33.116Z",
      "createdate"=>"2022-09-13T15:06:03.116Z",
      "hs_object_id"=>id.to_s,
      "hs_pipeline"=>"companies-lifecycle-pipeline",
      "lifecyclestage"=>"lead"
    }
    properties = default_properties.merge(properties)

    {
      "id"=>id.to_s,
      "properties"=>properties,
      "createdAt"=>"2022-09-13T15:06:03.116Z",
      "updatedAt"=>"2022-09-13T15:06:33.116Z",
      "archived"=>false
    }
  end
end

#contacts_create(bodyhash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hubspot_v3/mock_contract.rb', line 22

def contacts_create(bodyhash)
  inputs = _fetch_inputs(bodyhash)

  inputs.map do |input|
    properties = _fetch_input_properties(input)

    email = properties.fetch('email') { raise KeyError.new("Item in Inputs hash must contain key 'properties.email' - hash['inputs'][0]['properties']['email']") }
    id = _calculate_id(email) + 1_000_000

    sanitized_email = _sanitize_email_as_hubspot_would(email) # sanitize after id was generated

    default_properties = {
      "createdate"=>"2021-10-18T15:12:14.245Z",
      "email"=>sanitized_email,
      "hs_all_contact_vids"=>"3451",
      "hs_email_domain"=>sanitized_email.split('@').last,
      "hs_is_unworked"=>"true",
      "hs_object_id"=>id,
      "hs_pipeline"=>"contacts-lifecycle-pipeline",
      "lastmodifieddate"=>"2021-10-18T15:12:14.245Z",
    }

    properties = default_properties.merge(properties)

    {
      "id"=>id,
      "properties"=> properties,
      "createdAt"=>"2021-10-18T15:12:14.245Z",
      "updatedAt"=>"2021-10-18T15:12:14.245Z",
      "archived"=>false
    }
  end
end

#contacts_search(bodyhash) ⇒ Object



86
87
88
89
# File 'lib/hubspot_v3/mock_contract.rb', line 86

def contacts_search(bodyhash)
  puts _general_batch_search_should_be_overridden_msg('contacts_search')
  raise "HubspotV3::MockContract.contacts_search should be stubbed or overridden"
end

#contacts_search_by_emails(emails) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hubspot_v3/mock_contract.rb', line 56

def contacts_search_by_emails(emails)
  emails = emails.reject { |e| e.match?(/notfound/)}

  emails.map do |email|
    id = _calculate_id(email)
    first_name = email.split('@').first
    last_name  = email.split('@').last
    sanitized_email = _sanitize_email_as_hubspot_would(email) # sanitize after id was generated

    {
      "id"=>id,
      "createdAt"=>"2020-09-10T10:29:54.714Z",
      "updatedAt"=>"2021-10-13T10:16:19.015Z",
      "archived"=>false,
      "properties"=> {
        "createdate"=>"2020-09-10T10:29:54.714Z",
        "email"=>sanitized_email,
        "firstname"=>first_name,
        "hs_object_id"=>id,
        "lastmodifieddate"=>"2021-10-13T10:16:19.015Z",
        "lastname"=>last_name
      }
    }
  end
end

#contacts_search_by_emails_mapped(emails_ary) ⇒ Object



82
83
84
# File 'lib/hubspot_v3/mock_contract.rb', line 82

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

#contacts_update(bodyhash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hubspot_v3/mock_contract.rb', line 5

def contacts_update(bodyhash)
  inputs = _fetch_inputs(bodyhash)

  inputs.map do |input|
    id = _fetch_input_id(input)
    properties = _fetch_input_properties(input)

    {
      "id"=>id,
      "properties"=>properties,
      "createdAt"=>"2021-10-18T15:12:14.245Z",
      "updatedAt"=>"2021-10-18T15:12:16.539Z",
      "archived"=>false
    }
  end
end