faraday_ln_paywall 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 189f22ecf57061e88d58a3d9e70fa410e506779d
4
- data.tar.gz: d3b011669e55c07c16ab39cf024b1b9d8cf83e44
3
+ metadata.gz: e4b36c056ecb79191ceb42493b4672204e987df6
4
+ data.tar.gz: 239fcea1e9093d674879cad9ff9280e41ed21431
5
5
  SHA512:
6
- metadata.gz: 4bab8106092af137cc83eac83b7b2097a2099bb4da90b921d796ac31135a3a23071f1da5b176e89dc39ac7390c5b92f8ea3028642f1173a6fea382e22a95d83e
7
- data.tar.gz: 7b6f5913247aa87fea555ac86c1bb52b48a515e77a3d89453f024072f475e87ca292ada25ac8b3a568300452b022c05b4653507acff7a9c9e955e315842d020b
6
+ metadata.gz: af6d7c79810f155208ca721122449d9a2be992ecba5297036a4c5be26c162f2044449472aec862abf567eaed063031ade574946a14e8606c41bd3549daf968f1
7
+ data.tar.gz: a83bbe7be6d258fcd0931b55afb3383ab99056b32a123b9433a86461020360c66e0cdc073d2eda74104a704222997eb90abbd461ab428de25180155ae42e629f
data/example.rb CHANGED
@@ -2,11 +2,13 @@ require "bundler/setup"
2
2
  require "faraday_ln_paywall"
3
3
  require "faraday"
4
4
 
5
- conn = Faraday.new(:url => 'https://fourohtwo.herokuapp.com') do |faraday|
5
+ conn = Faraday.new(:url => 'https://lightning-2pdf.herokuapp.com/') do |faraday|
6
+ #conn = Faraday.new(:url => 'https://fourohtwo.herokuapp.com') do |faraday|
6
7
  faraday.use FaradayLnPaywall::Middleware, {
7
- max_amount: 1000,
8
+ max_amount: 100,
8
9
  timeout: 300
9
10
  }
10
11
  faraday.adapter Faraday.default_adapter
11
12
  end
12
- puts conn.get("/402").body
13
+ puts conn.post("/html", "# hallo").body
14
+ # puts conn.get("/402").body
@@ -26,7 +26,7 @@ module FaradayLnPaywall
26
26
  end
27
27
 
28
28
  def validate_invoice!(invoice)
29
- if !@options[:max_amount].nil? && @options[:max_amount] < invoice.amount
29
+ if !@options[:max_amount].nil? && @options[:max_amount] < invoice_amount_in_satoshi(invoice)
30
30
  raise PaymentError, "invoice amount greater than expected maximum of #{@options[:max_amount]}"
31
31
  end
32
32
  if !invoice.expiry.nil? && Time.now.to_i > invoice.timestamp + invoice.expiry.to_i
@@ -47,20 +47,35 @@ module FaradayLnPaywall
47
47
  end
48
48
 
49
49
  def call(request_env)
50
- @app.call(request_env).on_complete do |response_env|
50
+ original_call = request_env.dup
51
+ response = @app.call(request_env)
52
+ response.on_complete do |response_env|
51
53
  if payment_requested?(response_env)
52
54
  payment = pay(response_env)
53
55
  if payment && payment.payment_error == ""
54
56
  preimage = payment.payment_preimage.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
55
- request_env[:request_headers].merge!('X-Preimage' => preimage)
56
- @app.call(request_env)
57
+ original_call[:request_headers].merge!('X-Preimage' => preimage)
58
+ response = @app.call(original_call)
57
59
  else
58
60
  raise PaymentError, payment.payment_error
59
61
  end
60
62
  end
61
63
  end
64
+ response
62
65
  end
63
66
 
67
+ # todo move to Lightning/invoice gem
68
+ def invoice_amount_in_satoshi(invoice)
69
+ return if invoice.amount.nil?
70
+ multi = {
71
+ 'm' => 0.001,
72
+ 'u' => 0.000001,
73
+ 'n' => 0.000000001,
74
+ 'p' => 0.000000000001
75
+ }[invoice.multiplier]
76
+
77
+ (invoice.amount * multi * 100000000).to_i # amount in bitcoin * 100000000
78
+ end
64
79
  end
65
80
  end
66
81
 
@@ -1,3 +1,3 @@
1
1
  module FaradayLnPaywall
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_ln_paywall
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - bumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-21 00:00:00.000000000 Z
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler