leanweb 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ffe16c4e32b210678118c5ef8a55293d3a67e0d1e8ada3014b9913dd17b2abb
4
- data.tar.gz: 7e7da7e24539d389e688e2a0d3f88b5dca3c19804a104bc4ea2fe5f7fd7beb10
3
+ metadata.gz: d558c8dc51eb0ded4ae4ca0c73bed085cbc9f807df31f615ec697f2555c59492
4
+ data.tar.gz: f45c4c2df3cbdfd6dcc1b56c37e6a79d36b24497b8a62faad2ee2c7d0d47027d
5
5
  SHA512:
6
- metadata.gz: cd9730e0fd3f231d0dfa9d7cafcad4ece3d3e46f7665f77bf497e8a9f9789a5ef030c70fd42ec19606820a1fd2934005b1f09a7750e37a3766e278d7363beb0f
7
- data.tar.gz: 389915c7ec7301ecfc7cab2e2758e4e4de5f689ffaf3f77cb8d8de35159170db5658a0639112b476d3e8035f8628f24bbf19b3c2fac89a783813dedcebcf9b8d
6
+ metadata.gz: d7e428e0f47aea5c20e8bf59a8bc93e4bfec30488c9e84a2e9d3d26901fcd6ae5b96710fd7cfc4346c028781879cf343bc479069aec58b0e3f41ef1ec43a8a42
7
+ data.tar.gz: 87a6f8b615112c8525a9241b6fc0d4b02b305334abc064072b6c4babe80a775af138fa935161211c9af547acbdef9f9ab808aa1f2455f221357b17904fd4a498
@@ -20,6 +20,7 @@ require 'uri'
20
20
  # - HAWESE_RETURN_URL defaults to `LEANWEB_ENDPOINT/checkout`.
21
21
  # - HAWESE_ENDPOINT
22
22
  # - HAWESE_ORIGIN
23
+ # - HAWESE_AUTH_TOKEN
23
24
  class Hawese
24
25
  class << self
25
26
  ENDPOINT = ENV.fetch('HAWESE_ENDPOINT')
@@ -54,6 +55,21 @@ class Hawese
54
55
  JSON.parse(response.body, symbolize_names: true)
55
56
  end
56
57
 
58
+ def receipt(gateway, payment_uuid, fields = {})
59
+ uri = URI(
60
+ "#{ENDPOINT}/#{ORIGIN}/gateways/#{gateway}/receipt/#{payment_uuid}"
61
+ )
62
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
63
+ req = Net::HTTP::Post.new(uri, {
64
+ 'Authorization' => "Bearer #{ENV.fetch('HAWESE_AUTH_TOKEN')}",
65
+ 'Content-Type' => 'application/json'
66
+ })
67
+ req.body = fields.to_json
68
+ response = http.request(req)
69
+ receipt_process_response(response)
70
+ end
71
+ end
72
+
57
73
  def purchase_from_schema(gateway, schema)
58
74
  purchase(gateway, fields_to_schema_defaults(schema))
59
75
  end
@@ -81,5 +97,15 @@ class Hawese
81
97
  end
82
98
  fields
83
99
  end
100
+
101
+ def receipt_process_response(response)
102
+ 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])
108
+ end
109
+ end
84
110
  end
85
111
  end
@@ -12,6 +12,7 @@ require 'net/smtp'
12
12
  require 'securerandom'
13
13
  require 'socket'
14
14
  require 'time'
15
+ require 'uri/mailto'
15
16
 
16
17
  # Send an email with SMTP easily.
17
18
  #
@@ -25,27 +26,32 @@ require 'time'
25
26
  # - SMTP_FROM: In the format `Name <user@mail>` or `user@mail`.
26
27
  #
27
28
  # @example Single address
28
- # LeanMail.deliver('to@mail', 'subject', 'body')
29
+ # LeanMail.deliver('to@mail', 'subject', 'body', reply_to: 'to@other.host')
29
30
  #
30
31
  # @example Multiple addresses
31
32
  # LeanMail.deliver(['to@mail', 'to2@mail'], 'subject', 'body')
32
33
  module LeanMail
33
34
  # RFC 2821 message data representation.
34
35
  class Data
35
- attr_reader :from, :to, :subject, :message
36
+ attr_reader :from, :to, :subject, :message, :reply_to
37
+
38
+ def initialize(from, to, subject, message, reply_to: nil)
39
+ self.class.validate_recipients(to)
40
+ self.class.validate_recipients(reply_to) if reply_to
36
41
 
37
- def initialize(from, to, subject, message)
38
42
  @from = from
39
43
  @to = to
40
44
  @subject = subject
41
45
  @message = message
46
+ @reply_to = reply_to
42
47
  end
43
48
 
44
49
  def to_s
45
50
  <<~MAIL
46
51
  Content-type: text/plain; charset=UTF-8
47
52
  From: #{@from}
48
- To: #{to.instance_of?(Array) ? to.join(', ') : to}
53
+ To: #{@to.instance_of?(Array) ? @to.join(', ') : @to}
54
+ Reply-To: #{@reply_to || @from}
49
55
  Subject: =?UTF-8?B?#{Base64.strict_encode64(subject)}?=
50
56
  Date: #{Time.new.rfc2822}
51
57
  Message-Id: <#{SecureRandom.uuid}@#{Socket.gethostname}>
@@ -53,6 +59,29 @@ module LeanMail
53
59
  #{@message}
54
60
  MAIL
55
61
  end
62
+
63
+ class << self
64
+ def validate_recipients(recipients)
65
+ return recipients.each{ |recipient| validate_recipients(recipient) } if
66
+ recipients.instance_of?(Array)
67
+
68
+ raise(ArgumentError, "#{recipients} is not a valid recipient") unless
69
+ URI::MailTo::EMAIL_REGEXP.match?(extract_addr(recipients))
70
+ end
71
+
72
+ def extract_addr(str)
73
+ match = str.match(/\A[^\n\r<]+<([^\n\r>]+)>\z/)
74
+ return match[1] if match
75
+
76
+ str
77
+ end
78
+
79
+ def extract_addrs(to)
80
+ return to.map{ |addr| extract_addr(addr) } if to.instance_of?(Array)
81
+
82
+ extract_addr(to)
83
+ end
84
+ end
56
85
  end
57
86
 
58
87
  # Email deliverer.
@@ -76,12 +105,17 @@ module LeanMail
76
105
  # @param to [Array, String] In the format `Name <user@mail>` or `user@mail`.
77
106
  # @param subject [String]
78
107
  # @param body [String]
108
+ # @param reply_to [Array, String] In the same format as `to`.
79
109
  # @return [Deliver]
80
- def call(to, subject, body)
81
- @data = Data.new(@from, to, subject, body)
110
+ def call(to, subject, body, reply_to: nil)
111
+ @data = Data.new(@from, to, subject, body, reply_to: reply_to)
82
112
 
83
113
  @smtp.start(Socket.gethostname, @user, @password, :plain) do |smtp|
84
- smtp.send_message(@data.to_s, extract_addr(@from), extract_addrs(to))
114
+ smtp.send_message(
115
+ @data.to_s,
116
+ Data.extract_addr(@from),
117
+ Data.extract_addrs(to)
118
+ )
85
119
  end
86
120
 
87
121
  self
@@ -100,19 +134,6 @@ module LeanMail
100
134
  when 'starttls' then @smtp.enable_starttls
101
135
  end
102
136
  end
103
-
104
- def extract_addr(str)
105
- match = str.match(%r{<([^/]+)>})
106
- return match[1] if match
107
-
108
- str
109
- end
110
-
111
- def extract_addrs(to)
112
- return to.map{ |addr| extract_addr(addr) } if to.instance_of?(Array)
113
-
114
- extract_addr(to)
115
- end
116
137
  end
117
138
 
118
139
  # Deliver email.
@@ -120,8 +141,9 @@ module LeanMail
120
141
  # @param to [Array, String] In the format `Name <user@mail>` or `user@mail`.
121
142
  # @param subject [String]
122
143
  # @param body [String]
144
+ # @param reply_to [Array, String] In the same format as `to`.
123
145
  # @return [Deliver]
124
- def self.deliver(to, subject, body)
125
- LeanMail::Deliver.new.call(to, subject, body)
146
+ def self.deliver(to, subject, body, reply_to: nil)
147
+ LeanMail::Deliver.new.call(to, subject, body, reply_to: reply_to)
126
148
  end
127
149
  end
data/lib/leanweb/route.rb CHANGED
@@ -73,10 +73,8 @@ module LeanWeb
73
73
 
74
74
  respond_method(request)
75
75
  rescue NoMethodError
76
- raise unless @static == true && (view_path = guess_view_path)
77
-
78
76
  controller = default_controller_class.new(self)
79
- controller.default_static_action(view_path)
77
+ controller.default_static_action(guess_view_path)
80
78
  end
81
79
 
82
80
  # String path, independent if {#path} is Regexp or String.
@@ -9,5 +9,5 @@
9
9
 
10
10
 
11
11
  module LeanWeb
12
- VERSION = '0.5.0'
12
+ VERSION = '0.5.2'
13
13
  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.0
4
+ version: 0.5.2
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-08 00:00:00.000000000 Z
11
+ date: 2023-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack