antigate_rb 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3952502c45b4cc8a15522423013ebc84958525ec
4
+ data.tar.gz: f6dc2ed15421b756a7300772d0bddb85f49f6acc
5
+ SHA512:
6
+ metadata.gz: caa7c349e716e54e13cfc9a8986ec79cfc5fecb03c0d5720134967016de6f23c108581375835c3dd0c7bd1b64866f36a72319d3522d90c6c0c1e95d310a66305
7
+ data.tar.gz: 6171491feb4e0a2ec4c8177ab4a108d3a6c1f4c3c2575565591882dbdc8fa40b854377b9600d7ab76b21870fd3f292541aceb7df53d9d468b813f9e8c4afa118
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in antigate_rb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nikita Pomiashchii
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.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # AntigateRb
2
+
3
+ Antigate.com ruby api wrapper
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'antigate_rb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install antigate_rb
18
+
19
+ ## Usage example
20
+
21
+ Configure with api params
22
+
23
+ ```ruby
24
+ AntigateRb.configure do |config|
25
+ config.key = 'api_key'
26
+ config.min_len = 6
27
+ config.max_len = 10
28
+ end
29
+ ```
30
+
31
+ ```ruby
32
+ @client = AntigateRb::Client.new(retries_count: 5, phrase: 1)
33
+ code = @client.code(image_content)
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'antigate_rb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "antigate_rb"
8
+ spec.version = AntigateRb::VERSION
9
+ spec.authors = ["Nikita Pomiashchii"]
10
+ spec.email = ["pomnikita@gmail.com"]
11
+ spec.summary = "Antigate api ruby wrapper"
12
+ spec.description = "Antigate api ruby wrapper"
13
+ spec.homepage = ""
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_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_development_dependency "webmock"
24
+
25
+ spec.add_dependency "httpi"
26
+ spec.add_dependency "activesupport"
27
+ end
@@ -0,0 +1,16 @@
1
+ require 'antigate_rb/version'
2
+ require 'antigate_rb/configuration'
3
+ require 'antigate_rb/client'
4
+ require 'antigate_rb/error'
5
+
6
+ module AntigateRb
7
+
8
+ def self.configure(&block)
9
+ yield(configuration)
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ end
@@ -0,0 +1,113 @@
1
+ require 'httpi'
2
+ require 'active_support/core_ext/string'
3
+ require 'active_support/core_ext/object'
4
+
5
+ module AntigateRb
6
+ class Client
7
+
8
+ def initialize(options = {})
9
+ @retries_count = options.delete(:retries_count) || 10
10
+ @sleep = options.delete(:sleep) || 5
11
+ @options = AntigateRb.configuration.options.merge(options)
12
+ end
13
+
14
+ def decode(image)
15
+ case request_image(image)
16
+ when /OK\|(.+)/
17
+ @captcha_id = $1
18
+ check
19
+ when /^ERROR_(.+)/
20
+ raise error_class($1)
21
+ else
22
+ raise UnknownResponse
23
+ end
24
+ end
25
+
26
+ def check
27
+ return if @captcha_id.blank?
28
+ begin
29
+ get_status
30
+ rescue CaptchaNotReady => e
31
+ attempt ||= @retries_count
32
+ raise e if (attempt -= 1) < 0
33
+ sleep @sleep
34
+ retry
35
+ end
36
+ end
37
+
38
+ def report_bad
39
+ return if @captcha_id.blank?
40
+ request_report_bad
41
+ end
42
+
43
+ def get_balance
44
+ request_balance
45
+ end
46
+
47
+ def get_stats(date = Date.today)
48
+ request_stats date
49
+ end
50
+
51
+ private
52
+
53
+ def get_status
54
+ case request_status
55
+ when /^OK\|(.+)/
56
+ $1
57
+ when 'CAPCHA_NOT_READY'
58
+ raise CaptchaNotReady
59
+ when /^ERROR_(.+)/
60
+ raise error_class($1)
61
+ else
62
+ raise UnknownResponse
63
+ end
64
+ end
65
+
66
+ def request_status
67
+ request 'http://antigate.com/res.php',
68
+ key: AntigateRb.configuration.key,
69
+ action: 'get',
70
+ id: @captcha_id
71
+ end
72
+
73
+ def request_image(image)
74
+ request 'http://antigate.com/in.php',
75
+ @options.merge(method: 'base64', body: Base64.encode64(image))
76
+ end
77
+
78
+ def request_report_bad
79
+ request 'http://antigate.com/res.php',
80
+ key: AntigateRb.configuration.key,
81
+ action: 'reportbad',
82
+ id: @captcha_id
83
+ end
84
+
85
+ def request_balance
86
+ request 'http://antigate.com/res.php',
87
+ key: AntigateRb.configuration.key,
88
+ action: 'getbalance'
89
+ end
90
+
91
+ def request_stats(date)
92
+ request 'http://antigate.com/res.php',
93
+ key: AntigateRb.configuration.key,
94
+ action: 'getstats',
95
+ date: date.strftime('%Y-%m-%d')
96
+ end
97
+
98
+ def request(url, params)
99
+ request = HTTPI::Request.new.tap do |request|
100
+ request.url = url
101
+ request.body = params
102
+ end
103
+ HTTPI.post(request).body
104
+ end
105
+
106
+ def error_class(string)
107
+ "AntigateRb::#{string.downcase.classify}".constantize
108
+ rescue NameError
109
+ UnknownErrorResponse
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,32 @@
1
+ module AntigateRb
2
+ class Configuration
3
+
4
+ @@config_keys = []
5
+
6
+ def self.config_key(key, default_value = nil)
7
+ attr_accessor key
8
+ @@config_keys << key
9
+ if default_value
10
+ define_method key do
11
+ instance_variable_get(:"@#{key}") || default_value
12
+ end
13
+ end
14
+ end
15
+
16
+ config_key :key
17
+ config_key :phrase, 0
18
+ config_key :regsense, 0
19
+ config_key :numeric, 0
20
+ config_key :calc, 0
21
+ config_key :min_len, 0
22
+ config_key :max_len, 0
23
+ config_key :is_russian, 0
24
+
25
+ def options
26
+ @@config_keys.each_with_object({}) do |key, hash|
27
+ hash[key] = __send__(key) if __send__(key)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ module AntigateRb
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ class UnknownResponse < Error
7
+ end
8
+
9
+ class UnknownErrorResponse < Error
10
+ end
11
+
12
+ class WrongUserKey < Error
13
+ end
14
+
15
+ class KeyDoesNotExist < Error
16
+ end
17
+
18
+ class ZeroBalance < Error
19
+ end
20
+
21
+ class NoSlotAvailable < Error
22
+ end
23
+
24
+ class ZeroCaptchaFilesize < Error
25
+ end
26
+
27
+ class TooBigCaptchaFilesize < Error
28
+ end
29
+
30
+ class WrongFileExtension < Error
31
+ end
32
+
33
+ class ImageTypeNotSupported < Error
34
+ end
35
+
36
+ class IpNotAllowed < Error
37
+ end
38
+
39
+ class CaptchaNotReady < Error
40
+ end
41
+
42
+ class WrongIdFormat < Error
43
+ end
44
+
45
+ class CaptchaUnsolvable < Error
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ module AntigateRb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,130 @@
1
+ require 'minitest/autorun'
2
+ require 'webmock/minitest'
3
+ require 'antigate_rb'
4
+
5
+ HTTPI.log = false
6
+
7
+ class TestAntigateConfigure < MiniTest::Unit::TestCase
8
+ def setup
9
+ AntigateRb.configure do |config|
10
+ config.key = 'test_key'
11
+ config.phrase = 1
12
+ end
13
+ end
14
+
15
+ def test_configuration_default_options
16
+ assert_equal({ key: "test_key", phrase: 1, regsense: 0, numeric: 0, calc: 0,
17
+ min_len: 0, max_len: 0, is_russian: 0 },
18
+ AntigateRb.configuration.options)
19
+ end
20
+ end
21
+
22
+ class TestAntigate < MiniTest::Unit::TestCase
23
+ def setup
24
+ AntigateRb.configure do |config|
25
+ config.key = 'test_key'
26
+ end
27
+ @client = AntigateRb::Client.new(sleep: 0, retries_count: 1)
28
+ end
29
+
30
+ def test_success_request
31
+ stub_request(:post, "http://antigate.com/in.php").
32
+ to_return(status: 200, body: 'OK|request_id')
33
+
34
+ stub_request(:post, "http://antigate.com/res.php").
35
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
36
+ to_return(status: 200, body: 'CAPCHA_NOT_READY').then.
37
+ to_return(status: 200, body: 'OK|result')
38
+
39
+ result = @client.decode 'file_content'
40
+ assert_equal 'result', result
41
+ end
42
+
43
+ def test_with_retries
44
+ stub_request(:post, "http://antigate.com/in.php").
45
+ to_return(status: 200, body: 'OK|request_id')
46
+
47
+ stub_request(:post, "http://antigate.com/res.php").
48
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
49
+ to_return(status: 200, body: 'CAPCHA_NOT_READY').then.
50
+ to_return(status: 200, body: 'CAPCHA_NOT_READY')
51
+
52
+ assert_raises AntigateRb::CaptchaNotReady do
53
+ @client.decode 'file_content'
54
+ end
55
+ end
56
+
57
+ %w(ERROR_KEY_DOES_NOT_EXIST ERROR_WRONG_ID_FORMAT ERROR_CAPTCHA_UNSOLVABLE).each do |error_message|
58
+ define_method "test_status_#{error_message}" do
59
+ stub_request(:post, "http://antigate.com/in.php").
60
+ to_return(status: 200, body: 'OK|request_id')
61
+
62
+ stub_request(:post, "http://antigate.com/res.php").
63
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
64
+ to_return(status: 200, body: error_message)
65
+
66
+ assert_raises "AntigateRb::#{error_message.gsub(/^ERROR_/,'').downcase.classify}".constantize do
67
+ @client.decode 'file_content'
68
+ end
69
+ end
70
+ end
71
+
72
+ %w(ERROR_WRONG_USER_KEY ERROR_KEY_DOES_NOT_EXIST ERROR_ZERO_BALANCE
73
+ ERROR_NO_SLOT_AVAILABLE ERROR_ZERO_CAPTCHA_FILESIZE
74
+ ERROR_TOO_BIG_CAPTCHA_FILESIZE ERROR_WRONG_FILE_EXTENSION
75
+ ERROR_IMAGE_TYPE_NOT_SUPPORTED ERROR_IP_NOT_ALLOWED).each do |error_message|
76
+
77
+ define_method("test_request_#{error_message}") do
78
+ stub_request(:post, "http://antigate.com/in.php").
79
+ to_return(status: 200, body: error_message)
80
+
81
+ assert_raises "AntigateRb::#{error_message.gsub(/^ERROR_/,'').downcase.classify}".constantize do
82
+ @client.decode 'file_content'
83
+ end
84
+ end
85
+ end
86
+
87
+ def test_unknown_response
88
+ stub_request(:post, "http://antigate.com/in.php").
89
+ to_return(status: 200, body: 'unknown')
90
+
91
+ assert_raises AntigateRb::UnknownResponse do
92
+ @client.decode 'file_content'
93
+ end
94
+ end
95
+
96
+ def test_unknown_error_response
97
+ stub_request(:post, "http://antigate.com/in.php").
98
+ to_return(status: 200, body: 'ERROR_unknown')
99
+
100
+ assert_raises AntigateRb::UnknownErrorResponse do
101
+ @client.decode 'file_content'
102
+ end
103
+ end
104
+
105
+ def test_report_bad
106
+ @client.instance_variable_set :@captcha_id, 'test_id'
107
+ stub_request(:post, "http://antigate.com/res.php").
108
+ with(body: {"action"=>"reportbad", "id"=>"test_id", "key"=>"test_key"}).
109
+ to_return(status: 200, body: 'ok')
110
+
111
+ assert @client.report_bad
112
+ end
113
+
114
+ def test_get_balance
115
+ stub_request(:post, "http://antigate.com/res.php").
116
+ with(body: {"action"=>"getbalance", "key"=>"test_key"}).
117
+ to_return(status: 200, body: '10.05')
118
+
119
+ assert_equal '10.05', @client.get_balance
120
+ end
121
+
122
+ def test_get_stats
123
+ stub_request(:post, "http://antigate.com/res.php").
124
+ with(body: {"action" => "getstats", "key"=>"test_key", "date" => Date.today.strftime('%Y-%m-%d')}).
125
+ to_return(status: 200, body: '<?xml version="1.0"?><response></response>')
126
+
127
+ assert_equal '<?xml version="1.0"?><response></response>', @client.get_stats
128
+ end
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: antigate_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nikita Pomiashchii
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httpi
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Antigate api ruby wrapper
84
+ email:
85
+ - pomnikita@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - antigate_rb.gemspec
96
+ - lib/antigate_rb.rb
97
+ - lib/antigate_rb/client.rb
98
+ - lib/antigate_rb/configuration.rb
99
+ - lib/antigate_rb/error.rb
100
+ - lib/antigate_rb/version.rb
101
+ - test/antigate_rb_test.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.14
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Antigate api ruby wrapper
126
+ test_files:
127
+ - test/antigate_rb_test.rb