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 @@
module GitlabSubscriptions
class CreateLeadService
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 }
else
{ success: false, errors: response.data&.errors }
{ success: false, errors: response[:data]&.errors }
end
end
private
def subscription_app_client
def client
Gitlab::SubscriptionPortal::Client.new
end
end
......
......@@ -4,9 +4,11 @@ module Gitlab
module SubscriptionPortal
class Client
def generate_trial(params)
parse_response(Gitlab::HTTP.post("#{base_url}/trials",
body: params.to_json,
headers: headers))
response = Gitlab::HTTP.post("#{base_url}/trials", body: params.to_json, headers: headers)
parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end
private
......@@ -25,16 +27,16 @@ module Gitlab
end
def parse_response(http_response)
response = Hashie::Mash.new(success: false)
response = { success: false }
case http_response.response
when Net::HTTPSuccess
response.success = true
response.data = JSON.parse(http_response.body) rescue nil
response[:success] = true
response[:data] = http_response.parsed_response
when Net::HTTPUnprocessableEntity
response.data = JSON.parse(http_response.body) rescue nil
response[:data] = http_response.parsed_response
else
response.data = { errors: "HTTP status code: #{http_response.code}" }
response[:data] = { errors: "HTTP status code: #{http_response.code}" }
end
response
......
......@@ -17,7 +17,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a successful status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
expect(subject.success).to eq(true)
expect(subject[:success]).to eq(true)
end
end
......@@ -27,7 +27,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a unprocessable entity status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
expect(subject.success).to eq(false)
expect(subject[:success]).to eq(false)
end
end
......@@ -37,7 +37,7 @@ describe Gitlab::SubscriptionPortal::Client do
it 'has a server error status' do
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
......
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