keypic 0.0.1

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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in keypic.gemspec
4
+ gemspec
5
+
6
+ gem "rspec", "~> 2.13.0"
7
+ gem "fuubar"
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Fodojo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Keypic
2
+
3
+ It just client fot Keypic API http://ws.keypic.com/?op=help
4
+ How does it work? More info here: http://keypic.com/?tid=homepage_how_does_it_work
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'keypic'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install keypic
19
+
20
+ ## Configuration
21
+
22
+ Keypic.configure do |config|
23
+ config.endpoint = "http://ws.keypic.com"
24
+ config.form_id = "secret_token_here"
25
+ end
26
+
27
+ ## Usage
28
+
29
+ @keypic = Keypic::Proxy.new(env)
30
+
31
+ @keypic.token
32
+
33
+ @keypic.valid?({email: "", message: "", name: ""})
34
+
35
+ You can provide a numeric value (%):
36
+
37
+ @keypic.spam?({email: "", message: "", name: ""}, 90)
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
47
+
48
+ Copyright (c) 2013 Fodojo, released under the MIT license
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'keypic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "keypic"
8
+ spec.version = Keypic::VERSION
9
+ spec.authors = ["Igor Galeta"]
10
+ spec.email = ["galeta.igor@gmail.com"]
11
+ spec.description = %q{Client for Keypic Web Service API}
12
+ spec.summary = %q{Keypic is a Web Service for avoid the use of CAPTCHAs}
13
+ spec.homepage = "http://keypic.com/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httpi"
22
+ spec.add_dependency "multi_json"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require "keypic/version"
3
+ require "keypic/config"
4
+ require "keypic/client"
5
+ require "keypic/proxy"
6
+
7
+ module Keypic
8
+
9
+ def self.configuration
10
+ @configuration ||= Keypic::Config.new
11
+ end
12
+
13
+ def self.configure
14
+ block_given? ? yield(configuration) : configuration
15
+ end
16
+ end
@@ -0,0 +1,63 @@
1
+ require "httpi"
2
+ require "multi_json"
3
+
4
+ module Keypic
5
+ class Client
6
+ attr_reader :last_response, :parsed_response
7
+
8
+ @@methods = {
9
+ :new_token => "RequestNewToken",
10
+ :validate => "RequestValidation",
11
+ :spam => "ReportSpam",
12
+ :check_form => "checkFormID"
13
+ }.freeze
14
+
15
+ @@types = {
16
+ :csv => 1,
17
+ :json => 2,
18
+ :php => 3,
19
+ :xml => 4
20
+ }.freeze
21
+
22
+ def initialize(method_name, options = {})
23
+ @method_name = method_name.to_sym
24
+
25
+ @options = {
26
+ "FormID" => Keypic.configuration.form_id,
27
+ "ResponseType" => format,
28
+ "RequestType" => method
29
+ }.merge(options)
30
+ end
31
+
32
+ def method
33
+ @method ||= @@methods[@method_name]
34
+ end
35
+
36
+ def format
37
+ @format ||= @@types[Keypic.configuration.format.to_sym]
38
+ end
39
+
40
+ def invoke(options = {})
41
+ request.body = @options.merge(options)
42
+ parse_response(HTTPI.post(request))
43
+ end
44
+
45
+ def request
46
+ @request ||= ::HTTPI::Request.new(:url => Keypic.configuration.endpoint)
47
+ end
48
+
49
+ protected
50
+
51
+ def parse_response(response)
52
+ @last_response = response
53
+
54
+ if response.error?
55
+ @parsed_response = nil
56
+ else
57
+ @parsed_response = ::MultiJson.load(response.body)
58
+ end
59
+
60
+ @parsed_response
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,12 @@
1
+ module Keypic
2
+ class Config
3
+ attr_accessor :endpoint, :form_id, :format, :quantity
4
+
5
+ def initialize
6
+ @endpoint = "http://ws.keypic.com"
7
+ @form_id = "secret_token_here"
8
+ @format = :json
9
+ @quantity = 1
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,91 @@
1
+ module Keypic
2
+ class Proxy
3
+ attr_reader :env
4
+
5
+ def initialize(env)
6
+ @env = env
7
+ end
8
+
9
+ def token
10
+ @token ||= (fetch_token_from_session || fetch_token_form_server)
11
+ end
12
+
13
+ def valid?(options = {}, rank = 80)
14
+ fetch_rank(options) >= rank.to_f
15
+ end
16
+
17
+ def spam?(options = {}, rank = 80)
18
+ !valid?(options, rank)
19
+ end
20
+
21
+ def fetch_rank(options = {})
22
+ params = normalize_message(options)
23
+ params = { 'Token' => token }.merge(client_params).merge(params)
24
+
25
+ body = Keypic::Client.new(:validate).invoke(params)
26
+
27
+ if body && body["status"] == 'response'
28
+ body["spam"].to_f
29
+ else
30
+ 0.0
31
+ end
32
+ end
33
+
34
+ def client_params
35
+ {
36
+ 'ServerName' => env['SERVER_NAME'],
37
+ 'ClientIP' => env['REMOTE_ADDR'],
38
+ 'ClientUserAgent' => env['HTTP_USER_AGENT'],
39
+ 'ClientAccept' => env['HTTP_ACCEPT'],
40
+ 'ClientAcceptEncoding' => env['HTTP_ACCEPT_ENCODING'],
41
+ 'ClientAcceptLanguage' => env['HTTP_ACCEPT_LANGUAGE'],
42
+ 'ClientAcceptCharset' => env['HTTP_ACCEPT_CHARSET'],
43
+ 'ClientHttpReferer' => env['HTTP_REFERER']
44
+ }
45
+ end
46
+
47
+ def check!
48
+ body = Keypic::Client.new(:check_form).invoke
49
+
50
+ if body && body["status"] == 'response'
51
+ body["report"]
52
+ end
53
+ end
54
+
55
+ def clear!
56
+ session.delete("keypic.token")
57
+ @token = nil
58
+ end
59
+
60
+ def session
61
+ env["rack.session"] || {}
62
+ end
63
+
64
+ protected
65
+
66
+ def fetch_token_from_session
67
+ session["keypic.token"]
68
+ end
69
+
70
+ def fetch_token_form_server
71
+ params = { 'Quantity' => Keypic.configuration.quantity }.merge(client_params)
72
+
73
+ body = Keypic::Client.new(:new_token).invoke(params)
74
+
75
+ if body && body["status"] == 'new_token'
76
+ body["Token"]
77
+ end
78
+ end
79
+
80
+ def normalize_message(options)
81
+ params = options.symbolize_keys
82
+
83
+ {
84
+ "ClientEmailAddress" => params[:email],
85
+ "ClientUsername" => params[:name],
86
+ "ClientMessage" => params[:message]
87
+ }
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module Keypic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Keypic do
5
+ it "should be a module" do
6
+ Keypic.should be_a(Module)
7
+ end
8
+
9
+ context "configuration" do
10
+ it "should read default config values" do
11
+ Keypic.configuration.endpoint.should == "http://ws.keypic.com"
12
+ Keypic.configuration.form_id.should == "secret_token_here"
13
+ end
14
+
15
+ it "should set config value" do
16
+ Keypic.configure do |c|
17
+ c.endpoint = "endpoint"
18
+ c.form_id = "form_id"
19
+ end
20
+
21
+ Keypic.configuration.endpoint.should == "endpoint"
22
+ Keypic.configuration.form_id.should == "form_id"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require "rspec"
3
+ require "keypic"
4
+
5
+ # Load support files
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
7
+
8
+ RSpec.configure do |config|
9
+ # Remove this line if you don't want RSpec's should and should_not
10
+ # methods or matchers
11
+ require 'rspec/expectations'
12
+ config.include RSpec::Matchers
13
+
14
+ # == Mock Framework
15
+ config.mock_with :rspec
16
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keypic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Galeta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httpi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
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: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
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: Client for Keypic Web Service API
79
+ email:
80
+ - galeta.igor@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - keypic.gemspec
92
+ - lib/keypic.rb
93
+ - lib/keypic/client.rb
94
+ - lib/keypic/config.rb
95
+ - lib/keypic/proxy.rb
96
+ - lib/keypic/version.rb
97
+ - spec/models/keypic_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: http://keypic.com/
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.25
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Keypic is a Web Service for avoid the use of CAPTCHAs
124
+ test_files:
125
+ - spec/models/keypic_spec.rb
126
+ - spec/spec_helper.rb