yinxiangma 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,11 @@
1
+ == 0.0.3 / 2012-01-04
2
+
3
+ * Added Basic Validation
4
+
5
+ == 0.0.2 / 2012-01-04
6
+
7
+ * Added tests & error handles
8
+
9
+ == 0.0.1 / 2012-01-03
10
+
11
+ * Just display
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yinxiangma.gemspec
4
+ gemspec
@@ -0,0 +1,113 @@
1
+ = YinXiangMa CAPTCHA
2
+
3
+ Author:: SouthWolf
4
+ Copyright:: Copyright (c) 2011 SouthWolf, Yinxiangma Inc.
5
+ License:: {MIT}[http://creativecommons.org/licenses/MIT/]
6
+ Info:: http://github.com/southwolf/yinxiangma
7
+ Git:: http://github.com/southwolf/yinxiangma/tree/master
8
+ Bugs:: http://github.com/southwolf/yinxiangma/issues
9
+
10
+ This plugin adds helpers for the {YinXiangMa CAPTCHA API}[http://www.yinxiangma.com]. In your
11
+ views you can use the +yinxiangma_tags+ method to embed the needed javascript,
12
+ and you can validate in your controllers with +yinxiangma_valid+.
13
+
14
+ Beforehand you need to configure YinXiangMa with your custom publisher
15
+ key. You may find detailed examples below. Exceptions will be raised if you
16
+ call these methods and the keys can't be found.
17
+
18
+ == About this gem
19
+
20
+ This gem tries to introduces a more convenient way to configure YinXiangMa's
21
+ settings. The structure & style are inspired by {reCAPTCHA gem}[http://ambethia.com/recaptcha]
22
+
23
+ == Rails Installation
24
+
25
+ YinXiangMa for Rails, add this to your Gemfile:
26
+
27
+ gem "yinxiangma", :require => "yinxiangma/rails"
28
+
29
+ Or, it can be installed as a gem:
30
+
31
+ config.gem "yinxiangma", :lib => "yinxiangma/rails"
32
+
33
+ Or, as a standard rails plugin:
34
+
35
+ script/plugin install git://github.com/southwolf/yinxiangma.git
36
+
37
+ == Setting up your API Keys
38
+
39
+ There are multiple ways to setup your YinXiangMa API key once you
40
+ {obtain}[http://www.yinxiangma.com] one.
41
+
42
+ === YinXiangMa.configure
43
+
44
+ You may use the block style configuration. The following code could be placed
45
+ into a +config/initializers/yinxiangma.rb+ when used in a Rails project.
46
+
47
+ Yinxiangma.configure do |config|
48
+ config.publisher_key = 'cb9d0fe8dabdea89019d845be3059973'
49
+ config.mode ='live'
50
+ end
51
+
52
+
53
+ This way, you may also set additional options to fit YinXiangMa into your
54
+ deployment environment.
55
+
56
+ === Shell environment
57
+
58
+ Or, you can keep your keys out of your code base by exporting the following
59
+ environment variables. You might do this in the .profile/rc, or equivalent for
60
+ the user running your application:
61
+
62
+ export YINXIANGMA_PUBLISHER_KEY = 'cb9d0fe8dabdea89019d845be3059973'
63
+
64
+ === Per call
65
+
66
+ You can also pass in your keys as options at runtime, for example:
67
+
68
+ yinxiangma_tags :publisher_key => 'cb9d0fe8dabdea89019d845be3059973'
69
+
70
+ This option might be useful, if the same code base is used for multiple
71
+ YinXiangMa setups.
72
+
73
+ == To use 'YinXiangMa'
74
+
75
+ Add +yinxiangma_tags+ to each form you want to protect.
76
+
77
+ And, add +yinxiangma_valid+ logic to each form action that you've protected.
78
+
79
+ === +yinxiangma_tags+
80
+
81
+ This method generate HTML tag including YinXiangMa widget.
82
+
83
+ === +yinxiangma_valid+
84
+
85
+ This method returns +true+ or +false+ after processing the parameters from the YinXiangMa widget.
86
+
87
+ == TODO
88
+
89
+ * Validation Error handlings
90
+ * Better documentation
91
+ * Switch back to local CAPTCHA or reCAPTHA when YinXiangMa is unavailable
92
+
93
+ = 印象码 动态验证码
94
+
95
+ {印象码}[http://www.yinxiangma.com]是目前中国国内推出的第一款视频广告验证码云服务平台,不仅提供了安全的基于云计算的视频验证码服务,为网站提供了良好的验证码用户体验,并且提出了新一代的基于用户反馈的广告计费商业模式概念。
96
+
97
+ 本gem提供了Rails下的印象码功能,配置简单。
98
+
99
+ 首先请注册{印象码}[http://www.yinxiangma.com]服务,获取API key
100
+ 建立+config/initializers/yinxiangma.rb+ 内容如下(请将publisher_key替换成您的key)
101
+
102
+ Yinxiangma.configure do |config|
103
+ config.publisher_key = 'cb9d0fe8dabdea89019d845be3059973'
104
+ config.mode ='live'
105
+ end
106
+
107
+
108
+ 在Gemfile添加
109
+ gem "yinxiangma", :require => "yinxiangma/rails"
110
+
111
+ 建立
112
+
113
+ 在View中插入<%= yinxiangma_tags %>, 在View中需要进行验证处插入yinxiangma_valid() 即可
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new :test do |test|
6
+ test.libs << "lib"
7
+ test.pattern = "test/*_test.rb"
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,34 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "yinxiangma/configuration"
3
+ require "yinxiangma/client_helper"
4
+ require "yinxiangma/valid"
5
+
6
+ module Yinxiangma
7
+
8
+
9
+ YINXIANGMA_API_SERVER_URL = 'http://www.yinxiangma.com/api/'
10
+ YINXIANGMA_REGISTER_URL = 'http://www.yinxiangma.com/server/register.php'
11
+ YINXIANGMA_TOKEN_URL = 'yzm.token.php'
12
+ YINXIANGMA_VALID_URL = 'yzm.valid.php'
13
+ YINXIANGMA_DISPLAY_MODE = 'live'
14
+ YINXIANGMA_PUBLISHER_KEY = 'cb9d0fe8dabdea89019d845be3059973'
15
+
16
+ SKIP_VALID_ENV = 'test'
17
+
18
+
19
+ def self.configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ config = configuration
25
+ yield(config)
26
+ end
27
+
28
+ class YinxiangmaError < StandardError
29
+ end
30
+ end
31
+
32
+ if defined?(Rails)
33
+ require "yinxiangma/rails"
34
+ end
@@ -0,0 +1,65 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Yinxiangma
3
+ module ClientHelper
4
+ def yinxiangma_tags(options = {})
5
+ html = ""
6
+ api_server_url = Yinxiangma.configuration.api_server_url
7
+ reg_url = Yinxiangma.configuration.reg_url
8
+ key = options[:publisher_key] ||= Yinxiangma.configuration.publisher_key
9
+ raise YinxiangmaError, "No publisher key specified." unless key
10
+ error = options[:error] ||= (defined? flash ? flash[:yinxiangma_error] : "")
11
+ token = request_token(key)
12
+ if(token[0] != 'false')
13
+ html << <<-EOS
14
+ <script type='text/javascript'>
15
+ var YinXiangMaDataString ='#{token[0]}';
16
+ </script>
17
+ <script type='text/javascript'
18
+ src='http://www.yinxiangma.com/widget/YinXiangMa.php' charset='utf-8'>
19
+ </script>
20
+ EOS
21
+ elsif(token[1] == 'null key')
22
+ html << <<-EOS
23
+ 为了使用<strong>印象码</strong>视频验证码广告系统, 你必须注册并拥有一个 KEY. <br /><a href='#{reg_url}'>点击注册</a>
24
+ EOS
25
+ elsif(token[1] == 'key_invalid')
26
+ html << <<-EOS
27
+ 您的<strong>印象码</strong>插件配置有误,请检查您的配置文件!
28
+ EOS
29
+ end
30
+
31
+ return (html.respond_to?(:html_safe) && html.html_safe) || html
32
+ end
33
+
34
+ def request_token(publisher_key)
35
+ token_uri = Yinxiangma.configuration.api_server_url + Yinxiangma.configuration.token_url
36
+
37
+ #TODO Resolve Rails depedency for request.env
38
+
39
+ # userAgent = request.env["HTTP_USER_AGENT"] ||= "unknown"
40
+ # ip = request.env["HTTP_REMOTE_ADDR"] ||= "127.0.0.1"
41
+ # page = request.env["HTTP_REQUEST_URI"] ||= "Ruby Hosts"
42
+
43
+ userAgent = "unknown"
44
+ ip = "127.0.0.1"
45
+ page = "Ruby Hosts"
46
+
47
+ cookie = "YinXiangMa_Cookies"
48
+ version = "YinXiangMa_Ruby_v0.1"
49
+ mode = "live"
50
+
51
+
52
+ yxm = Net::HTTP.post_form(URI.parse(token_uri), {
53
+ "s" => publisher_key,
54
+ "u" => userAgent,
55
+ "i" => ip,
56
+ "p" => page,
57
+ "o" => cookie,
58
+ "v" => version,
59
+ "m" => mode
60
+ }).body.split('+')
61
+
62
+ return yxm
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Yinxiangma
3
+ class Configuration
4
+ attr_accessor :api_server_url,
5
+ :token_url,
6
+ :valid_url,
7
+ :reg_url,
8
+ :skip_valid_env,
9
+ :publisher_key,
10
+ :mode
11
+
12
+ def initialize
13
+ @api_server_url = YINXIANGMA_API_SERVER_URL
14
+ @token_url = YINXIANGMA_TOKEN_URL
15
+ @valid_url = YINXIANGMA_VALID_URL
16
+ @skip_valid_env = SKIP_VALID_ENV
17
+ @reg_url = YINXIANGMA_REGISTER_URL
18
+
19
+ @publisher_key = ENV['YINXIANGMA_PUBLISHER_KEY']
20
+ @mode = ENV['YINXIANGMA_DISPLAY_MODE'] ||= YINXIANGMA_DISPLAY_MODE
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'net/http'
3
+ require 'yinxiangma'
4
+
5
+ ActionView::Base.send(:include, Yinxiangma::ClientHelper)
6
+ ActionController::Base.send(:include, Yinxiangma::Valid)
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Yinxiangma
3
+ module Valid
4
+ def yinxiangma_valid
5
+ valid_uri = "http://www.yinxiangma.com/api/yzm.valid.php"
6
+ http = Net::HTTP
7
+ answer, error = http.post_form(URI.parse(valid_uri), {
8
+ "s" => "cb9d0fe8dabdea89019d845be3059973",
9
+ "t" => params[:YinXiangMa_challenge],
10
+ "i" => params[:YinXiangMa_response]
11
+ }).body.split('+')
12
+
13
+ if(answer == 'true')
14
+ return true
15
+ elsif(answer == 'false')
16
+ return false
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Yinxiangma
3
+ VERSION = '0.0.3'
4
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test/unit'
3
+ require 'net/http'
4
+
5
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/yinxiangma'
6
+
7
+ class YinxiangmaClientHelperTest < Test::Unit::TestCase
8
+ include Yinxiangma
9
+ include Yinxiangma::ClientHelper
10
+
11
+ def setup
12
+ Yinxiangma.configure do |config|
13
+ config.publisher_key = 'cb9d0fe8dabdea89019d845be3059973'
14
+ end
15
+ end
16
+
17
+ # def test_yinxiangma_request_token_with_null_key
18
+ # assert_match /null key/, request_token('')[1]
19
+ # end
20
+ #
21
+ # def test_yinxiangma_request_token_with_invalid_key
22
+ # assert_match /key_invalid/, request_token('123456')[1]
23
+ # end
24
+ #
25
+ # def test_yinxiangma_request_token_with_valid_key
26
+ # assert_match /^\{\"token\":\"[0-9a-f]{32}\"/, request_token('cb9d0fe8dabdea89019d845be3059973')[0]
27
+ # end
28
+
29
+ def test_yinxiangma_tags_with_null_key
30
+ assert_raise YinxiangmaError do
31
+ Yinxiangma.configuration.publisher_key = nil
32
+ assert_match /你必须注册并拥有一个 KEY/, yinxiangma_tags()
33
+ end
34
+ end
35
+
36
+ def test_yinxiangma_tags_with_invalid_key
37
+ Yinxiangma.configuration.publisher_key = '123456'
38
+ assert_match /请检查您的配置文件/, yinxiangma_tags()
39
+ end
40
+
41
+ def test_yinxiangma_tags_with_default_key
42
+ assert_match /var YinXiangMaDataString ='{\"token\":\"[0-9a-f]{32}\"/, yinxiangma_tags()
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/yinxiangma/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["SouthWolf"]
6
+ gem.email = ["wp.southwolf@gmail.com"]
7
+ gem.summary = %q{Helpers for the YinXiangMa CAPTCHA API}
8
+ gem.description = %q{This plugin adds helpers for the YinXiangMa CAPTCHA API}
9
+ gem.homepage = "http://www.yinxiangma.com"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "yinxiangma"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Yinxiangma::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yinxiangma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SouthWolf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70132404212460 !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: *70132404212460
25
+ description: This plugin adds helpers for the YinXiangMa CAPTCHA API
26
+ email:
27
+ - wp.southwolf@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - CHANGELOG
34
+ - Gemfile
35
+ - README.rdoc
36
+ - Rakefile
37
+ - lib/yinxiangma.rb
38
+ - lib/yinxiangma/client_helper.rb
39
+ - lib/yinxiangma/configuration.rb
40
+ - lib/yinxiangma/rails.rb
41
+ - lib/yinxiangma/valid.rb
42
+ - lib/yinxiangma/version.rb
43
+ - test/yinxiangma_test.rb
44
+ - yinxiangma.gemspec
45
+ homepage: http://www.yinxiangma.com
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ segments:
58
+ - 0
59
+ hash: -307653484978211841
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -307653484978211841
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.11
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Helpers for the YinXiangMa CAPTCHA API
75
+ test_files:
76
+ - test/yinxiangma_test.rb