katalyst-google-apis 1.3.0 → 1.3.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: 249511e97a7e28426cbf5ad750c3c77c1dc5f882c885a37868537bfea975a9e7
4
- data.tar.gz: 86758221a5cdc2a554d767a7e126b1acd48d3034b87100e23dd88876e291e713
3
+ metadata.gz: 91607481f87f73f9f4e17f687e6f9f98c6005ec7b25b90f2e16f3a96dd2f8286
4
+ data.tar.gz: 9663f5605e940f362fb2b7e9d7294890960c6666dfe0ae595e83c700c5e559be
5
5
  SHA512:
6
- metadata.gz: da31ead0972e8152ab79e04c2a18ed875dcc367187f8ee03a60f7d89337d85956f95f944f524852eddd8fbe940da4e45884bd39dae471d499134a19faaccea59
7
- data.tar.gz: ece80e3217d17d22c28f8b787c10ad843200b3c322b6dd1427b2a13ef3bdcbd4df484adeccef53ef83c5d12d104bbfc012fd0a49e39a5834e4a7dda7b71e393a
6
+ metadata.gz: 77b2105dd910de97aaf9c0eb80999d76f3e0f972bd9e8b29699c8daf33504166ceef279f07c601dd8ea19e6337d642a8e439ee50b55dc816251e617f4a1cc735
7
+ data.tar.gz: a2d8a5ed4d6f19fcf58380084400c355b1a013b71aa62e5eb5964b2a7fe32158b0e41a8233b0a8f718120d16cdade6ba4df856cbfdccea782cacc75d9d4329d9
@@ -2,28 +2,33 @@
2
2
 
3
3
  module Katalyst
4
4
  module GoogleApis
5
+ # rubocop:disable Rails/HelperInstanceVariable
5
6
  module FormBuilder
6
- def recaptcha_field(attribute = :recaptcha_token, action: object_name,
7
- site_key: GoogleApis.config.recaptcha.site_key)
8
- safe_join([
9
- RecaptchaField.new(action:, attribute:, site_key:).render(self),
10
- hidden_field(attribute),
11
- ])
7
+ ##
8
+ # :method: recaptcha_field
9
+ #
10
+ # :call-seq: recaptcha_field(method = :recaptcha_token, options = {})
11
+ #
12
+ # <%= form_with model: @contact do |f| %>
13
+ # <%= f.recaptcha_field %>
14
+ # <% end %>
15
+ #
16
+ # Please refer to the documentation of the base helper for details.
17
+ def recaptcha_field(attribute = :recaptcha_token, site_key: GoogleApis.config.recaptcha.site_key, **)
18
+ RecaptchaField.new(@object_name, attribute, site_key:, **).render(@template)
12
19
  end
13
20
 
14
21
  class RecaptchaField
15
- attr_reader :action, :attribute, :site_key
16
-
17
- def initialize(action:, attribute:, site_key:)
18
- # rubocop:disable Rails/HelperInstanceVariable
19
- @action = action
20
- @attribute = attribute
21
- @site_key = site_key
22
- # rubocop:enable Rails/HelperInstanceVariable
22
+ def initialize(object_name, attribute, site_key:, **html_attributes)
23
+ @object_name = object_name
24
+ @attribute = attribute
25
+ @site_key = site_key
26
+ @html_attributes = html_attributes
23
27
  end
24
28
 
25
29
  def render(template)
26
- template.tag.div(data:)
30
+ template.safe_join([template.tag.div(**@html_attributes, data:),
31
+ template.hidden_field(@object_name, @attribute)])
27
32
  end
28
33
 
29
34
  private
@@ -32,11 +37,12 @@ module Katalyst
32
37
  {
33
38
  controller: "recaptcha",
34
39
  action: "turbo:before-morph-element->recaptcha#morph",
35
- recaptcha_action_value: action,
36
- recaptcha_site_key_value: site_key,
40
+ recaptcha_action_value: @object_name,
41
+ recaptcha_site_key_value: @site_key,
37
42
  }
38
43
  end
39
44
  end
40
45
  end
46
+ # rubocop:enable Rails/HelperInstanceVariable
41
47
  end
42
48
  end
@@ -3,13 +3,73 @@
3
3
  module Katalyst
