face 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in face.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ Face is a ruby library of http://developers.face.com/.
2
+
3
+ == Getting Started
4
+
5
+ sudo gem install face
6
+
7
+ irb
8
+ >> require 'face'
9
+ >> client = Face.get_client(:api_key => 'your_api_key', :api_secret => 'your_api_secret')
10
+
11
+ Detect Faces with Urls:
12
+
13
+ >> client.faces_detect(:urls => ['http://farm6.static.flickr.com/5220/5431220348_fbdf80ae9.jpg'])
14
+
15
+ Detect Faces with Raw image data:
16
+
17
+ >> client.faces_detect(:file => File.new('image.jpg', 'rb'))
18
+
19
+ More Documentation refer to http://developers.face.com/.
20
+
21
+
22
+ == Copyright
23
+
24
+ (The MIT License)
25
+
26
+ Copyright (c) 2010 {rociiu (Roc Yu)}[http://rociiu.com]
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining
29
+ a copy of this software and associated documentation files (the
30
+ "Software"), to deal in the Software without restriction, including
31
+ without limitation the rights to use, copy, modify, merge, publish,
32
+ distribute, sublicense, and/or sell copies of the Software, and to
33
+ permit persons to whom the Software is furnished to do so, subject to
34
+ the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be
37
+ included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
43
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
44
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/face.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "face/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "face"
7
+ s.version = Face::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Roc Yu"]
10
+ s.email = ["rociiu.yu@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/face"
12
+ s.summary = %q{Ruby wraper of face.com api}
13
+ s.description = %q{}
14
+
15
+ s.rubyforge_project = "face"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "rest-client"
23
+ s.add_dependency "json"
24
+
25
+ end
data/lib/ext/hash.rb ADDED
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../object', __FILE__)
2
+
3
+ class Hash
4
+
5
+ def assert_valid_keys(*valid_keys)
6
+ unknown_keys = keys - [valid_keys].flatten
7
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
8
+ end
9
+
10
+ def to_param(namespace = nil)
11
+ collect do |key, value|
12
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
13
+ end.sort * '&'
14
+ end
15
+
16
+ end
data/lib/ext/object.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Object
2
+
3
+ def to_param
4
+ to_s
5
+ end
6
+
7
+ def to_query(key)
8
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
9
+ "#{CGI.escape(key.to_s).gsub(/%(5B|5D)/n) { [$1].pack('H*') }}=#{CGI.escape(to_param.to_s)}"
10
+ end
11
+
12
+ end
data/lib/face.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'ruby-debug'
3
+ require 'rest_client'
4
+ require 'json'
5
+ require 'pp'
6
+
7
+ require File.expand_path('../face/client', __FILE__)
8
+
9
+ module Face
10
+
11
+ def self.get_client(opts={})
12
+ Face::Client::Base.new(opts)
13
+ end
14
+
15
+ end
16
+
17
+ #client = Face.get_client(:api_key => '1a16f5a64ae7f87677f8d16e621cb918', :api_secret => '2fb4249e299ed6a01cb38145837b9525')
18
+ #pp client.faces_detect(:urls => ['http://farm6.static.flickr.com/5220/5431220348_fbdf80ae9.jpg'])
19
+ ##pp client.faces_detect(:file => File.new('5431220348_fbdf810ae9.jpg', 'rb'))
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../../ext/hash', __FILE__)
2
+ require File.expand_path('../client/base', __FILE__)
3
+
4
+ module Face
5
+ module Client
6
+
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ module Face
2
+ module Client
3
+ module Accounts
4
+ def account_limits(opts={})
5
+ make_request(:tags_remove, opts.merge(user_auth_param))
6
+ end
7
+
8
+ def account_users(opts={})
9
+ make_request(:account_users, opts.merge(user_auth_param))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../utils', __FILE__)
2
+ require File.expand_path('../recognition', __FILE__)
3
+ require File.expand_path('../tags', __FILE__)
4
+ require File.expand_path('../accounts', __FILE__)
5
+
6
+ module Face
7
+ module Client
8
+ class Base
9
+
10
+ attr_accessor :api_key, :api_secret
11
+
12
+ #{
13
+ # twitter_username => [twitter screen name]
14
+ # twitter_password => [twitter password]
15
+ #}
16
+ attr_accessor :twitter_credentials
17
+
18
+ #{
19
+ # twitter_oauth_user => [twitter OAuth user]
20
+ # twitter_oauth_secret => [twitter OAuth secret]
21
+ # twitter_oauth_token => [twitter OAuth token]
22
+ #}
23
+ attr_accessor :twitter_oauth_credentials
24
+
25
+ #{
26
+ # fb_user => [facebook user id]
27
+ # fb_session => [facebook session id]
28
+ # fb_oauth_token => [facebook oauth 2.0 access token]
29
+ #}
30
+ attr_accessor :facebook_credentials
31
+
32
+ include Face::Client::Utils
33
+ include Face::Client::Recognition
34
+ include Face::Client::Tags
35
+ include Face::Client::Accounts
36
+
37
+
38
+ def initialize(opts={})
39
+ opts.assert_valid_keys(:api_key, :api_secret)
40
+ @api_key, @api_secret = opts[:api_key], opts[:api_secret]
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module Face
2
+ module Client
3
+ module Recognition
4
+ # http://api.face.com/faces/detect.json
5
+ def faces_detect(opts={})
6
+ opts.assert_valid_keys(:urls)
7
+ make_request(:faces_detect, opts)
8
+ end
9
+
10
+ def faces_recognize(opts={})
11
+ opts.assert_valid_keys(:uids, :urls)
12
+ make_request(:faces_recognize, opts.merge(user_auth_param))
13
+ end
14
+
15
+ def faces_train(opts={})
16
+ opts.assert_valid_keys(:uids)
17
+ make_request(:faces_train, opts.merge(user_auth_param))
18
+ end
19
+
20
+ def faces_status(opts={})
21
+ make_request(:faces_status, opts.merge(user_auth_param))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module Face
2
+ module Client
3
+ module Tags
4
+ def tags_get(opts={})
5
+ make_request(:tags_get, opts.merge(user_auth_param))
6
+ end
7
+
8
+ def tags_add(opts={})
9
+ make_request(:tags_add, opts.merge(user_auth_param))
10
+ end
11
+
12
+ def tags_save(opts={})
13
+ make_request(:tags_save, opts.merge(user_auth_param))
14
+ end
15
+
16
+ def tags_remove(opts={})
17
+ make_request(:tags_remove, opts.merge(user_auth_param))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ module Face
2
+ module Client
3
+ module Utils
4
+
5
+ class FaceError < StandardError; end
6
+
7
+ API_METHODS = {
8
+ :faces_detect => 'http://api.face.com/faces/detect.json',
9
+ :faces_recognize => 'http://api.face.com/faces/recognize.json',
10
+ :faces_train => 'http://api.face.com/faces/train.json',
11
+ :faces_status => 'http://api.face.com/faces/status.json',
12
+ :tags_get => 'http://api.face.com/tags/get.json',
13
+ :tags_add => 'http://api.face.com/tags/add.json',
14
+ :tags_save => 'http://api.face.com/tags/save.json',
15
+ :tags_remove => 'http://api.face.com/tags/remove.json',
16
+ :account_limits => 'http://api.face.com/account/limits.json',
17
+ :account_users => 'http://api.face.com/account/users.json'
18
+ }
19
+
20
+ def api_crendential
21
+ { :api_key => api_key, :api_secret => api_secret }
22
+ end
23
+
24
+ def make_request(api_method, opts={})
25
+ if opts[:urls].is_a? Array
26
+ opts[:urls] = opts[:urls].join(',')
27
+ end
28
+
29
+ if opts[:uids].is_a? Array
30
+ opts[:uids] = opts[:uids].join(',')
31
+ end
32
+ response = JSON.parse( RestClient.post(API_METHODS[ api_method ], opts.merge(api_crendential)) )
33
+ if response['status'] == 'success'
34
+ response
35
+ elsif response['status'] == 'failure'
36
+ raise FaceError.new("Error: #{response['error_code']}, #{response['error_message']}")
37
+ end
38
+ end
39
+
40
+ def user_auth_param
41
+ user_auth_value = []
42
+ if twitter_credentials
43
+ twitter_credentials.each do |k, v|
44
+ user_auth_value << "#{k}:#{v}"
45
+ end
46
+ elsif twitter_oauth_credentials
47
+ twitter_oauth_credentials.each do |k,v|
48
+ user_auth_value << "#{k}:#{v}"
49
+ end
50
+ end
51
+ if facebook_credentials
52
+ facebook_credentials.each do |k, v|
53
+ user_auth_value << "#{k}:#{v}"
54
+ end
55
+ end
56
+ user_auth_value.size > 0 ? { :user_auth => user_auth_value.join(',') } : {}
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Face
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: face
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Roc Yu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-12 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rest-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: ""
50
+ email:
51
+ - rociiu.yu@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - README.rdoc
62
+ - Rakefile
63
+ - face.gemspec
64
+ - lib/ext/hash.rb
65
+ - lib/ext/object.rb
66
+ - lib/face.rb
67
+ - lib/face/client.rb
68
+ - lib/face/client/accounts.rb
69
+ - lib/face/client/base.rb
70
+ - lib/face/client/recognition.rb
71
+ - lib/face/client/tags.rb
72
+ - lib/face/client/utils.rb
73
+ - lib/face/version.rb
74
+ has_rdoc: true
75
+ homepage: http://rubygems.org/gems/face
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project: face
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Ruby wraper of face.com api
108
+ test_files: []
109
+