Class: Mautic::FormHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/mautic/form_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, request = nil) ⇒ FormHelper

Returns a new instance of FormHelper.



15
16
17
18
19
20
# File 'lib/mautic/form_helper.rb', line 15

def initialize(url, request = nil)
  @url = url

  @host = request&.host
  @forward_ip = request&.remote_ip
end

Instance Attribute Details

#dataObject

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'lib/mautic/form_helper.rb', line 30

def data
  raise ArgumentError if @data.nil?
  defaults = {
    'submit' => '1',
    'return' => host,
    'domain' => host
  }
  defaults.merge(@data.to_h).inject({}){|mem, (name, value)| mem["mauticform[#{name}]"] = value; mem}
end

#forward_ipObject

Returns the value of attribute forward_ip.



12
13
14
# File 'lib/mautic/form_helper.rb', line 12

def forward_ip
  @forward_ip
end

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/mautic/form_helper.rb', line 11

def host
  @host
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/mautic/form_helper.rb', line 11

def url
  @url
end

Class Method Details

.submit(url: nil, form: nil, request: nil, &block) ⇒ Object

shortcut



6
7
8
9
# File 'lib/mautic/form_helper.rb', line 6

def self.submit(url: nil, form: nil, request: nil, &block)
  m = new(url || Mautic.config.mautic_url, request)
  m.send_data form, &block
end

Instance Method Details

#send_data(form_id) {|@collector| ... } ⇒ Object

Yields:

  • (@collector)


22
23
24
25
26
27
28
# File 'lib/mautic/form_helper.rb', line 22

def send_data(form_id, &block)
  @collector = OpenStruct.new(formId: form_id)
  yield @collector
  self.data = @collector.to_h

  push
end

#submitObject Also known as: push



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mautic/form_helper.rb', line 40

def submit
  uri = URI.parse(url)
  uri.path = '/form/submit'
  headers = {}
  headers.store 'X-Forwarded-For', forward_ip if forward_ip
  begin
    @response = RestClient.post uri.to_s, data, headers
  rescue RestClient::Found => e
    @response = e
  end

end