Commit ee8717a9 authored by Rubén Dávila's avatar Rubén Dávila

Address suggestions from last code review

Subscription Portal client and Create Lead service were refactored
parent 1cf4977d
...@@ -3,18 +3,18 @@ ...@@ -3,18 +3,18 @@
module GitlabSubscriptions module GitlabSubscriptions
class CreateLeadService class CreateLeadService
def execute(company_params) def execute(company_params)
response = subscription_app_client.generate_trial(company_params) response = client.generate_trial(company_params)
if response.success if response[:success]
{ success: true } { success: true }
else else
{ success: false, errors: response.data&.errors } { success: false, errors: response[:data]&.errors }
end end
end end
private private
def subscription_app_client def client
Gitlab::SubscriptionPortal::Client.new Gitlab::SubscriptionPortal::Client.new
end end
end end
......
...@@ -4,9 +4,11 @@ module Gitlab ...@@ -4,9 +4,11 @@ module Gitlab
module SubscriptionPortal module SubscriptionPortal
class Client class Client
def generate_trial(params) def generate_trial(params)
parse_response(Gitlab::HTTP.post("#{base_url}/trials", response = Gitlab::HTTP.post("#{base_url}/trials", body: params.to_json, headers: headers)
body: params.to_json,
headers: headers)) parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end end
private private
...@@ -25,16 +27,16 @@ module Gitlab ...@@ -25,16 +27,16 @@ module Gitlab
end end
def parse_response(http_response) def parse_response(http_response)
response = Hashie::Mash.new(success: false) response = { success: false }
case http_response.response case http_response.response
when Net::HTTPSuccess when Net::HTTPSuccess
response.success = true response[:success] = true
response.data = JSON.parse(http_response.body) rescue nil response[:data] = http_response.parsed_response
when Net::HTTPUnprocessableEntity when Net::HTTPUnprocessableEntity
response.data = JSON.parse(http_response.body) rescue nil response[:data] = http_response.parsed_response
else else
response.data = { errors: "HTTP status code: #{http_response.code}" } response[:data] = { errors: "HTTP status code: #{http_response.code}" }
end end
response response
......
...@@ -17,7 +17,7 @@ describe Gitlab::SubscriptionPortal::Client do ...@@ -17,7 +17,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a successful status' do it 'has a successful status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
expect(subject.success).to eq(true) expect(subject[:success]).to eq(true)
end end
end end
...@@ -27,7 +27,7 @@ describe Gitlab::SubscriptionPortal::Client do ...@@ -27,7 +27,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a unprocessable entity status' do it 'has a unprocessable entity status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
expect(subject.success).to eq(false) expect(subject[:success]).to eq(false)
end end
end end
...@@ -37,7 +37,7 @@ describe Gitlab::SubscriptionPortal::Client do ...@@ -37,7 +37,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a server error status' do it 'has a server error status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
expect(subject.success).to eq(false) expect(subject[:success]).to eq(false)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment