leanweb 0.5.2 → 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: d558c8dc51eb0ded4ae4ca0c73bed085cbc9f807df31f615ec697f2555c59492
4
- data.tar.gz: f45c4c2df3cbdfd6dcc1b56c37e6a79d36b24497b8a62faad2ee2c7d0d47027d
3
+ metadata.gz: 6b3993343fb571b11c2bdf16e2874103e79b5994dee7218c94fd7175a5458a57
4
+ data.tar.gz: c9cdaff0f1ae2cb6af897b644f3ffa39829bd590c5c2f6aecf81329485f96495
5
5
  SHA512:
6
- metadata.gz: d7e428e0f47aea5c20e8bf59a8bc93e4bfec30488c9e84a2e9d3d26901fcd6ae5b96710fd7cfc4346c028781879cf343bc479069aec58b0e3f41ef1ec43a8a42
7
- data.tar.gz: 87a6f8b615112c8525a9241b6fc0d4b02b305334abc064072b6c4babe80a775af138fa935161211c9af547acbdef9f9ab808aa1f2455f221357b17904fd4a498
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
data/lib/leanweb/route.rb CHANGED
@@ -72,9 +72,6 @@ module LeanWeb
72
72
  return respond_proc(request) if @action.instance_of?(Proc)
73
73
 
74
74
  respond_method(request)
75
- rescue NoMethodError
76
- controller = default_controller_class.new(self)
77
- controller.default_static_action(guess_view_path)
78
75
  end
79
76
 
80
77
  # String path, independent if {#path} is Regexp or String.
@@ -157,14 +154,28 @@ module LeanWeb
157
154
  Controller
158
155
  end
159
156
 
157
+ def default_static_action
158
+ default_controller_class.new(self).default_static_action(guess_view_path)
159
+ end
160
+
161
+ def controller_for_request(request)
162
+ require_relative("#{CONTROLLER_PATH}/#{@action.controller.to_s.snakeize}")
163
+ Object.const_get(@action.controller).new(self, request)
164
+ rescue LoadError
165
+ nil
166
+ end
167
+
160
168
  # @param request [Rack::Request]
161
169
  # @return [Array] a valid Rack response.
162
170
  def respond_method(request)
163
171
  params = action_params(request.path)
164
- require_relative("#{CONTROLLER_PATH}/#{@action.controller.to_s.snakeize}")
165
- controller = Object.const_get(@action.controller).new(self, request)
166
- return controller.public_send(@action.action, **params) \
167
- if params.instance_of?(Hash)
172
+ controller = controller_for_request(request)
173
+
174
+ return default_static_action unless
175
+ controller&.class&.method_defined?(@action.action)
176
+
177
+ return controller.public_send(@action.action, **params) if
178
+ params.instance_of?(Hash)
168
179
 
169
180
  controller.public_send(@action.action, *params)
170
181
  end
@@ -9,5 +9,5 @@
9
9
 
10
10
 
11
11
  module LeanWeb
12
- VERSION = '0.5.2'
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.2
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-05-23 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
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
224
  requirements: []
225
- rubygems_version: 3.4.7
225
+ rubygems_version: 3.4.10
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: LeanWeb is a minimal hybrid static / dynamic web framework