leanweb 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4420d0acf838a96016cabcbdf4fb5977044c4c43c7102bbb7c520dab34a3908
4
- data.tar.gz: 861d7f89f86f9749ac69e007ffd23218047f6cabf825a53074df2aab953e9bb3
3
+ metadata.gz: 6b3993343fb571b11c2bdf16e2874103e79b5994dee7218c94fd7175a5458a57
4
+ data.tar.gz: c9cdaff0f1ae2cb6af897b644f3ffa39829bd590c5c2f6aecf81329485f96495
5
5
  SHA512:
6
- metadata.gz: 3377a07bfdee4a0fe5ebfdd40d3094786ac204b2301eafbf796b296bcce86ab6728352246f74abea581d5910f0f3868d5d11c7706ee4f61ce5275f74914365c4
7
- data.tar.gz: 616f2bcc6979253f43d20135a72a3537c6eb3680278c39fa73c879e0a088434c1242f10f200202ab0ec5533c746cab0ca971e655ed79956cdd3f3bca6886bac8
6
+ metadata.gz: b5abde684aa9f4f542c0256a3af664298797c424541db46540d77862098c7e8509cb00ff81e7d1a7d4032025727e4cb2610d3ee75bcc51be0cd7b10f54c704f5
7
+ data.tar.gz: c4a154a07332e20bcd5f60d830516a6a937f3e507cd010e9df5266c51b14fa9740d32cc03594b2b362f215780aad68f833f1939c7eba63df201fb6c616de5d6f
@@ -10,7 +10,6 @@
10
10
  require 'json'
11
11
  require 'net/http'
12
12
  require 'openssl'
13
- require 'tilt'
14
13
  require 'uri'
15
14
 
16
15
  # Hawese client.
@@ -22,6 +21,13 @@ require 'uri'
22
21
  # - HAWESE_ORIGIN
23
22
  # - HAWESE_AUTH_TOKEN
24
23
  class Hawese
24
+ # Error to display when a response is unexpected.
25
+ class UnexpectedResponseError < RuntimeError
26
+ def initialize(message = 'Unexpected response')
27
+ super(message)
28
+ end
29
+ end
30
+
25
31
  class << self
26
32
  ENDPOINT = ENV.fetch('HAWESE_ENDPOINT')
27
33
  RETURN_URL = ENV.fetch('HAWESE_RETURN_URL') do
@@ -44,15 +50,31 @@ class Hawese
44
50
  JSON.parse(Net::HTTP.get(uri), symbolize_names: true)
45
51
  end
46
52
 
47
- def purchase(gateway, fields)
48
- uri = URI("#{ENDPOINT}/#{ORIGIN}/gateways/#{gateway}/purchase")
49
- uri.query = URI.encode_www_form(return_url: RETURN_URL)
53
+ def do_action(gateway, schema, fields)
54
+ uri = URI("#{ENDPOINT}/#{ORIGIN}/gateways/#{gateway}/#{schema}")
55
+ uri.query = URI.encode_www_form(return_url: RETURN_URL) if
56
+ schema == 'purchase'
50
57
  response = Net::HTTP.post(
51
58
  uri,
52
59
  fields.to_json,
53
- 'Content-Type' => 'application/json'
60
+ {
61
+ 'Authorization' => "Bearer #{ENV.fetch('HAWESE_AUTH_TOKEN')}",
62
+ 'Content-Type' => 'application/json'
63
+ }
54
64
  )
55
- JSON.parse(response.body, symbolize_names: true)
65
+ process_response(response)
66
+ end
67
+
68
+ def purchase(gateway, fields)
69
+ do_action(gateway, 'purchase', fields)
70
+ end
71
+
72
+ def link(gateway, fields)
73
+ do_action(gateway, 'link', fields)
74
+ end
75
+
76
+ def charge(gateway, fields)
77
+ do_action(gateway, 'charge', fields)
56
78
  end
57
79
 
58
80
  def receipt(gateway, payment_uuid, fields = {})
@@ -66,7 +88,7 @@ class Hawese
66
88
  })
67
89
  req.body = fields.to_json
68
90
  response = http.request(req)
69
- receipt_process_response(response)
91
+ process_response(response)
70
92
  end
71
93
  end
72
94
 
@@ -98,14 +120,22 @@ class Hawese
98
120
  fields
99
121
  end
100
122
 
101
- def receipt_process_response(response)
123
+ def process_response(response)
102
124
  response_body = JSON.parse(response.body, symbolize_names: true)
103
-
104
- case response.code
105
- when '401' then raise(response_body)
106
- when '200' then response_body
107
- else raise(response_body[:error][:message])
125
+ unless response.code.to_i.between?(200, 299)
126
+ raise(
127
+ UnexpectedResponseError,
128
+ if response_body.instance_of?(String)
129
+ response_body
130
+ else
131
+ response_body.[](:error)&.[](:message)
132
+ end
133
+ )
108
134
  end
135
+
136
+ response_body
137
+ rescue JSON::ParserError
138
+ raise(response.body)
109
139
  end
110
140
  end
111
141
  end
@@ -76,7 +76,7 @@ module Rack
76
76
 
77
77
  def generate_sid(*)
78
78
  sid = super
79
- FileUtils.mkdir_p(@session_dir, mode: 0o700)
79
+ FileUtils.mkdir_p(@session_dir, mode: 0o1777)
80
80
  ::File.new("#{@session_dir}/#{sid}", 'w', 0o600).close
81
81
  sid
82
82
  end
@@ -9,5 +9,5 @@
9
9
 
10
10
 
11
11
  module LeanWeb
12
- VERSION = '0.5.3'
12
+ VERSION = '0.5.4'
13
13
  end
data/lib/leanweb.rb CHANGED
@@ -9,7 +9,6 @@
9
9
 
10
10
  # LeanWeb is a minimal hybrid static / dynamic web framework.
11
11
  module LeanWeb
12
- require_relative 'leanweb/version'
13
12
 
14
13
  ROOT_PATH = ENV.fetch('LEANWEB_ROOT_PATH', Dir.pwd)
15
14
  CONTROLLER_PATH = "#{ROOT_PATH}/src/controllers"
@@ -31,6 +30,7 @@ module LeanWeb
31
30
  autoload :Route, 'leanweb/route.rb'
32
31
  autoload :Controller, 'leanweb/controller.rb'
33
32
  autoload :App, 'leanweb/app.rb'
34
- end
35
33
 
36
- require_relative 'leanweb/helpers'
34
+ require_relative 'leanweb/version'
35
+ require_relative 'leanweb/helpers'
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leanweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Freeman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-08 00:00:00.000000000 Z
11
+ date: 2023-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack