M contrib/lib/hawese.rb => contrib/lib/hawese.rb +40 -9
@@ 10,7 10,6 @@
require 'json'
require 'net/http'
require 'openssl'
-require 'tilt'
require 'uri'
# Hawese client.
@@ 22,6 21,13 @@ require 'uri'
# - HAWESE_ORIGIN
# - HAWESE_AUTH_TOKEN
class Hawese
+ # Error to display when a response is unexpected.
+ class UnexpectedResponseError < RuntimeError
+ def initialize(message = 'Unexpected response')
+ super(message)
+ end
+ end
+
class << self
ENDPOINT = ENV.fetch('HAWESE_ENDPOINT')
RETURN_URL = ENV.fetch('HAWESE_RETURN_URL') do
@@ 44,15 50,31 @@ class Hawese
JSON.parse(Net::HTTP.get(uri), symbolize_names: true)
end
- def purchase(gateway, fields)
- uri = URI("#{ENDPOINT}/#{ORIGIN}/gateways/#{gateway}/purchase")
- uri.query = URI.encode_www_form(return_url: RETURN_URL)
+ def do_action(gateway, schema, fields)
+ uri = URI("#{ENDPOINT}/#{ORIGIN}/gateways/#{gateway}/#{schema}")
+ uri.query = URI.encode_www_form(return_url: RETURN_URL) if
+ schema == 'purchase'
response = Net::HTTP.post(
uri,
fields.to_json,
- 'Content-Type' => 'application/json'
+ {
+ 'Authorization' => "Bearer #{ENV.fetch('HAWESE_AUTH_TOKEN')}",
+ 'Content-Type' => 'application/json'
+ }
)
- JSON.parse(response.body, symbolize_names: true)
+ process_response(response)
+ end
+
+ def purchase(gateway, fields)
+ do_action(gateway, 'purchase', fields)
+ end
+
+ def link(gateway, fields)
+ do_action(gateway, 'link', fields)
+ end
+
+ def charge(gateway, fields)
+ do_action(gateway, 'charge', fields)
end
def receipt(gateway, payment_uuid, fields = {})
@@ 66,7 88,7 @@ class Hawese
})
req.body = fields.to_json
response = http.request(req)
- receipt_process_response(response)
+ process_response(response)
end
end
@@ 98,9 120,18 @@ class Hawese
fields
end
- def receipt_process_response(response)
+ def process_response(response)
response_body = JSON.parse(response.body, symbolize_names: true)
- raise(response_body[:error][:message]) unless response.code == '200'
+ unless response.code.to_i.between?(200, 299)
+ raise(
+ UnexpectedResponseError,
+ if response_body.instance_of?(String)
+ response_body
+ else
+ response_body.[](:error)&.[](:message)
+ end
+ )
+ end
response_body
rescue JSON::ParserError
M lib/leanweb.rb => lib/leanweb.rb +3 -3
@@ 9,7 9,6 @@
# LeanWeb is a minimal hybrid static / dynamic web framework.
module LeanWeb
- require_relative 'leanweb/version'
ROOT_PATH = ENV.fetch('LEANWEB_ROOT_PATH', Dir.pwd)
CONTROLLER_PATH = "#{ROOT_PATH}/src/controllers"
@@ 31,6 30,7 @@ module LeanWeb
autoload :Route, 'leanweb/route.rb'
autoload :Controller, 'leanweb/controller.rb'
autoload :App, 'leanweb/app.rb'
-end
-require_relative 'leanweb/helpers'
+ require_relative 'leanweb/version'
+ require_relative 'leanweb/helpers'
+end
M lib/leanweb/version.rb => lib/leanweb/version.rb +1 -1
@@ 9,5 9,5 @@
module LeanWeb
- VERSION = '0.5.3'
+ VERSION = '0.5.4'
end
M test/contrib/hawese_test.rb => test/contrib/hawese_test.rb +82 -19
@@ 66,11 66,71 @@ class HaweseTest < Minitest::Test
:post,
'https://api.hackware.cl/origin/gateways/paypal/purchase'
).with(
+ headers: { Authorization: 'Bearer Myself' },
query: { return_url: 'https://example.org/return' },
body: { field: 'value' }
).to_return(body: '"purchase"')
- assert_equal('purchase', Hawese.purchase('paypal', { field: 'value' }))
+ temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
+ assert_equal(
+ 'purchase',
+ Hawese.do_action('paypal', 'purchase', { field: 'value' })
+ )
+ end
+ end
+
+ def test_link_and_charge
+ %w[link charge].each do |action|
+ stub_request(
+ :post,
+ "https://api.hackware.cl/origin/gateways/paypal/#{action}"
+ ).with(
+ headers: { Authorization: 'Bearer Myself' },
+ body: { field: 'value' }
+ ).to_return(body: "\"#{action}\"")
+
+ temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
+ assert_equal(
+ action,
+ Hawese.do_action('paypal', action, { field: 'value' })
+ )
+ end
+ end
+ end
+
+ def test_do_action
+ stub_request(
+ :post,
+ 'https://api.hackware.cl/origin/gateways/paypal/action'
+ ).with(
+ headers: { Authorization: 'Bearer Myself' },
+ body: { field: 'value' }
+ ).to_return(body: '"action"')
+
+ temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
+ assert_equal(
+ 'action',
+ Hawese.do_action('paypal', 'action', { field: 'value' })
+ )
+ end
+ end
+
+ def test_do_action__with_error
+ stub_request(
+ :post,
+ 'https://api.hackware.cl/origin/gateways/paypal/action'
+ ).with(
+ headers: { Authorization: 'Bearer Myself' },
+ body: { field: 'value' }
+ ).to_return(body: '{"error": {"message": "error"} }', status: 400)
+
+ temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
+ error = assert_raises(Hawese::UnexpectedResponseError) do
+ Hawese.do_action('paypal', 'action', { field: 'value' })
+ end
+
+ assert_equal('error', error.message)
+ end
end
def test_purchase_from_schema
@@ 78,6 138,7 @@ class HaweseTest < Minitest::Test
:post,
'https://api.hackware.cl/origin/gateways/paypal/purchase'
).with(
+ headers: { Authorization: 'Bearer Myself' },
query: { return_url: 'https://example.org/return' },
body: {
field: 'value',
@@ 85,27 146,29 @@ class HaweseTest < Minitest::Test
}
).to_return(body: '"purchase_from_schema"')
- assert_equal(
- 'purchase_from_schema',
- Hawese.purchase_from_schema(
- 'paypal',
- {
- properties: {
- field: { default: 'value' },
- deep: {
- properties: {
- inner: {
- properties: {
- field: { default: 'value' }
+ temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
+ assert_equal(
+ 'purchase_from_schema',
+ Hawese.purchase_from_schema(
+ 'paypal',
+ {
+ properties: {
+ field: { default: 'value' },
+ deep: {
+ properties: {
+ inner: {
+ properties: {
+ field: { default: 'value' }
+ }
}
}
- }
- },
- other_field: { any: 'thing' }
+ },
+ other_field: { any: 'thing' }
+ }
}
- }
+ )
)
- )
+ end
end
def test_receipt
@@ 130,7 193,7 @@ class HaweseTest < Minitest::Test
.to_return(body: '{"error": {"message": "error"} }', status: 500)
temp_env('HAWESE_AUTH_TOKEN' => 'Myself') do
- error = assert_raises(RuntimeError) do
+ error = assert_raises(Hawese::UnexpectedResponseError) do
Hawese.receipt('paypal', 'payment-uuid')
end