Comments (7)
Hey John!
If you're using Rails there's a default middleware which sets those vars for the current request: https://github.com/joshfrench/rakismet/blob/master/lib/rakismet/middleware.rb (Which is why you need to call spam?
in situ, or store the details for later recall.)
If you're not using Rails, you'd have to adapt that or set those vars manually.
from rakismet.
Gotcha β but where/how in the model does it access those vars? Is that a
normal thing that an AR object can do when it's instantiated in a
controller action?
from rakismet.
So that middleware sets the current request data as class-level vars on the Rakismet
class, which is just a plain old Ruby object: https://github.com/joshfrench/rakismet/blob/master/lib/rakismet.rb#L30
Then spam?
and friends just look up whatever's currently stored in those Rakismet
class vars: https://github.com/joshfrench/rakismet/blob/master/lib/rakismet/model.rb#L76
Rakismet has no knowledge of AR, you could include rakismet/model
into any kind of object and get the same behavior.
from rakismet.
Ahhh.
So I suppose that means rakismet isn't thread-safe?
from rakismet.
It's not. There's an out-of-date PR that I'd gladly merge if someone wanted to bring that branch up to speed with master: #35
(Hint hint, nudge nudge.)
from rakismet.
Instead of explicitly supporting it with thread-local variables, what do you think of these ideasβ¦
-
pass the
request
object intospam?
and friends, which will then invoke.user_ip
etc on it. this can even be used outside of the context of a controller by passing in some other object with the same api.thing = Thing.new(params) raise "spam!" if thing.spam?(request) thing.save
-
have
include Rakismet::Model
invokeattr_accessor :user_ip, :user_agent, :referrer
(or maybe namespaced with something safer likerakismet_
orrequest_
). then there is the option to set these non-persited values in whatever context, be it a controller or otherwise. this might simplify the case ofthing = Thing.new(params) thing.user_ip, thing.user_agent, thing.referrer = request.user_ip, request.user_agent, request.referer raise "spam!" if thing.spam? thing.save
I like 1 a lot better but maybe there are reasons that 2 is more flexible or something.
WDYT?
from rakismet.
They're both breaking changes, but in hindsight I think explicitly passing the request data in some form is preferable to magically gleaning it from the environment.
The first option would be my choice, as it relieves the user of needing to know which request vars are important.
Open question: if I decide to store my own request info --
@comment = Comment.new({ user_ip: foo, ... })
And later I supply a different request env:
@comment.spam?({ user_ip: bar, ... })
Which value would you expect to take precedence?
from rakismet.
Related Issues (20)
- ArgumentError: invalid byte sequence in UTF-8 in ruby-1.9.2-p136 HOT 1
- rakismet_attrs not propogated to subclasses HOT 2
- Define raskimet attrs with accepts_nested_attributes_for HOT 4
- which is the best way to update my Table and define raskimet attributes ? HOT 6
- Doubts with the response HOT 4
- SocketError (getaddrinfo: nodename nor servname provided, or not known): HOT 1
- Turn off rakismet in tests HOT 1
- permalink never set (or used) HOT 7
- Rakismet don't work HOT 1
- Rails 4 not compatible?
- user_role is not working HOT 5
- List required fields? HOT 1
- Building headers with Rails.version causes NoMethodError
- Problem when validating extra attributes HOT 1
- permalink parameter is sent as comment_permalink HOT 2
- Unicode URL support. HOT 2
- Rakismet in test and development environments? HOT 5
- v1.0.1 doesn't work with Rails 2.3.10 (possibly anything pre Rails 3?) HOT 2
- rakismet-1.0.0/lib/rakismet/railtie.rb:12:in `key': wrong number of arguments(0 for 1) (ArgumentError) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rakismet.