nlpcaptcha 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ require 'nlpcaptcha/configuration'
2
+ require 'nlpcaptcha/client_helper'
3
+ require 'nlpcaptcha/verify'
4
+
5
+ module Nlpcaptcha
6
+ NLPCAPTCHA_JS_URL = 'http://call.nlpcaptcha.in/js/captcha.js'
7
+ NLPCAPTCHA_VALIDATE_URL = 'http://call.nlpcaptcha.in/index.php/ad/validate'
8
+
9
+
10
+ SKIP_VERIFY_ENV = ['test', 'cucumber']
11
+
12
+ # Gives access to the current Configuration.
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ # Allows easy setting of multiple configuration options. See Configuration
18
+ # for all available options.
19
+ #--
20
+ # The temp assignment is only used to get a nicer rdoc. Feel free to remove
21
+ # this hack.
22
+ #++
23
+ def self.configure
24
+ config = configuration
25
+ yield(config)
26
+ end
27
+
28
+ def self.with_configuration(config)
29
+ original_config = {}
30
+
31
+ config.each do |key, value|
32
+ original_config[key] = configuration.send(key)
33
+ configuration.send("#{key}=", value)
34
+ end
35
+
36
+ result = yield if block_given?
37
+
38
+ original_config.each { |key, value| configuration.send("#{key}=", value) }
39
+ result
40
+ end
41
+
42
+ class NlpcaptchaError < StandardError
43
+ end
44
+ end
45
+
46
+ if defined?(Rails)
47
+ require 'nlpcaptcha/rails'
48
+ end
@@ -0,0 +1,22 @@
1
+ module Nlpcaptcha
2
+ module ClientHelper
3
+ # Your public API can be specified in the +options+ hash or preferably
4
+ # using the Configuration.
5
+ def nlpcaptcha_tags(options = {})
6
+ # Default options
7
+ key = options[:publisher_key] ||= Nlpcaptcha.configuration.publisher_key
8
+ raise NlpcaptchaError, "No publisher key specified." unless key
9
+ error = options[:error] ||= (defined? flash ? flash[:nlpcaptcha_error] : "")
10
+ uri = Nlpcaptcha.configuration.js_url
11
+ html = ""
12
+ html << <<-EOS
13
+
14
+ <script type="text/javascript"> var NLPOptions = {key:'#{key}'};</script>
15
+ <script type="text/javascript" src="#{uri}" ></script>
16
+
17
+ EOS
18
+
19
+ return (html.respond_to?(:html_safe) && html.html_safe) || html
20
+ end # nlpcaptcha_tags
21
+ end # ClientHelper
22
+ end # Nlpcaptcha
@@ -0,0 +1,50 @@
1
+ module Nlpcaptcha
2
+ # This class enables detailed configuration of the nlpcaptcha services.
3
+ #
4
+ # By calling
5
+ #
6
+ # Nlpcaptcha.configuration # => instance of Nlpcaptcha::Configuration
7
+ #
8
+ # or
9
+ # Nlpcaptcha.configure do |config|
10
+ # config # => instance of Nlpcaptcha::Configuration
11
+ # end
12
+ #
13
+ # you are able to perform configuration updates.
14
+ #
15
+ # Your are able to customize all attributes listed below. All values have
16
+ # sensitive default and will very likely not need to be changed.
17
+ #
18
+ #The keys may be set via the Shell enviroment or using this configuration. Settings within this configuration always take
19
+ # precedence.
20
+ #
21
+ # Setting the keys with this Configuration
22
+ #
23
+ # Nlpcaptcha.configure do |config|
24
+ # config.publisher_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
25
+ # config.validator_key='YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
26
+ # config.private_key='ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'
27
+ # config.proxy = 'http://myproxy.com.au:8080'
28
+ # end
29
+ class Configuration
30
+ attr_accessor :js_url,
31
+ :validate_url,
32
+ :skip_verify_env,
33
+ :publisher_key,
34
+ :validator_key,
35
+ :private_key,
36
+ :proxy
37
+
38
+
39
+ def initialize #:nodoc:
40
+ @js_url = NLPCAPTCHA_JS_URL
41
+ @validate_url = NLPCAPTCHA_VALIDATE_URL
42
+ @skip_verify_env = SKIP_VERIFY_ENV
43
+ @validator_key = ENV['NLPCAPTCHA_VALIDATOR_KEY']
44
+ @private_key = ENV['NLPCAPTCHA_PRIVATE_KEY']
45
+ @publisher_key = ENV['NLPCAPTCHA_PUBLISHER_KEY']
46
+ end
47
+
48
+
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ require 'net/http'
2
+ require 'nlpcaptcha'
3
+
4
+ ActionView::Base.send(:include, Nlpcaptcha::ClientHelper)
5
+ ActionController::Base.send(:include, Nlpcaptcha::Verify)
@@ -0,0 +1,15 @@
1
+ require 'net/http'
2
+ require 'nlpcaptcha'
3
+ module Rails
4
+ module Nlpcaptcha
5
+ class Railtie < Rails::Railtie
6
+ initializer "setup config" do
7
+ begin
8
+ ActionView::Base.send(:include, ::Nlpcaptcha::ClientHelper)
9
+ ActionController::Base.send(:include, ::Nlpcaptcha::Verify)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,60 @@
1
+ require "uri"
2
+ module Nlpcaptcha
3
+ module Verify
4
+ # Your private API can be specified in the +options+ hash or preferably
5
+ # using the Configuration.
6
+ def NLPValidate(options = {})
7
+ if !options.is_a? Hash
8
+ options = {:model => options}
9
+ end
10
+
11
+ env = options[:env] || ENV['RAILS_ENV']
12
+ return true if Nlpcaptcha.configuration.skip_verify_env.include? env
13
+ model = options[:model]
14
+ attribute = options[:attribute] || :base
15
+ validator_key = options[:validator_key] || Nlpcaptcha.configuration.validator_key
16
+ raise NlpcaptchaError, "No Validator key specified." unless validator_key
17
+
18
+ begin
19
+ nlpcaptcha = nil
20
+ if(Nlpcaptcha.configuration.proxy)
21
+ proxy_server = URI.parse(Nlpcaptcha.configuration.proxy)
22
+ http = Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
23
+ else
24
+ http = Net::HTTP
25
+ end
26
+
27
+ Timeout::timeout(options[:timeout] || 3) do
28
+ nlpcaptcha = http.post_form(URI.parse(Nlpcaptcha.configuration.validate_url), {
29
+ "ValidateKey" => validator_key,
30
+ "Identifier" => params[:nlpIdentifier],
31
+ "Answer" => params[:nlpAnswer]
32
+ })
33
+ end
34
+ answer, error = nlpcaptcha.body.split(":@NLP@:").map { |s| s.chomp }
35
+ unless answer == 'success'
36
+ flash[:nlpcaptcha_error] = error
37
+ if model
38
+ message = "Your Response Could not be Validated"
39
+ message = I18n.translate(:'nlpcaptcha.errors.validation_failed', {:default => message}) if defined?(I18n)
40
+ model.errors.add attribute, options[:message] || message
41
+ end
42
+ return false
43
+ else
44
+ flash[:nlpcaptcha_error] = nil
45
+ return true
46
+ end
47
+ rescue Timeout::Error
48
+ flash[:nlpcaptcha_error] = "nlpcaptcha-not-reachable"
49
+ if model
50
+ message = "An error occured while connecting to the captcha validation service. Please try again."
51
+ message = I18n.translate(:'nlpcaptcha.errors.sever_unreachable', :default => message) if defined?(I18n)
52
+ model.errors.add attribute, options[:message] || message
53
+ end
54
+ return false
55
+ rescue Exception => e
56
+ raise NlpcaptchaError, e.message, e.backtrace
57
+ end
58
+ end # verify_nlpcaptcha
59
+ end # Verify
60
+ end # Nlpcaptcha
@@ -0,0 +1,3 @@
1
+ module Nlpcaptcha
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nlpcaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - NLPCaptcha.com
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mocha
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: i18n
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Rails Plugin For NLPCaptcha spam prevention system
79
+ email:
80
+ - support@nlpcaptcha.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/nlpcaptcha.rb
86
+ - lib/nlpcaptcha/client_helper.rb
87
+ - lib/nlpcaptcha/configuration.rb
88
+ - lib/nlpcaptcha/rails.rb
89
+ - lib/nlpcaptcha/railtie.rb
90
+ - lib/nlpcaptcha/verify.rb
91
+ - lib/nlpcaptcha/version.rb
92
+ homepage: http://www.nlpcaptcha.com
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.24
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Rails Plugin For NLPCaptcha spam prevention system
116
+ test_files: []