Class: HubspotInformer

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot_informer.rb,
lib/hubspot_informer/version.rb

Constant Summary collapse

HubspotInformerError =
Class.new(StandardError)
VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ HubspotInformer

Returns a new instance of HubspotInformer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hubspot_informer.rb', line 8

def initialize args
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
  @page_url ||= ''
  @request_ip ||= ''
  @hutk ||= ''
  @page_title ||= ''

  raise HubspotInformerError.new(
    "HubspotInformer API Error: Missing portal_id and/or form_guid"
    ) unless @portal_id && @form_guid

  base_url = "https://forms.hubspot.com/"
  @conn = Faraday.new(
    url:      base_url,
    ssl:      { verify: false },
    headers:  { accept: 'application/json' })
end

Instance Method Details

#hs_contextObject



40
41
42
43
44
45
46
47
48
# File 'lib/hubspot_informer.rb', line 40

def hs_context
  values = {
    'hutk' => @hutk,
    'ipAddress' => @request_ip,
    'pageUrl' => @page_url,
    'pageTitle' => @page_title
  }
  values.to_json
end

#submit(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hubspot_informer.rb', line 28

def submit params
  params[:hs_context] = hs_context
  response = @conn.post do |req|
    req.url "uploads/form/v2/#{@portal_id}/#{@form_guid}"
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    req.body = URI.encode_www_form params
  end
  unless response.status == 204
    raise HubspotInformerError.new("HubspotInformer API Error: #{response.body} (status code #{response.status})")
  end
end