Class: Mollom

Inherits:
Object
  • Object
show all
Includes:
ApiCompatibility
Defined in:
lib/mollom.rb,
lib/mollom/content_response.rb,
lib/mollom/api_compatibility.rb

Overview

Mollom API requires this to change, but this gives a warning! XMLRPC::Client::USER_AGENT = “Ruby Mollom/0.1”

Defined Under Namespace

Modules: ApiCompatibility, Errors Classes: ContentResponse, Error, NoAvailableServers

Constant Summary collapse

API_VERSION =
'1.0'
STATIC_SERVER_LIST =
[{:proto => 'http', :host => 'xmlrpc3.mollom.com'},
{:proto => 'http', :host => 'xmlrpc2.mollom.com'},
{:proto => 'http', :host => 'xmlrpc1.mollom.com'}].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiCompatibility

included

Constructor Details

#initialize(options = {}) ⇒ Mollom

Creates a new Mollom object. Takes private_key and public_key as keys.

Mollom.new(:private_key => 'qopzalnzanzajlazjna', :public_key => 'aksakzaddazidzaodjaz')
# => #<Mollom:0x5b6454 @public_key="aksakzaddazidzaodjaz", @private_key="qopzalnzanzajlazjna">


29
30
31
32
# File 'lib/mollom.rb', line 29

def initialize options = {}
  @private_key = options[:private_key]
  @public_key = options[:public_key]
end

Instance Attribute Details

#private_keyObject

Returns the value of attribute private_key.



22
23
24
# File 'lib/mollom.rb', line 22

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



22
23
24
# File 'lib/mollom.rb', line 22

def public_key
  @public_key
end

Instance Method Details

#audio_captcha(info = {}) ⇒ Object

Requests an Audio captcha from Mollom. It takes the optional session_id and author_ip keys, if you allready have a session. It returns a hash with the URL where the captcha can be found, and the session_id, to keep track of the current session (Needed later in Mollom#check_captcha)

captcha = mollom.audio_captcha :author_ip => '172.16.0.2', :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d'
captcha['url']        # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.mp3
captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d


76
77
78
# File 'lib/mollom.rb', line 76

def audio_captcha info = {}
  send_command('mollom.getAudioCaptcha', info)
end

#check_content(content = {}) ⇒ Object

Checks the content whether it is spam, ham (not spam), or undecided, and gives a quality assessment of the content. Possible content keys are:

session_id     # => If you allready have a session_id
post_title     # => The title
post_body      # => The main content of the post.
author_name    # => The name of the post author
author_url     # => The url the author enters
author_mail    # => The author's email address
author_ip      # => The author's IP address
author_openid  # => The author's OpenID
author_id      # => The author's ID
checks         # => A comma-separated list of checks. Available checks include 'spam', 'quality', 'profanity', 'sentiment' and 'language'. E.g. 'spam,quality,sentiment'

Only the post_body key is required, all other keys are optional. This function returns a ContentResponse object.

response = mollom.check_content :post_title => 'Mollom rules!', 
                                :post_body => 'I think that mollom is so cool!', 
                                :author_name => 'Jan De Poorter', 
                                :author_url => 'http://www.openminds.be'
response.spam? # => false
response.ham?  # => true


56
57
58
# File 'lib/mollom.rb', line 56

def check_content content = {}
  ContentResponse.new(send_command('mollom.checkContent', content))
end

#image_captcha(info = {}) ⇒ Object

Requests an Image captcha from Mollom. It takes the optional session_id and author_ip keys, if you allready have a session. It returns a hash with the URL where the captcha can be found, and the session_id, to keep track of the current session (Needed later in Mollom#check_captcha)

captcha = mollom.image_captcha :author_ip => '172.16.0.1'
captcha['url']        # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.png
captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d


66
67
68
# File 'lib/mollom.rb', line 66

def image_captcha info = {}
  send_command('mollom.getImageCaptcha', info)
end

#key_ok?Boolean

Standard check to see if your public/private keypair are recognized. Takes no options

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/mollom.rb', line 92

def key_ok?
  send_command('mollom.verifyKey')
rescue XMLRPC::FaultException
  false
end

#language_for(text) ⇒ Object

Gets language of input text.

mollom.language_for ‘This is my text’



130
131
132
# File 'lib/mollom.rb', line 130

def language_for text
  send_command('mollom.detectLanguage', :text => text)
end

#send_feedback(feedback = {}) ⇒ Object

Send feedback to Mollom about a certain content. Required keys are session_id and feedback.

Feedback can be any of

spam
profanity
low-quality
unwanted

mollom.send_feedback :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :feedback => 'unwanted'


123
124
125
# File 'lib/mollom.rb', line 123

def send_feedback feedback = {}
  send_command('mollom.sendFeedback', feedback)
end

#server_list(refresh = false) ⇒ Object

Gets a list of servers from Mollom. You should cache this information in your application in a temporary file or in a database. You can set this with Mollom#server_list=

Takes an optional parameter refresh, which resets the cached value.

mollom.server_list
# => [{:proto=>"http", :host=>"88.151.243.81"}, {:proto=>"http", :host=>"82.103.131.136"}]

Raises:



140
141
142
143
144
145
146
147
148
# File 'lib/mollom.rb', line 140

def server_list refresh = false
  return @server_list if @server_list && !refresh
  STATIC_SERVER_LIST.each do |static_server|
    @server_list = get_server_list_from(static_server)
    return @server_list if @server_list
  end
  # Should have returned a server_list here..
  raise(Error.new("Can't get mollom server-list"))
end

#server_list=(list) ⇒ Object

Sets the server list used to contact Mollom. This should be used to set the list of cached servers.

If you try to set a faulty server list, the function will silently fail, so we can get the server-list from Mollom.



153
154
155
156
157
158
# File 'lib/mollom.rb', line 153

def server_list=(list)
  # Check if we get an actual serverlist-array
  if list.is_a?(Array) && list.all? {|hash| hash.has_key?(:host) && hash.has_key?(:proto) } 
    @server_list = list
  end
end

#statistics(options = {}) ⇒ Object

Gets some statistics from Mollom about your site.

The type has to be passed. Possible types:

total_days
total_accepted
total_rejected
yesterday_accepted
yesterday_rejected
today_accepted
today_rejected

mollom.statistics :type => 'total_accepted' # => 123


110
111
112
# File 'lib/mollom.rb', line 110

def statistics options = {}
  send_command('mollom.getStatistics', options)
end

#valid_captcha?(info = {}) ⇒ Boolean

Checks with mollom if the given captcha (by the user) is correct. Takes session_id and solution keys. Both keys are required. Returns true if the captcha is valid, false if it is incorrect

captcha = mollom.image_captcha :author_ip => '172.16.0.1'
# show to user... input from user
return = mollom.valid_captcha? :session_id => captcha['session_id'], :solution => 'abcDe9'
return # => true

Returns:

  • (Boolean)


87
88
89
# File 'lib/mollom.rb', line 87

def valid_captcha? info = {}
  send_command('mollom.checkCaptcha', info)
end