-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rafasimao/master
Creates the possibility to get an user account from apikey
- Loading branch information
Showing
12 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
source "https://rubygems.org" | ||
source 'https://rubygems.org' | ||
|
||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | ||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } | ||
|
||
# Specify your gem's dependencies in get_response_api.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path("../lib", __FILE__) | ||
|
||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require "get_response_api/version" | ||
require 'get_response_api/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "get_response_api" | ||
spec.name = 'get_response_api' | ||
spec.version = GetResponseApi::VERSION | ||
spec.authors = ["Juliane Lima", "Rafael S."] | ||
spec.email = ["[email protected]", "[email protected]"] | ||
spec.authors = ['Juliane Lima', 'Rafael S.'] | ||
spec.email = ['[email protected]', '[email protected]'] | ||
|
||
spec.summary = %q{Wrapper for integration with GetResponse API.} | ||
spec.description = %q{See https://github.com/ignicaodigitalbr/get_response_api for more information.} | ||
spec.homepage = "https://github.com/ignicaodigitalbr/get_response_api" | ||
spec.license = "MIT" | ||
spec.homepage = 'https://github.com/ignicaodigitalbr/get_response_api' | ||
spec.license = 'MIT' | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.bindir = "exe" | ||
spec.bindir = 'exe' | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.15" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "~> 3.0" | ||
spec.add_development_dependency 'bundler', '~> 1.15' | ||
spec.add_development_dependency 'rake', '~> 10.0' | ||
spec.add_development_dependency 'rspec', '~> 3.0' | ||
spec.add_development_dependency 'webmock' | ||
spec.add_runtime_dependency 'httparty', '~> 0.15' | ||
|
||
spec.required_ruby_version = '>=2.0.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
require "get_response_api/version" | ||
require 'get_response_api/version' | ||
require 'get_response_api/connection' | ||
require 'get_response_api/client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module GetResponseApi | ||
class Client | ||
def initialize(api_key) | ||
@connection = Connection.new(api_key) | ||
end | ||
|
||
def account | ||
response = @connection.get('/accounts').parsed_response | ||
|
||
if error?(response) && response['message'] | ||
return response['message'] | ||
end | ||
response | ||
end | ||
|
||
private | ||
|
||
def error?(response) | ||
response['httpStatus'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'httparty' | ||
|
||
module GetResponseApi | ||
class Connection | ||
API_ENDPOINT = 'https://api.getresponse.com/v3' | ||
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 | ||
) | ||
end | ||
|
||
def get(path, body: {}, headers: {}) | ||
headers.merge!(auth) | ||
HTTParty.get( | ||
"#{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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module GetResponseApi | ||
VERSION = "0.1.0" | ||
VERSION = '0.1.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'spec_helper' | ||
require 'webmock/rspec' | ||
|
||
RSpec.describe GetResponseApi::Client do | ||
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) | ||
end | ||
|
||
context 'when the request is valid' do | ||
let(:response) { result.to_json } | ||
let(:result) do | ||
{ | ||
"accountId"=>"pdIhh", | ||
"firstName"=>"Jane", | ||
"lastName"=>"Doe", | ||
"email"=>"[email protected]" | ||
} | ||
end | ||
|
||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe GetResponseApi::Connection do | ||
let(:apikey) { 'apikey123456' } | ||
let(:connection) { described_class.new(apikey) } | ||
|
||
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' | ||
} | ||
end | ||
let(:timeout) { 7 } | ||
|
||
before do | ||
allow(HTTParty).to receive(:post) | ||
connection.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 | ||
|
||
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' | ||
} | ||
end | ||
let(:timeout) { 7 } | ||
|
||
before do | ||
allow(HTTParty).to receive(:get) | ||
connection.get(path) | ||
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) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
require "spec_helper" | ||
require 'spec_helper' | ||
|
||
RSpec.describe GetResponseApi do | ||
it "has a version number" do | ||
it 'has a version number' do | ||
expect(GetResponseApi::VERSION).not_to be nil | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters