Skip to content

Commit

Permalink
Merge pull request #1 from rafasimao/master
Browse files Browse the repository at this point in the history
Creates the possibility to get an user account from apikey
  • Loading branch information
rafasimao authored Aug 30, 2017
2 parents e3fdae9 + d2f5c8a commit 29f05c9
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Gemfile
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
First of all you will need to create an instance of the GetResponse Client with it's api-key:
```ruby
client = GetResponseApi::Client.new('CLIENT API KEY')
```
After this you can call the (API)[https://apidocs.getresponse.com/v3] methods through it.
For example:
```ruby
client.account
# This method will return the client account hash if it succeds and a error message otherwise
```

## Development

Expand Down
6 changes: 3 additions & 3 deletions Rakefile
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
28 changes: 15 additions & 13 deletions get_response_api.gemspec
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
4 changes: 3 additions & 1 deletion lib/get_response_api.rb
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'
22 changes: 22 additions & 0 deletions lib/get_response_api/client.rb
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
41 changes: 41 additions & 0 deletions lib/get_response_api/connection.rb
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
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.1.0'
end
46 changes: 46 additions & 0 deletions spec/get_response_api/client_spec.rb
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
52 changes: 52 additions & 0 deletions spec/get_response_api/connection_spec.rb
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
8 changes: 2 additions & 6 deletions spec/get_response_api_spec.rb
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
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "bundler/setup"
require "get_response_api"
require 'bundler/setup'
require 'get_response_api'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
Expand Down

0 comments on commit 29f05c9

Please sign in to comment.