repost 0.2.8 → 0.3.3

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: 0c1eb812466ae1d54e302afadfa2b7576e69495df40f154679a0168aa0e38b36
4
- data.tar.gz: a09f449f0856017e620eb9677f19c863ca04518df9f8fa805480f23784658d45
3
+ metadata.gz: 998c57594b27cb1c140eb6e141b2e64467a5cde407410c58bd3547f3529e7d20
4
+ data.tar.gz: d7fef86b10e728d49154e6883fdf53659bbbacd0f281283e6ff586f82cc67199
5
5
  SHA512:
6
- metadata.gz: 4e33d7fac875714bfab601362aa4682fca3cb41cc38e893ac44369fa29050a93b2d25ae4919080886bec98e841fb4be1b0a0c05be813f2cf4ab666bf06e12dee
7
- data.tar.gz: 540c5cecdabf30b9225af96d0475a305fd5beef07e4d1380fffa82151bed725a62261244d9b710956bca9c209857fe0b3db5b93f6ff98659ef0f944848caf38c
6
+ metadata.gz: 4ce51773149c3bb5afc481c415753a46e2136d2ef1e41aa17709028ec24682c95a44a913dddba3ea20c06a15bb201384478ab6cac42cb1f41eba97316d35e875
7
+ data.tar.gz: 97a6ab774d780fbe9cf238ee6172764adbc10d6dd6b0282f3c8950e967f413b560cb4852179d419c27d62dc1dd5ee542c66108864d90444902388a8c8235af64
@@ -1,10 +1,23 @@
1
- if defined?(Rails)
2
- class ::ActionController::Base
1
+ if defined?(Rails) && defined?(ActiveSupport)
2
+ ActiveSupport.on_load(:action_controller) do
3
+ class ::ActionController::Base
3
4
 
4
- def repost(*args)
5
- render html: Repost::Senpai.perform(*args).html_safe
6
- end
5
+ def repost(url, params: {}, options: {})
6
+ authenticity_token = form_authenticity_token if ['auto', :auto].include?(options[:authenticity_token])
7
+ render html: Repost::Senpai.perform(
8
+ url,
9
+ params: params,
10
+ options: options.merge({authenticity_token: authenticity_token}.compact)
11
+ ).html_safe
12
+ end
13
+
14
+ alias :redirect_post :repost
7
15
 
8
- alias :redirect_post :repost
16
+ end
9
17
  end
10
18
  end
19
+
20
+ # Sinatra & Rack Protection
21
+ # TODO
22
+ # defined?(Sinatra::Base) && defined?(Rack::Protection::AuthenticityToken)
23
+ # env&.fetch('rack.session', :csrf)
@@ -1,18 +1,21 @@
1
1
  module Repost
2
2
  class Senpai < Action
3
3
  DEFAULT_SUBMIT_BUTTON_TEXT = 'Continue'
4
+ DEFAULT_CHARSET = 'UTF-8'
4
5
 
5
6
  def initialize(url, params: {}, options: {})
6
- @url = url
7
- @params = params
8
- @options = options
9
- @method = options[:method] || :post
10
- @form_id = options[:form_id] || generated_form_id
11
- @autosubmit = options.fetch(:autosubmit, true)
12
- @section_classes = options.dig(:decor, :section, :classes)
13
- @section_html = options.dig(:decor, :section, :html)
14
- @submit_classes = options.dig(:decor, :submit, :classes)
15
- @submit_text = options.dig(:decor, :submit, :text) || DEFAULT_SUBMIT_BUTTON_TEXT
7
+ @url = url
8
+ @params = params
9
+ @options = options
10
+ @method = options.fetch(:method, :post)
11
+ @authenticity_token = options.fetch(:authenticity_token, nil)
12
+ @charset = options.fetch(:charset, DEFAULT_CHARSET)
13
+ @form_id = options.fetch(:form_id, generated_form_id)
14
+ @autosubmit = options.fetch(:autosubmit, true)
15
+ @section_classes = options.dig(:decor, :section, :classes)
16
+ @section_html = options.dig(:decor, :section, :html)
17
+ @submit_classes = options.dig(:decor, :submit, :classes)
18
+ @submit_text = options.dig(:decor, :submit, :text) || DEFAULT_SUBMIT_BUTTON_TEXT
16
19
  end
17
20
 
18
21
  def perform
@@ -27,23 +30,39 @@ module Repost
27
30
  private
28
31
 
29
32
  attr_reader :url, :params, :options, :method, :form_id, :autosubmit,
30
- :section_classes, :section_html, :submit_classes, :submit_text
33
+ :section_classes, :section_html, :submit_classes,
34
+ :submit_text, :authenticity_token, :charset
35
+
36
+ def form_head
37
+ "<form id='#{form_id}' action='#{url}' method='#{method}' accept-charset='#{charset}'>"
38
+ end
31
39
 
32
40
  def form_body
33
41
  inputs = params.map do |key, value|
34
- "<input type='hidden' name='#{key}' value='#{value}'>"
42
+ form_input(key, value)
35
43
  end
44
+ inputs.unshift(csrf_token) if authenticity_token
36
45
  inputs.join
37
46
  end
38
47
 
39
- def form_head
40
- "<form id='#{form_id}' action='#{url}' method='#{method}'>"
48
+ def form_input(key, value)
49
+ if value.is_a?(Hash)
50
+ value.map do |inner_key, inner_value|
51
+ form_input("#{key}[#{inner_key}]", inner_value)
52
+ end.join
53
+ else
54
+ "<input type='hidden' name='#{key}' value='#{value}'>"
55
+ end
41
56
  end
42
57
 
43
58
  def form_footer
44
59
  "</form>"
45
60
  end
46
61
 
62
+ def csrf_token
63
+ "<input name='authenticity_token' value='#{authenticity_token}' type='hidden'>"
64
+ end
65
+
47
66
  def no_script
48
67
  "<noscript>
49
68
  #{submit_section}
@@ -1,3 +1,3 @@
1
1
  module Repost
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - YaroslavO
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-19 00:00:00.000000000 Z
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  requirements: []
89
- rubygems_version: 3.0.2
89
+ rubygems_version: 3.0.8
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: Gem implements Redirect using POST method