Skip to content

Refactor WePay::Client #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.DS_STORE
/.yardoc
/*.gem
/*.gemspec
Expand Down
77 changes: 77 additions & 0 deletions lib/we_pay/base_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'json'
require 'net/http'
require 'net/https'
require 'uri'

module WePay
module BaseRequest

attr_reader :client_id,
:client_secret,
:api_version,
:path,
:access_token,
:params

def initialize(client_id, client_secret, api_version, path, access_token, params)
@client_id = client_id
@client_secret = client_secret
@api_version = api_version
@path = path
@access_token = access_token
@params = params
end

def response
JSON.parse(raw_response.body)
end

private

def raw_response
@raw_response ||= client.start do |c|
c.request(request)
end
end

def request
@request ||= Net::HTTP::Post.new(uri.path, default_headers).tap do |r|
if params.any?
r.body = params.to_json
end

if access_token
r.add_field('Authorization', "Bearer #{access_token}")
end

if api_version
r.add_field('Api-Version', @api_version)
end
end
end

def client
@client ||= Net::HTTP.new(uri.host, uri.port).tap do |c|
c.read_timeout = 30
c.use_ssl = true
end
end

def uri
@uri ||= URI.join(api_endpoint, normalized_path)
end

def normalized_path
return path if path.start_with?('/')

path.prepend('/')
end

def default_headers
{
'Content-Type' => 'application/json',
'User-Agent' => 'WePay Ruby SDK'
}
end
end
end
138 changes: 138 additions & 0 deletions lib/we_pay/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
##
# Copyright (c) 2012-2016 WePay.
#
# http://opensource.org/licenses/Apache2.0
##

require 'cgi'
require 'json'
require 'net/http'
require 'net/https'

module WePay

##
# A very simple wrapper for the WePay API.
##
class Client

# Stage API endpoint
STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2"

# Stage UI endpoint
STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2"

# Production API endpoint
PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2"

# Production UI endpoint
PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2"

attr_reader :client_id,
:client_secret,
:use_stage,
:api_version

def initialize(client_id, client_secret, use_stage = true, api_version = nil)
@client_id = client_id.to_s
@client_secret = client_secret.to_s
@use_stage = !!use_stage
@api_version = api_version.to_s
end

##
# Execute a call to the WePay API.
##
def call(path, access_token = false, params = {})
request_class.new(
client_id,
client_secret,
api_version,
path,
access_token,
params
).response
end

##
# Returns the OAuth 2.0 URL that users should be redirected to for
# authorizing your API application. The `redirect_uri` must be a
# fully-qualified URL (e.g., `https://www.wepay.com`).
##
def oauth2_authorize_url(
redirect_uri,
user_email = false,
user_name = false,
permissions = default_oauth_permissions,
user_country = false
)
url = ui_endpoint +
'/oauth2/authorize?client_id=' + @client_id.to_s +
'&redirect_uri=' + redirect_uri +
'&scope=' + permissions

url += user_name ? '&user_name=' + CGI::escape(user_name) : ''
url += user_email ? '&user_email=' + CGI::escape(user_email) : ''
url += user_country ? '&user_country=' + CGI::escape(user_country) : ''
end

##
# Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`.
##
def oauth2_token(code, redirect_uri)
call('/oauth2/token', false, {
'client_id' => @client_id,
'client_secret' => @client_secret,
'redirect_uri' => redirect_uri,
'code' => code
})
end

##
# Support exisiting API
#
# `WePay::Client#api_endpoint`
##
def api_endpoint
if use_stage
STAGE_API_ENDPOINT
else
PRODUCTION_API_ENDPOINT
end
end

##
# Support exisiting API
#
# `WePay::Client#ui_endpoint`
##
def ui_endpoint
if use_stage
STAGE_UI_ENDPOINT
else
PRODUCTION_UI_ENDPOINT
end
end

private

def request_class
if use_stage
TestRequest
else
ProductionRequest
end
end

def default_oauth_permissions
%w(
manage_accounts
collect_payments
view_user
send_money
preapprove_payments
manage_subscriptions
).join(',')
end
end
end
17 changes: 17 additions & 0 deletions lib/we_pay/production_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WePay
class ProductionRequest
include BaseRequest

private

def api_endpoint
::WePay::Client::PRODUCTION_API_ENDPOINT
end

# :nocov:
def ui_endpoint
::WePay::Client::PRODUCTION_UI_ENDPOINT
end
# :nocov:
end
end
17 changes: 17 additions & 0 deletions lib/we_pay/test_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WePay
class TestRequest
include BaseRequest

private

def api_endpoint
::WePay::Client::STAGE_API_ENDPOINT
end

# :nocov:
def ui_endpoint
::WePay::Client::STAGE_UI_ENDPOINT
end
# :nocov:
end
end
118 changes: 4 additions & 114 deletions lib/wepay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,123 +4,13 @@
# http://opensource.org/licenses/Apache2.0
##

require 'cgi'
require 'json'
require 'net/http'
require 'net/https'
require 'rubygems'
require 'uri'

##
# The root WePay namespace.
##
module WePay
autoload :Client, 'we_pay/client'

##
# A very simple wrapper for the WePay API.
##
class Client

# Stage API endpoint
STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2"

# Stage UI endpoint
STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2"

# Production API endpoint
PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2"

# Production UI endpoint
PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2"

attr_reader :api_endpoint
attr_reader :api_version
attr_reader :client_id
attr_reader :client_secret
attr_reader :ui_endpoint

def initialize(client_id, client_secret, use_stage = true, api_version = nil)
@client_id = client_id.to_s
@client_secret = client_secret.to_s
@api_version = api_version.to_s

if use_stage
@api_endpoint = STAGE_API_ENDPOINT
@ui_endpoint = STAGE_UI_ENDPOINT
else
@api_endpoint = PRODUCTION_API_ENDPOINT
@ui_endpoint = PRODUCTION_UI_ENDPOINT
end
end

##
# Execute a call to the WePay API.
##
def call(call, access_token = false, params = {})
path = call.start_with?('/') ? call : call.prepend('/')
url = URI.parse(api_endpoint + path)

call = Net::HTTP::Post.new(url.path, {
'Content-Type' => 'application/json',
'User-Agent' => 'WePay Ruby SDK'
})

unless params.empty?
call.body = params.to_json
end

if access_token then call.add_field('Authorization', "Bearer #{access_token}"); end
if @api_version then call.add_field('Api-Version', @api_version); end

make_request(call, url)
end

##
# Returns the OAuth 2.0 URL that users should be redirected to for
# authorizing your API application. The `redirect_uri` must be a
# fully-qualified URL (e.g., `https://www.wepay.com`).
##
def oauth2_authorize_url(
redirect_uri,
user_email = false,
user_name = false,
permissions = "manage_accounts,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions",
user_country = false
)
url = @ui_endpoint +
'/oauth2/authorize?client_id=' + @client_id.to_s +
'&redirect_uri=' + redirect_uri +
'&scope=' + permissions

url += user_name ? '&user_name=' + CGI::escape(user_name) : ''
url += user_email ? '&user_email=' + CGI::escape(user_email) : ''
url += user_country ? '&user_country=' + CGI::escape(user_country) : ''
end

##
# Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`.
##
def oauth2_token(code, redirect_uri)
call('/oauth2/token', false, {
'client_id' => @client_id,
'client_secret' => @client_secret,
'redirect_uri' => redirect_uri,
'code' => code
})
end

private

##
# Make the HTTP request to the endpoint.
##
def make_request(call, url)
request = Net::HTTP.new(url.host, url.port)
request.read_timeout = 30
request.use_ssl = true

response = request.start { |http| http.request(call) }
JSON.parse(response.body)
end
end
autoload :BaseRequest, 'we_pay/base_request'
autoload :TestRequest, 'we_pay/test_request'
autoload :ProductionRequest, 'we_pay/production_request'
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
CodeClimate::TestReporter.start

require 'simplecov'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
SimpleCov.start

require 'wepay'
Expand Down
Loading