Skip to content

Commit

Permalink
Merge pull request #2 from rafasimao/master
Browse files Browse the repository at this point in the history
Add campaigns method to client and refactors connection requests.
  • Loading branch information
rafasimao committed Sep 14, 2017
2 parents 29f05c9 + 5d9f005 commit 0c88765
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 62 deletions.
13 changes: 3 additions & 10 deletions lib/get_response_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ def initialize(api_key)
end

def account
response = @connection.get('/accounts').parsed_response

if error?(response) && response['message']
return response['message']
end
response
@connection.request(:get, '/accounts')
end

private

def error?(response)
response['httpStatus']
def campaigns
@connection.request(:get, '/campaigns')
end
end
end
30 changes: 17 additions & 13 deletions lib/get_response_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@

module GetResponseApi
class Connection
API_ENDPOINT = 'https://api.getresponse.com/v3'
API_ENDPOINT = 'https://api.getresponse.com/v3'.freeze
TIMEOUT = 7

def initialize(api_key)
@api_key = api_key
end

def post(path, body: {}, headers: {})
headers.merge!(auth)
HTTParty.post(
"#{API_ENDPOINT}#{path}",
body: body.to_json,
headers: headers,
timeout: TIMEOUT
)
def request(method, path)
response = http_request(method, path).parsed_response

if error?(response) && response['message']
return response['message']
end
response
end

def get(path, body: {}, headers: {})
private

def http_request(request, path, body: {}, headers: {})
headers.merge!(auth)
HTTParty.get(
HTTParty.public_send(
request,
"#{API_ENDPOINT}#{path}",
body: body.to_json,
headers: headers,
timeout: TIMEOUT
)
end

private

def auth
{
'X-Auth-Token' => "api-key #{@api_key}",
'Content-Type' => 'application/json'
}
end

def error?(response)
response.is_a?(Hash) && response['httpStatus']
end
end
end
2 changes: 1 addition & 1 deletion lib/get_response_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GetResponseApi
VERSION = '0.1.0'
VERSION = '0.2.0'
end
68 changes: 55 additions & 13 deletions spec/get_response_api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,85 @@
require 'webmock/rspec'

RSpec.describe GetResponseApi::Client do
let(:header) { {'Content-Type': 'application/json'} }
let(:header) { {'Content-Type' => 'application/json'} }
let(:url) { 'https://api.getresponse.com/v3' }
let(:client) { described_class.new('1236547hjuh') }

describe '#account' do
before do
WebMock.stub_request(:get, "#{url}/accounts")
.to_return(body: response, headers: header)
.to_return(body: response, headers: header)
end

context 'when the request is valid' do
let(:response) { result.to_json }
let(:result) do
{
"accountId"=>"pdIhh",
"firstName"=>"Jane",
"lastName"=>"Doe",
"email"=>"jane.doe@test.com"
}
'accountId' => 'pdIhh',
'firstName' => 'Jane',
'lastName' => 'Doe',
'email' => 'jane.doe@test.com'
}
end
let(:response) { result.to_json }

subject { client.account }

it { is_expected.to eq(result) }
end

context 'when the request is invalid' do
let(:error_message) do
'Unable to authenticate request. ' \
'Check credentials or authentication method details'
end
let(:response) do
{
'httpStatus' => 401,
'message' => error_message
}.to_json
end

subject { client.account }

it { is_expected.to eq(error_message) }
end
end

describe '#campaigns' do
before do
WebMock.stub_request(:get, "#{url}/campaigns")
.to_return(body: response, headers: header)
end

context 'when the request is valid' do
let(:result) do
[{
"campaignId" => "B",
"name" => "TestCampaigns",
"description" => "New test campaign",
"isDefault" => "true",
}]
end
let(:response) { result.to_json }

subject{ client.account() }
subject { client.campaigns }

it { is_expected.to eq(result) }
end

context 'when the request is invalid' do
let(:error_message) do
'Unable to authenticate request. Check credentials or authentication method details'
'Unable to authenticate request. ' \
'Check credentials or authentication method details'
end
let(:response) do
{
"httpStatus"=>401,
"message"=>error_message
'httpStatus' => 401,
'message' => error_message
}.to_json
end

subject{ client.account() }
subject { client.campaigns }

it { is_expected.to eq(error_message) }
end
Expand Down
75 changes: 50 additions & 25 deletions spec/get_response_api/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:apikey) { 'apikey123456' }
let(:connection) { described_class.new(apikey) }

describe '#post' do
describe '#request' do
let(:url) { 'https://api.getresponse.com/v3' }
let(:path) { '/accounts' }
let(:body) { {} }
Expand All @@ -15,38 +15,63 @@
}
end
let(:timeout) { 7 }
let(:response) { {} }

before do
allow(HTTParty).to receive(:post)
connection.post(path)
end
describe 'when making a GET request' do
before do
allow(response).to receive(:parsed_response).and_return({})
allow(HTTParty).to receive(:get).and_return(response)
connection.request(:get, path)
end

it 'should call post with valid params' do
expect(HTTParty).to have_received(:post)
.with("#{url}#{path}", body: body.to_json, headers: header, timeout: 7)
it 'should call post with valid params' do
expect(HTTParty).to have_received(:get)
.with("#{url}#{path}", body: body.to_json, headers: header, timeout: 7)
end
end
end

describe '#post' do
let(:url) { 'https://api.getresponse.com/v3' }
let(:path) { '/accounts' }
let(:body) { {} }
let(:header) do
{
'X-Auth-Token' => "api-key #{apikey}",
'Content-Type' => 'application/json'
}
describe 'when making a POST request' do
before do
allow(response).to receive(:parsed_response).and_return({})
allow(HTTParty).to receive(:post).and_return(response)
connection.request(:post, path)
end

it 'should call post with valid params' do
expect(HTTParty).to have_received(:post)
.with("#{url}#{path}", body: body.to_json, headers: header, timeout: 7)
end
end
let(:timeout) { 7 }

before do
allow(HTTParty).to receive(:get)
connection.get(path)
describe 'when response is a success' do
let(:success) { {'id' => 'all ok'} }
before do
allow(response).to receive(:parsed_response).and_return(success)
allow(HTTParty).to receive(:get).and_return(response)
end

subject {connection.request(:get, path) }

it 'should return success' do
is_expected.to eq(success)
end
end

it 'should call post with valid params' do
expect(HTTParty).to have_received(:get)
.with("#{url}#{path}", body: body.to_json, headers: header, timeout: 7)
describe 'when response is an error' do
let(:error_message) { 'error_message' }
let(:error) { {'httpStatus' => 404, 'message' => error_message} }

before do
allow(response).to receive(:parsed_response).and_return(error)
allow(HTTParty).to receive(:get).and_return(response)
connection.request(:get, path)
end

subject {connection.request(:get, path) }

it 'should return error message' do
is_expected.to eq(error_message)
end
end
end
end

0 comments on commit 0c88765

Please sign in to comment.