Class: Keypic::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/keypic/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Proxy

Returns a new instance of Proxy.



5
6
7
# File 'lib/keypic/proxy.rb', line 5

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/keypic/proxy.rb', line 3

def env
  @env
end

Instance Method Details

#check!Object



47
48
49
50
51
52
53
# File 'lib/keypic/proxy.rb', line 47

def check!
  body = Keypic::Client.new(:check_form).invoke

  if body && body["status"] == 'response'
    body["report"]
  end
end

#clear!Object



55
56
57
58
# File 'lib/keypic/proxy.rb', line 55

def clear!
  session.delete("keypic.token")
  @token = nil
end

#client_paramsObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/keypic/proxy.rb', line 34

def client_params
  {
    'ServerName' => env['SERVER_NAME'],
    'ClientIP' => env['REMOTE_ADDR'],
    'ClientUserAgent' => env['HTTP_USER_AGENT'],
    'ClientAccept' => env['HTTP_ACCEPT'],
    'ClientAcceptEncoding' => env['HTTP_ACCEPT_ENCODING'],
    'ClientAcceptLanguage' => env['HTTP_ACCEPT_LANGUAGE'],
    'ClientAcceptCharset' => env['HTTP_ACCEPT_CHARSET'],
    'ClientHttpReferer' => env['HTTP_REFERER']
  }
end

#fetch_rank(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/keypic/proxy.rb', line 21

def fetch_rank(options = {})
  params = normalize_message(options)
  params = { 'Token' => token }.merge(client_params).merge(params)

  body = Keypic::Client.new(:validate).invoke(params)

  if body && body["status"] == 'response'
    body["spam"].to_f
  else
    0.0
  end
end

#sessionObject



60
61
62
# File 'lib/keypic/proxy.rb', line 60

def session
  env["rack.session"] || {}
end

#spam?(options = {}, rank = 80) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/keypic/proxy.rb', line 17

def spam?(options = {}, rank = 80)
  !valid?(options, rank)
end

#tokenObject



9
10
11
# File 'lib/keypic/proxy.rb', line 9

def token
  @token ||= (fetch_token_from_session || fetch_token_form_server)
end

#valid?(options = {}, rank = 80) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/keypic/proxy.rb', line 13

def valid?(options = {}, rank = 80)
  fetch_rank(options) >= rank.to_f
end