Class: AYAH::Integration

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

Overview

Are You a Human Publisher Integration Rubygem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher_key, scoring_key, ayah_server = 'ws.areyouahuman.com') ⇒ Integration

Returns a new instance of Integration.



19
20
21
22
23
24
25
# File 'lib/ayah_integration.rb', line 19

def initialize(publisher_key, scoring_key, ayah_server = 'ws.areyouahuman.com')
  @publisher_key = publisher_key
  @scoring_key = scoring_key
  @ayah_server = ayah_server
  @passed = nil
  @converted = nil
end

Instance Attribute Details

#__last_errorObject (readonly)

Returns the value of attribute __last_error.



11
12
13
# File 'lib/ayah_integration.rb', line 11

def __last_error
  @__last_error
end

#__score_result_status_codeObject (readonly)

Returns the value of attribute __score_result_status_code.



11
12
13
# File 'lib/ayah_integration.rb', line 11

def __score_result_status_code
  @__score_result_status_code
end

#ayah_serverObject

Returns the value of attribute ayah_server.



10
11
12
# File 'lib/ayah_integration.rb', line 10

def ayah_server
  @ayah_server
end

#convertedObject (readonly)

Returns the value of attribute converted.



11
12
13
# File 'lib/ayah_integration.rb', line 11

def converted
  @converted
end

#passedObject (readonly)

Returns the value of attribute passed.



11
12
13
# File 'lib/ayah_integration.rb', line 11

def passed
  @passed
end

#publisher_keyObject

Returns the value of attribute publisher_key.



10
11
12
# File 'lib/ayah_integration.rb', line 10

def publisher_key
  @publisher_key
end

#scoring_keyObject

Returns the value of attribute scoring_key.



10
11
12
# File 'lib/ayah_integration.rb', line 10

def scoring_key
  @scoring_key
end

Instance Method Details

#get_publisher_htmlString

returns the HTML string that should be embedded into the form page to be protected

Returns:

  • (String)

    the HTML string



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

def get_publisher_html
  "<div id='AYAH'></div><script src='https://#{@ayah_server}/ws/script/#{@publisher_key}'></script>"
end

#last_errorString

returns the last error, if any, that occurred

Returns:

  • (String)

    the last error



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

def last_error
  @__last_error
end

#record_conversion(session_secret) ⇒ String

returns the HTML to be embedded in the landing page of a completed and converted session

Parameters:

  • session_secret (String)

    the session_secret for the session that converted

Returns:

  • (String)

    the HTML string



68
69
70
71
# File 'lib/ayah_integration.rb', line 68

def record_conversion(session_secret)
  @converted = true
  "<iframe style='border: none;' height='0' width='0' src='https://#{@ayah_server}/ws/recordConversion/#{session_secret}\"></iframe>"
end

#score_result(session_secret, client_ip = '0.0.0.0') ⇒ True, False

returns the score of the current session

Parameters:

  • session_secret (String)

    the session_secret for the session to be scored

  • client_ip (String) (defaults to: '0.0.0.0')

    the ip address being used by the client

Returns:

  • (True, False)

    whether the session passed



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ayah_integration.rb', line 39

def score_result(session_secret, client_ip='0.0.0.0')
  
  # set up the url and form vars for web service submission
  sr_url = "https://#{@ayah_server}/ayahwebservices/index.php/ayahwebservice/scoreGame"
  payload = {:session_secret => session_secret, :client_ip => client_ip}
  
  result = RestClient.post sr_url, payload, :content_type => 'application/x-www-form-urlencoded'
  
  # if there was an HTTP error, fail them automatically
  unless result.code == 200
    log_error("ERROR: HTTP Response code: #{result.code}")
    @passed = false
    return @passed
  end
  
  json = JSON.parse(result)
  
  # set the score result and see if they passed
  unless json['status_code'].nil?
    @__score_result_status_code = json['status_code'].to_i
    @passed = (json['status_code'].to_i ==  1)
    return @passed
  end
end