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

Separate HTTP request for a Hubspot::Forms object #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions lib/hubspot/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,18 @@ def fields(opts={})

# {https://developers.hubspot.com/docs/methods/forms/submit_form}
def submit(opts={})
response = Hubspot::FormsConnection.submit(SUBMIT_DATA_PATH, params: { form_guid: @guid }, body: opts)
[204, 302, 200].include?(response.code)
[204, 302, 200].include?(http_request(opts).code)
end

#
# Separate the HTTP request so the caller can optionally use
# the HTTParty object that is returned.
# That way there is access to the HTTPParty request methods
# The returned object can retrieve and record the response for compliance/other purpose:
# => Ex: |> request = hubspot_form_object.submit_http_request
# => |> request.request # Save this if neededs
def http_request(opts={})
Hubspot::FormsConnection.submit(SUBMIT_DATA_PATH, params: { form_guid: @guid }, body: opts)
end

# {https://developers.hubspot.com/docs/methods/forms/update_form}
Expand Down
45 changes: 45 additions & 0 deletions spec/lib/hubspot/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,49 @@
form.destroyed?.should be true
end
end

describe '#http_request' do
cassette 'form_submit_data'

let(:form) { Hubspot::Form.find('561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0') }

context 'with a valid portal id' do
before do
Hubspot.configure(hapikey: 'demo', portal_id: '62515')
end

it 'returns the HTTParty::Response if the form submission is successful' do
params = {}
result = form.http_request(params)
result.should be_an_instance_of HTTParty::Response
result.code.should eq 200
end
end

context 'with an invalid portal id' do
before do
Hubspot.configure(hapikey: 'demo', portal_id: 'xxxx')
end

it 'returns the HTTParty::Response in case of errors' do
params = { unknown_field: :bogus_value }
result = form.http_request(params)
result.should be_an_instance_of HTTParty::Response
result.code.should eq 404
end
end

context 'when initializing Hubspot::Form directly' do
let(:form) { Hubspot::Form.new('guid' => '561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0') }

before { Hubspot.configure(hapikey: 'demo', portal_id: '62515') }

it 'returns the HTTParty::Response if the form submission is successful' do
params = {}
result = form.http_request(params)
result.should be_an_instance_of HTTParty::Response
result.code.should eq 200
end
end
end
end