4
4
  module GoogleApis
5
5
  module GOVUKFormBuilder
6
- def govuk_recaptcha_field(attribute = :recaptcha_token, **)
7
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(self, object_name, attribute).html do
6
+ # Generates a Google recaptcha input that shows errors.
7
+ #
8
+ # @param attribute_name [Symbol] The name of the attribute
9
+ # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
10
+ # @param form_group [Hash] configures the form group
11
+ # @option form_group kwargs [Hash] additional attributes added to the form group
12
+ # @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
13
+ # @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
14
+ # @return [ActiveSupport::SafeBuffer] HTML output
15
+ #
16
+ # @example A google recaptcha input
17
+ # = f.govuk_recaptcha_field
18
+ #
19
+ def govuk_recaptcha_field(attribute_name = :recaptcha_token,
20
+ form_group: {}, before_input: nil, after_input: nil, **)
21
+ Recaptcha.new(self, object_name, attribute_name, form_group:, before_input:, after_input:, **).html
22
+ end
23
+
24
+ class Recaptcha < GOVUKDesignSystemFormBuilder::Base
25
+ include GOVUKDesignSystemFormBuilder::Traits::Error
26
+ include GOVUKDesignSystemFormBuilder::Traits::HTMLAttributes
27
+ include GOVUKDesignSystemFormBuilder::Traits::ContentBeforeAndAfter
28
+
29
+ def initialize(builder, object_name, attribute_name, form_group:, before_input:, after_input:, **kwargs)
30
+ super(builder, object_name, attribute_name)
31
+
32
+ @html_attributes = kwargs
33
+ @form_group = form_group
34
+ @before_input = before_input
35
+ @after_input = after_input
36
+ end
37
+
38
+ def html
39
+ GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(*bound, **@form_group).html do
40
+ safe_join([error_element, content])
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def content
8
47
  safe_join([
9
- GOVUKDesignSystemFormBuilder::Elements::ErrorMessage.new(self, object_name, attribute),
10
- recaptcha_field(attribute, **),
48
+ before_input_content,
49
+ input,
50
+ after_input_content,
11
51
  ])
12
52
  end
53
+
54
+ def input
55
+ @builder.send(:recaptcha_field, @attribute_name, **attributes(@html_attributes))
56
+ end
57
+
58
+ def options
59
+ {
60
+ id: field_id(link_errors: true),
61
+ class: classes,
62
+ aria: { describedby: combine_references(error_id) },
63
+ }
64
+ end
65
+
66
+ def classes
67
+ [%(#{brand}-recaptcha)].push(error_classes).compact
68
+ end
69
+
70
+ def error_classes
71
+ %(#{brand}-recaptcha--error) if has_errors?
72
+ end
13
73
  end
14
74
  end
15
75
  end
@@ -8,8 +8,8 @@ module Katalyst
8
8
  def initialize(code:, status:, message:, details: nil)
9
9
  super(message)
10
10
 
11
- @code = code
12
- @status = status
11
+ @code = code
12
+ @status = status
13
13
  @details = details
14
14
  end
15
15
 
@@ -16,11 +16,11 @@ module Katalyst
16
16
 
17
17
  def initialize(credentials:, model:, parent:, attempt:, retries:, jitter:)
18
18
  @credentials = credentials
19
- @model = model
20
- @parent = parent
21
- @attempt = attempt
22
- @retries = retries
23
- @jitter = jitter
19
+ @model = model
20
+ @parent = parent
21
+ @attempt = attempt
22
+ @retries = retries
23
+ @jitter = jitter
24
24
  end
25
25
 
26
26
  def call(payload:)
@@ -101,8 +101,8 @@ module Katalyst
101
101
  Kernel.sleep(backoff)
102
102
 
103
103
  @response = nil
104
- @result = nil
105
- @error = nil
104
+ @result = nil
105
+ @error = nil
106
106
  @attempt += 1
107
107
  true
108
108
  else
@@ -74,7 +74,7 @@ module Katalyst
74
74
  location = first_location&.dig(:location)
75
75
  return if location.blank?
76
76
 
77
- latitude = location[:latitude]
77
+ latitude = location[:latitude]
78
78
  longitude = location[:longitude]
79
79
  return if latitude.nil? || longitude.nil?
80
80
 
@@ -23,7 +23,7 @@ module Katalyst
23
23
 
24
24
  def call(address:, bounds:)
25
25
  @address = address
26
- @bounds = bounds
26
+ @bounds = bounds
27
27
 
28
28
  @response = Curl.get(url, **params) do |http|
29
29
  http.headers["Content-Type"] = "application/json; UTF-8"
@@ -75,7 +75,7 @@ module Katalyst
75
75
  location = first_location&.dig(:location)
76
76
  return if location.blank?
77
77
 
78
- latitude = location[:latitude]
78
+ latitude = location[:latitude]
79
79
  longitude = location[:longitude]
80
80
  return if latitude.nil? || longitude.nil?
81
81
 
@@ -95,7 +95,7 @@ module Katalyst
95
95
  def params
96
96
  low, high = @bounds.split("|")
97
97
 
98
- low_lat, low_lng = low.split(",")
98
+ low_lat, low_lng = low.split(",")
99
99
  high_lat, high_lng = high.split(",")
100
100
 
101
101
  {
@@ -14,7 +14,7 @@ module Katalyst
14
14
 
15
15
  def initialize(credentials:, parent:)
16
16
  @credentials = credentials
17
- @parent = parent
17
+ @parent = parent
18
18
  end
19
19
 
20
20
  def call(assessment:)
@@ -3,20 +3,19 @@
3
3
  module Katalyst
4
4
  module GoogleApis
5
5
  class Config
6
- include ActiveSupport::Configurable
6
+ attr_accessor :project_id, :project_number, :service_account_email, :identity_pool, :identity_provider, :recaptcha
7
7
 
8
- config_accessor(:project_id) { ENV.fetch("GOOGLE_PROJECT_ID", nil) }
9
- config_accessor(:project_number) { ENV.fetch("GOOGLE_PROJECT_NUMBER", nil) }
10
- config_accessor(:service_account_email) { ENV.fetch("GOOGLE_SERVICE_ACCOUNT_EMAIL", nil) }
11
- config_accessor(:identity_pool) { ENV.fetch("GOOGLE_OIDC_IDENTITY_POOL", nil) }
12
- config_accessor(:identity_provider) { ENV.fetch("GOOGLE_OIDC_IDENTITY_PROVIDER", nil) }
13
-
14
- config_accessor(:recaptcha) do
15
- defaults = ActiveSupport::OrderedOptions.new
16
- defaults.site_key = ENV.fetch("GOOGLE_RECAPTCHA_SITE_KEY", nil)
17
- defaults.score = ENV.fetch("GOOGLE_RECAPTCHA_SCORE", 0.5).to_f
18
- defaults.test_mode = !ENV.fetch("VERIFY_RECAPTCHA", !Rails.env.local?) # rubocop:disable Rails/UnknownEnv
19
- defaults
8
+ def initialize
9
+ @project_id = ENV.fetch("GOOGLE_PROJECT_ID", nil)
10
+ @project_number = ENV.fetch("GOOGLE_PROJECT_NUMBER", nil)
11
+ @service_account_email = ENV.fetch("GOOGLE_SERVICE_ACCOUNT_EMAIL", nil)
12
+ @identity_pool = ENV.fetch("GOOGLE_OIDC_IDENTITY_POOL", nil)
13
+ @identity_provider = ENV.fetch("GOOGLE_OIDC_IDENTITY_PROVIDER", nil)
14
+ @recaptcha = ActiveSupport::OrderedOptions.new.tap do |defaults|
15
+ defaults.site_key = ENV.fetch("GOOGLE_RECAPTCHA_SITE_KEY", nil)
16
+ defaults.score = ENV.fetch("GOOGLE_RECAPTCHA_SCORE", 0.5).to_f
17
+ defaults.test_mode = !ENV.fetch("VERIFY_RECAPTCHA", !Rails.env.local?)
18
+ end
20
19
  end
21
20
  end
22
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-google-apis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 4.0.3
115
+ rubygems_version: 4.0.10
116
116
  specification_version: 4
117
117
  summary: Google REST APIs for use in Rails projects
118
118
  test_files: []