has_face 0.0.3 → 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.
data/README.md CHANGED
@@ -31,6 +31,7 @@ HasFace.configure do |config|
31
31
  config.api_key = 'your face.com API key'
32
32
  config.api_secret = 'your face.com API secret'
33
33
  config.skip_validation_on_error = false
34
+ config.transfer_method = :upload
34
35
  end
35
36
  ```
36
37
 
@@ -92,6 +93,26 @@ end
92
93
  If an error does occur then it will be logged as a warning in the log
93
94
  for your applications current environment.
94
95
 
96
+ ## Tranfer Methods
97
+
98
+ The default behavior is to upload images to the face.com API. This
99
+ allows images that are not publicly accessible to be validated as well,
100
+ has_face also has the option to send the URL of the image to be
101
+ validated instead of uploading the data, to use url transering simply
102
+ set the configuration option:
103
+
104
+ ``` ruby
105
+ HasFace.configure do |config|
106
+ config.transfer_method = :url
107
+ end
108
+ ```
109
+
110
+ You will also need to set the `ActionController::Base.asset_host` to the
111
+ base URL your images are accessible at. This can be done in your
112
+ `environment.rb` or the environment for your specific environment, eg
113
+ `production.rb` or `staging.rb`.
114
+
115
+
95
116
  ## Testing has_face
96
117
 
97
118
  To speed up your test suite, you can disable face validations by setting the
@@ -111,7 +132,6 @@ RSpec.configure do |config|
111
132
  end
112
133
  ```
113
134
 
114
-
115
135
  Once included he matcher can be used:
116
136
 
117
137
  ``` ruby
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rake'
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:test) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.rspec_opts = ['--backtrace']
9
+ end
10
+
11
+ task :default => :test
@@ -2,4 +2,5 @@ HasFace.configure do |config|
2
2
  config.api_key = 'your face.com API key'
3
3
  config.api_secret = 'your face.com API secret'
4
4
  config.skip_validation_on_error = false
5
+ config.transfer_method = :upload
5
6
  end
data/lib/has_face.rb CHANGED
@@ -15,7 +15,7 @@ module HasFace
15
15
 
16
16
  class << self
17
17
 
18
- configs = [ :api_key, :api_secret, :enable_validation, :detect_url, :skip_validation_on_error ]
18
+ configs = [ :api_key, :api_secret, :enable_validation, :detect_url, :skip_validation_on_error, :transfer_method ]
19
19
 
20
20
  configs.each do |config|
21
21
  delegate config, "#{config}=", :to => HasFace::Configuration
@@ -3,6 +3,7 @@ module HasFace
3
3
 
4
4
  @enable_validation = true
5
5
  @skip_validation_on_error = false
6
+ @transfer_method = :upload
6
7
  @detect_url = "http://api.face.com/faces/detect.json"
7
8
 
8
9
  class << self
@@ -11,6 +12,7 @@ module HasFace
11
12
  attr_accessor :api_key
12
13
  attr_accessor :api_secret
13
14
  attr_accessor :skip_validation_on_error
15
+ attr_accessor :transfer_method
14
16
  end
15
17
 
16
18
  end
@@ -1,4 +1,5 @@
1
1
  require 'rest_client'
2
+ require 'action_controller'
2
3
 
3
4
  module HasFace
4
5
  class Validator < ActiveModel::EachValidator
@@ -15,6 +16,7 @@ module HasFace
15
16
 
16
17
  image = record.send(attr_name)
17
18
  @image_path = image.respond_to?(:path) ? image.path : nil
19
+ @image_url = "#{ActionController::Base.asset_host}#{image.url if image.respond_to?(:url)}"
18
20
 
19
21
  # Skip validation if our image is nil/blank and allow nil/blank is on
20
22
  return if (@allow_nil && image.nil?) || (@allow_blank && image.blank?)
@@ -40,7 +42,11 @@ module HasFace
40
42
  protected
41
43
 
42
44
  def params
43
- { :api_key => HasFace.api_key, :api_secret => HasFace.api_secret, :image => File.new(@image_path, 'r') }
45
+ if HasFace.transfer_method == :url
46
+ { :api_key => HasFace.api_key, :api_secret => HasFace.api_secret, :urls => @image_url }
47
+ else
48
+ { :api_key => HasFace.api_key, :api_secret => HasFace.api_secret, :image => File.new(@image_path, 'r') }
49
+ end
44
50
  end
45
51
 
46
52
  def handle_api_error(response)
@@ -1,3 +1,3 @@
1
1
  module HasFace
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -144,6 +144,55 @@ describe HasFace::Validator do
144
144
 
145
145
  end
146
146
 
147
+ context 'using the url transfer method' do
148
+
149
+ let(:hit_url) { 'u/17429266/swoonme/test/hit.jpeg' }
150
+ let(:miss_url) { 'u/17429266/swoonme/test/miss.jpeg' }
151
+
152
+ before :each do
153
+ ActionController::Base.asset_host = 'http://dl.dropbox.com/'
154
+ stub(HasFace).transfer_method { :url }
155
+ end
156
+
157
+ context 'when the image is a valid face' do
158
+
159
+ before :each do
160
+ avatar.path = hit_url # Needs to have 'some' value
161
+ avatar.url = hit_url
162
+ end
163
+
164
+ it 'should be valid' do
165
+ VCR.use_cassette('valid url transfer') do
166
+ user.should be_valid
167
+ end
168
+ end
169
+
170
+ end
171
+
172
+ context 'when the image is not a valid face' do
173
+
174
+ before :each do
175
+ avatar.path = miss_url # Needs to have 'some' value
176
+ avatar.url = miss_url
177
+ end
178
+
179
+ it 'should not be valid' do
180
+ VCR.use_cassette('invalid url transfer') do
181
+ user.should_not be_valid
182
+ end
183
+ end
184
+
185
+ it 'should have an error on the image field' do
186
+ VCR.use_cassette('invalid url transfer') do
187
+ user.valid?
188
+ user.errors[:avatar].should == [ "We couldn't see a face in your photo, try taking another one." ]
189
+ end
190
+ end
191
+
192
+ end
193
+
194
+ end
195
+
147
196
  context 'allowing blank' do
148
197
 
149
198
  context 'when allow blank is true' do
@@ -0,0 +1,41 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :post
5
+ uri: http://api.face.com:80/faces/detect.json
6
+ body: !!null
7
+ headers:
8
+ accept:
9
+ - ! '*/*; q=0.5, application/xml'
10
+ accept-encoding:
11
+ - gzip, deflate
12
+ content-length:
13
+ - '161'
14
+ content-type:
15
+ - application/x-www-form-urlencoded
16
+ response: !ruby/struct:VCR::Response
17
+ status: !ruby/struct:VCR::ResponseStatus
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ date:
22
+ - Sat, 30 Jul 2011 03:45:21 GMT
23
+ server:
24
+ - Apache/2.2.16 (Debian) mod_ssl/2.2.16 OpenSSL/0.9.8o
25
+ content-encoding:
26
+ - gzip
27
+ vary:
28
+ - Accept-Encoding
29
+ content-length:
30
+ - '282'
31
+ content-type:
32
+ - ! 'application/json; Charset: utf-8'
33
+ body: !binary |-
34
+ H4sIAAAAAAAAA0yPy2rDMBBFf8XMtiZ6WYqlVVdddNtlFYIqj20VOzLRmARC
35
+ /r0qdNHlvcM53HnANmfKBdznA/brAg5mos155tmwHIZr3r7y/RDz6tnumTh2
36
+ 0kpjPCu3nC8rekZYyLM1lXL43nCCFrY0VM3bq8ZBHcc+RGWF7vRoouaRh64b
37
+ e6sUmrMUxkTkOqId+nHkUSsbAw8DjrqvVHXd0kAzOCF5CzOmaaa/QGH6HX16
38
+ nlooFGivCcoeI5ZSub2ECcHVnwrWNaqFK64hXdJlAtdZe2xhSWuqNs05/70W
39
+ pDOlFc+E91rDR6C2Ubx535dGciEarpw2jsvmpRIc/jN1kxLC2l4Y+Xz+AAAA
40
+ //8DAFXBupBUAQAA
41
+ http_version: '1.1'
@@ -0,0 +1,48 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :post
5
+ uri: http://api.face.com:80/faces/detect.json
6
+ body: !!null
7
+ headers:
8
+ accept:
9
+ - ! '*/*; q=0.5, application/xml'
10
+ accept-encoding:
11
+ - gzip, deflate
12
+ content-length:
13
+ - '160'
14
+ content-type:
15
+ - application/x-www-form-urlencoded
16
+ response: !ruby/struct:VCR::Response
17
+ status: !ruby/struct:VCR::ResponseStatus
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ date:
22
+ - Sat, 30 Jul 2011 03:45:18 GMT
23
+ server:
24
+ - Apache/2.2.16 (Debian) mod_ssl/2.2.16 OpenSSL/0.9.8o
25
+ content-encoding:
26
+ - gzip
27
+ vary:
28
+ - Accept-Encoding
29
+ content-length:
30
+ - '588'
31
+ content-type:
32
+ - ! 'application/json; Charset: utf-8'
33
+ body: !binary |-
34
+ H4sIAAAAAAAAA6xSy47cIBD8lRHXTDAG49cpl+QQKVKk5BavLIyxTYTtkYHs
35
+ blbz72n8mM3OJZfIBwNdVFc19YIuw+xmi8ofL8gvBpVocO5SVlEVtQa3y3xp
36
+ 5ics57GKfBXFWUILmqZVZB/neRpVFTllXRUN2uGfF9WjM7roFlg+fUgp7RhV
37
+ WcNyRkWc5l3adILIpmgaIWRc0zhNpSJcqqLNu45IzgopiGhVx3PJCuB61K0b
38
+ UBlTckaD0v3g9o0T/abZrd2+f/zytf4PLWtOMCE1zzAnNXwgYVFy7if9WzRG
39
+ odItXkH3YVF2mA20nrwxZ+R1G+Q8nFGvb4dGNCoMFEjkPHV6GRXUOmEsUIxi
40
+ 8sLctuCnV0v9enl3nuaYsVfv+1aqyakFlS/oCZUcxvEMP9B8PSP1rGqjOrcX
41
+ Gce8WOtJjHm2A5aNbUWkCaZsQxDMY0CMs3fDG5IcZ3SFZAzn9AZ5IyPJcZ4e
42
+ IJbdQH/34jlO4wOTpICZZqvufHCcJEGnWHYN20DCfufaDuSgp2P9LB5BJiZg
43
+ dZkNjPU9xTENWXQy5IdhCtqEc4tuPCQ2tOyEXFv/EsbDAoWnPZ6qVVMoFsFG
44
+ r6Z2c3kgR2HukSwMrjfC2o38gK7Pe88a7NlRGz31/8TS6/X6cIVgWSecB25k
45
+ vZTKWgB6K/rVgrchWTSEdRR6WnmTosghg3rULsyWkFC1ytVOj6p26gmO0Tfh
46
+ zidGTp+9OVESxyfCSp6WhJ7ewY0t/sedMMY4BtY4BVF/AAAA//8DAJNIsFo5
47
+ BAAA
48
+ http_version: '1.1'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_face
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-25 00:00:00.000000000 +08:00
13
- default_executable:
12
+ date: 2011-07-30 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rails
17
- requirement: &70365406834240 !ruby/object:Gem::Requirement
16
+ requirement: &70280597718040 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '3.0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *70365406834240
24
+ version_requirements: *70280597718040
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rest-client
28
- requirement: &70365406833820 !ruby/object:Gem::Requirement
27
+ requirement: &70280597717560 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *70365406833820
35
+ version_requirements: *70280597717560
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: rspec
39
- requirement: &70365406833280 !ruby/object:Gem::Requirement
38
+ requirement: &70280597716920 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '2.0'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *70365406833280
46
+ version_requirements: *70280597716920
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: sqlite3
50
- requirement: &70365406832780 !ruby/object:Gem::Requirement
49
+ requirement: &70280597716320 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ! '>='
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: '1.0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *70365406832780
57
+ version_requirements: *70280597716320
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: rr
61
- requirement: &70365406832320 !ruby/object:Gem::Requirement
60
+ requirement: &70280597715800 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
63
  - - ! '>='
@@ -66,10 +65,10 @@ dependencies:
66
65
  version: '1.0'
67
66
  type: :development
68
67
  prerelease: false
69
- version_requirements: *70365406832320
68
+ version_requirements: *70280597715800
70
69
  - !ruby/object:Gem::Dependency
71
70
  name: vcr
72
- requirement: &70365406831860 !ruby/object:Gem::Requirement
71
+ requirement: &70280597715300 !ruby/object:Gem::Requirement
73
72
  none: false
74
73
  requirements:
75
74
  - - ! '>='
@@ -77,10 +76,10 @@ dependencies:
77
76
  version: '1.0'
78
77
  type: :development
79
78
  prerelease: false
80
- version_requirements: *70365406831860
79
+ version_requirements: *70280597715300
81
80
  - !ruby/object:Gem::Dependency
82
81
  name: fakeweb
83
- requirement: &70365406831400 !ruby/object:Gem::Requirement
82
+ requirement: &70280597714740 !ruby/object:Gem::Requirement
84
83
  none: false
85
84
  requirements:
86
85
  - - ! '>='
@@ -88,7 +87,7 @@ dependencies:
88
87
  version: '1.0'
89
88
  type: :development
90
89
  prerelease: false
91
- version_requirements: *70365406831400
90
+ version_requirements: *70280597714740
92
91
  description: An Active Model validator that uses the face.com API to ensures an image
93
92
  contains a face
94
93
  email:
@@ -126,8 +125,9 @@ files:
126
125
  - spec/support/vcr_cassettes/invalid_api_key.yml
127
126
  - spec/support/vcr_cassettes/invalid_detect_url.yml
128
127
  - spec/support/vcr_cassettes/invalid_image.yml
128
+ - spec/support/vcr_cassettes/invalid_url_transfer.yml
129
129
  - spec/support/vcr_cassettes/valid_image.yml
130
- has_rdoc: true
130
+ - spec/support/vcr_cassettes/valid_url_transfer.yml
131
131
  homepage: https://github.com/mariovisic/has_face
132
132
  licenses: []
133
133
  post_install_message:
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project: has_face
151
- rubygems_version: 1.6.2
151
+ rubygems_version: 1.8.5
152
152
  signing_key:
153
153
  specification_version: 3
154
154
  summary: Easily validate if an image contains faces
@@ -167,4 +167,6 @@ test_files:
167
167
  - spec/support/vcr_cassettes/invalid_api_key.yml
168
168
  - spec/support/vcr_cassettes/invalid_detect_url.yml
169
169
  - spec/support/vcr_cassettes/invalid_image.yml
170
+ - spec/support/vcr_cassettes/invalid_url_transfer.yml
170
171
  - spec/support/vcr_cassettes/valid_image.yml
172
+ - spec/support/vcr_cassettes/valid_url_transfer.yml