Skip to content

Add Reply class to handle Recaptcha responses#466

Merged
grosser merged 9 commits into
ambethia:masterfrom
RubenIgnacio:add_recaptcha_reply_class
Jul 20, 2025
Merged

Add Reply class to handle Recaptcha responses#466
grosser merged 9 commits into
ambethia:masterfrom
RubenIgnacio:add_recaptcha_reply_class

Conversation

@RubenIgnacio
Copy link
Copy Markdown
Contributor

@RubenIgnacio RubenIgnacio commented Jul 18, 2025

Add Recaptcha::Reply class to standardize and simplify response handling for both free and enterprise versions. It also defines the []= method for backward compatibility and prevents the Recaptcha failure after API call. API reply: #{@_recaptcha_reply}. error message when using recaptcha enterprise.

@grosser
Copy link
Copy Markdown
Collaborator

grosser commented Jul 19, 2025

yeah cleans up the logic nicely

Comment thread lib/recaptcha/reply.rb Outdated
Comment thread lib/recaptcha/reply.rb Outdated
Comment on lines +27 to +29
return raw_reply['hostname'] unless enterprise?

token_properties&.dig('hostname')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be cleaner as if enterprise? x else y end

Comment thread lib/recaptcha.rb Outdated
Comment on lines +83 to +87
success = reply.success? &&
hostname_valid?(reply.hostname, options[:hostname]) &&
action_valid?(reply.action, options[:action]) &&
score_above_threshold?(reply.score, options[:minimum_score]) &&
score_below_threshold?(reply.score, options[:maximum_score])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be dried up since they are the same now ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I can put that validations in the class

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was more thinking as a reused method in recaptcha.rb but in the class would also work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i moved the validation to the success? method, then I added the success method to allow access to the raw value of that key. What do you think?

Comment thread lib/recaptcha/reply.rb Outdated
Comment on lines +61 to +63
def free?
!enterprise?
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would actually avoid having 2 different methods, so the code is easier to reason about since it has less concepts to keep in mind

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so remove the free? method?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove it to simplify

Comment thread lib/recaptcha/reply.rb Outdated
Comment thread lib/recaptcha/reply.rb
@raw_reply['tokenProperties'] if enterprise?
end

def success?(options = {})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a ? return a non-bool is a bit sus

Suggested change
def success?(options = {})
def success(options = {})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, in that case we can keep success? like a boolean for test success and add validate_success method. What do you think?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe keep success? here and make the caller do the if options[:with_reply] == true dance

Comment thread lib/recaptcha/reply.rb Outdated
Comment on lines +61 to +63
def free?
!enterprise?
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove it to simplify

@grosser
Copy link
Copy Markdown
Collaborator

grosser commented Jul 19, 2025

just some rubocop, rest seems fine 🎉

RubenIgnacio and others added 2 commits July 19, 2025 19:16
Co-authored-by: Michael Grosser <michael@grosser.it>
@RubenIgnacio
Copy link
Copy Markdown
Contributor Author

just some rubocop, rest seems fine 🎉

it seems it's because I forgot to add # frozen_string_literal: true. I'll add it now

@grosser grosser merged commit 089f81d into ambethia:master Jul 20, 2025
7 checks passed
@grosser
Copy link
Copy Markdown
Collaborator

grosser commented Jul 20, 2025

I'll do the polish :)
... do you want this released or this was just for fun refactor ?

@grosser
Copy link
Copy Markdown
Collaborator

grosser commented Jul 20, 2025

👁️ #467

@RubenIgnacio
Copy link
Copy Markdown
Contributor Author

RubenIgnacio commented Jul 20, 2025 via email

@grosser
Copy link
Copy Markdown
Collaborator

grosser commented Jul 21, 2025

5.20.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants