has_face 0.0.1 → 0.0.2
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/LICENSE +20 -0
- data/README.md +6 -1
- data/has_face.gemspec +1 -0
- data/lib/has_face/validator.rb +17 -19
- data/lib/has_face/version.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- metadata +81 -109
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (C) 2011 by Mario Visic
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/README.md
CHANGED
@@ -126,7 +126,12 @@ The options `allow_blank` and `allow_nil` can also be passed to the matcher:
|
|
126
126
|
it { should validate_has_face_for :avatar, :allow_blank => true }
|
127
127
|
```
|
128
128
|
|
129
|
-
|
129
|
+
## License
|
130
|
+
|
131
|
+
Has face is distributed under a standard MIT license, see LICENSE for
|
132
|
+
further information.
|
133
|
+
|
134
|
+
## Contributing
|
130
135
|
|
131
136
|
Fork on GitHub and after you’ve committed tested patches, send a pull request.
|
132
137
|
|
data/has_face.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_dependency 'rest-client'
|
18
18
|
|
19
19
|
s.add_development_dependency 'rspec', '>= 2.0'
|
20
|
+
s.add_development_dependency 'sqlite3', '>= 1.0'
|
20
21
|
s.add_development_dependency 'rr', '>= 1.0'
|
21
22
|
s.add_development_dependency 'vcr', '>= 1.0'
|
22
23
|
s.add_development_dependency 'fakeweb', '>= 1.0'
|
data/lib/has_face/validator.rb
CHANGED
@@ -13,40 +13,38 @@ module HasFace
|
|
13
13
|
# Skip validation if globally turned off
|
14
14
|
return if HasFace.enable_validation == false
|
15
15
|
|
16
|
-
image
|
17
|
-
image_path = image.
|
16
|
+
image = record.send(attr_name)
|
17
|
+
@image_path = image.respond_to?(:path) ? image.path : nil
|
18
18
|
|
19
19
|
# Skip validation if our image is nil/blank and allow nil/blank is on
|
20
20
|
return if (@allow_nil && image.nil?) || (@allow_blank && image.blank?)
|
21
21
|
|
22
|
-
# Add an error if the
|
23
|
-
return record.errors.add(attr_name, :no_face) if image_path.blank?
|
22
|
+
# Add an error if the image path is blank
|
23
|
+
return record.errors.add(attr_name, :no_face) if @image_path.blank?
|
24
24
|
|
25
|
-
# Get
|
26
|
-
|
27
|
-
|
28
|
-
begin
|
29
|
-
response = RestClient.post(HasFace.detect_url, params)
|
30
|
-
rescue RestClient::Exception => error
|
31
|
-
return handle_request_error(error)
|
32
|
-
end
|
33
|
-
|
34
|
-
json_response = JSON.parse(response.body)
|
25
|
+
# Get the response and parse to JSON
|
26
|
+
raw_response = RestClient.post(HasFace.detect_url, params)
|
27
|
+
response = JSON.parse(raw_response)
|
35
28
|
|
36
29
|
# Error handling for failed responses
|
37
|
-
return handle_api_error(
|
38
|
-
|
30
|
+
return handle_api_error(response) unless response['status'] == 'success'
|
39
31
|
|
40
32
|
# Add errors if no tags are present
|
41
|
-
tags =
|
33
|
+
tags = response.try(:[], 'photos').try(:first).try(:[], 'tags')
|
42
34
|
record.errors.add(attr_name, :no_face) if tags.blank?
|
43
35
|
|
36
|
+
rescue RestClient::Exception => error
|
37
|
+
return handle_request_error(error)
|
44
38
|
end
|
45
39
|
|
46
40
|
protected
|
47
41
|
|
42
|
+
def params
|
43
|
+
{ :api_key => HasFace.api_key, :api_secret => HasFace.api_secret, :image => File.new(@image_path, 'r') }
|
44
|
+
end
|
45
|
+
|
48
46
|
def handle_api_error(response)
|
49
|
-
error_message =
|
47
|
+
error_message = %{face.com API Error: "#{response['error_message']}" Code: #{response['error_code']}}
|
50
48
|
|
51
49
|
if HasFace.skip_validation_on_error
|
52
50
|
Rails.logger.warn error_message if Rails.logger.present?
|
@@ -57,7 +55,7 @@ module HasFace
|
|
57
55
|
end
|
58
56
|
|
59
57
|
def handle_request_error(error)
|
60
|
-
error_message =
|
58
|
+
error_message = %{has_face HTTP Request Error: "#{error.message}"}
|
61
59
|
|
62
60
|
if HasFace.skip_validation_on_error
|
63
61
|
Rails.logger.warn error_message if Rails.logger.present?
|
data/lib/has_face/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -5,9 +5,9 @@ require 'rails'
|
|
5
5
|
require 'active_model'
|
6
6
|
require 'active_model/validations'
|
7
7
|
require 'active_record'
|
8
|
-
require 'lib/has_face'
|
8
|
+
require './lib/has_face'
|
9
9
|
|
10
|
-
Dir["spec/support/**/*.rb"].each {|f| require f}
|
10
|
+
Dir["spec/support/**/*.rb"].each {|f| require "./#{f}"}
|
11
11
|
|
12
12
|
VALID_IMAGE_PATH = 'spec/support/assets/hit.jpg'
|
13
13
|
INVALID_IMAGE_PATH = 'spec/support/assets/miss.jpg'
|
metadata
CHANGED
@@ -1,125 +1,106 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_face
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Mario Visic
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-07-11 00:00:00 +08:00
|
12
|
+
date: 2011-07-24 00:00:00.000000000 +08:00
|
19
13
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
22
16
|
name: rails
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &70110405819840 !ruby/object:Gem::Requirement
|
25
18
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 3
|
32
|
-
- 0
|
33
|
-
version: "3.0"
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
34
23
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: rest-client
|
38
24
|
prerelease: false
|
39
|
-
|
25
|
+
version_requirements: *70110405819840
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rest-client
|
28
|
+
requirement: &70110405819420 !ruby/object:Gem::Requirement
|
40
29
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
|
45
|
-
segments:
|
46
|
-
- 0
|
47
|
-
version: "0"
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
48
34
|
type: :runtime
|
49
|
-
|
50
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70110405819420
|
37
|
+
- !ruby/object:Gem::Dependency
|
51
38
|
name: rspec
|
39
|
+
requirement: &70110405818880 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '2.0'
|
45
|
+
type: :development
|
52
46
|
prerelease: false
|
53
|
-
|
47
|
+
version_requirements: *70110405818880
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: sqlite3
|
50
|
+
requirement: &70110405818380 !ruby/object:Gem::Requirement
|
54
51
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
segments:
|
60
|
-
- 2
|
61
|
-
- 0
|
62
|
-
version: "2.0"
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.0'
|
63
56
|
type: :development
|
64
|
-
version_requirements: *id003
|
65
|
-
- !ruby/object:Gem::Dependency
|
66
|
-
name: rr
|
67
57
|
prerelease: false
|
68
|
-
|
58
|
+
version_requirements: *70110405818380
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rr
|
61
|
+
requirement: &70110405817920 !ruby/object:Gem::Requirement
|
69
62
|
none: false
|
70
|
-
requirements:
|
71
|
-
- -
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
|
74
|
-
segments:
|
75
|
-
- 1
|
76
|
-
- 0
|
77
|
-
version: "1.0"
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '1.0'
|
78
67
|
type: :development
|
79
|
-
version_requirements: *id004
|
80
|
-
- !ruby/object:Gem::Dependency
|
81
|
-
name: vcr
|
82
68
|
prerelease: false
|
83
|
-
|
69
|
+
version_requirements: *70110405817920
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: vcr
|
72
|
+
requirement: &70110405817460 !ruby/object:Gem::Requirement
|
84
73
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
|
89
|
-
segments:
|
90
|
-
- 1
|
91
|
-
- 0
|
92
|
-
version: "1.0"
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.0'
|
93
78
|
type: :development
|
94
|
-
version_requirements: *id005
|
95
|
-
- !ruby/object:Gem::Dependency
|
96
|
-
name: fakeweb
|
97
79
|
prerelease: false
|
98
|
-
|
80
|
+
version_requirements: *70110405817460
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: fakeweb
|
83
|
+
requirement: &70110405817000 !ruby/object:Gem::Requirement
|
99
84
|
none: false
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
104
|
-
segments:
|
105
|
-
- 1
|
106
|
-
- 0
|
107
|
-
version: "1.0"
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '1.0'
|
108
89
|
type: :development
|
109
|
-
|
110
|
-
|
111
|
-
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: *70110405817000
|
92
|
+
description: An Active Model validator that uses the face.com API to ensures an image
|
93
|
+
contains a face
|
94
|
+
email:
|
112
95
|
- mario@mariovisic.com
|
113
96
|
executables: []
|
114
|
-
|
115
97
|
extensions: []
|
116
|
-
|
117
98
|
extra_rdoc_files: []
|
118
|
-
|
119
|
-
files:
|
99
|
+
files:
|
120
100
|
- .gitignore
|
121
101
|
- .rvmrc
|
122
102
|
- Gemfile
|
103
|
+
- LICENSE
|
123
104
|
- README.md
|
124
105
|
- Rakefile
|
125
106
|
- has_face.gemspec
|
@@ -149,38 +130,29 @@ files:
|
|
149
130
|
has_rdoc: true
|
150
131
|
homepage: https://github.com/mariovisic/has_face
|
151
132
|
licenses: []
|
152
|
-
|
153
133
|
post_install_message:
|
154
134
|
rdoc_options: []
|
155
|
-
|
156
|
-
require_paths:
|
135
|
+
require_paths:
|
157
136
|
- lib
|
158
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
138
|
none: false
|
160
|
-
requirements:
|
161
|
-
- -
|
162
|
-
- !ruby/object:Gem::Version
|
163
|
-
|
164
|
-
|
165
|
-
- 0
|
166
|
-
version: "0"
|
167
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
144
|
none: false
|
169
|
-
requirements:
|
170
|
-
- -
|
171
|
-
- !ruby/object:Gem::Version
|
172
|
-
|
173
|
-
segments:
|
174
|
-
- 0
|
175
|
-
version: "0"
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
176
149
|
requirements: []
|
177
|
-
|
178
150
|
rubyforge_project: has_face
|
179
151
|
rubygems_version: 1.6.2
|
180
152
|
signing_key:
|
181
153
|
specification_version: 3
|
182
154
|
summary: Easily validate if an image contains faces
|
183
|
-
test_files:
|
155
|
+
test_files:
|
184
156
|
- spec/has_face/test/matchers_spec.rb
|
185
157
|
- spec/has_face/validator_spec.rb
|
186
158
|
- spec/spec_helper.rb
|