social_profile 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,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 social_profile.gemspec
4
+ gemspec
5
+
6
+ gem "rspec", "~> 2.13.0"
7
+ gem "fuubar"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Fodojo.com
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,36 @@
1
+ # SocialProfile
2
+
3
+ Wrapper for Omniauth profile hash https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'social_profile'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install social_profile
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ profile = SocialProfile.get(auth_hash)
23
+ profile.picture_url # http://profile.ak.fbcdn.net/h..._4963049_s.jpg
24
+ profile.gender # 0 - unknown, 1 - female, 2 - male
25
+ profile.profile_url # http://www.facebook.com/develop.rails
26
+ profile.provider # facebook
27
+ ```
28
+
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,102 @@
1
+ # encoding: utf-8
2
+ require "date"
3
+
4
+ module SocialProfile
5
+ module Providers
6
+ class Base
7
+ attr_reader :auth_hash
8
+
9
+ def initialize(hash, options)
10
+ @auth_hash = hash
11
+ @options = options
12
+ end
13
+
14
+ def uid
15
+ @uid ||= auth_hash['uid']
16
+ end
17
+
18
+ def provider
19
+ @provider ||= auth_hash['provider']
20
+ end
21
+
22
+ def name
23
+ @name ||= info('name')
24
+ end
25
+
26
+ def first_name
27
+ @first_name ||= (info('first_name') || name)
28
+ end
29
+
30
+ def last_name
31
+ @last_name ||= info('last_name')
32
+ end
33
+
34
+ def nickname
35
+ @nickname ||= info('nickname')
36
+ end
37
+
38
+ def info(key)
39
+ if info? && Utils.exists?(auth_hash['info'][key])
40
+ auth_hash['info'][key]
41
+ end
42
+ end
43
+
44
+ def extra(key)
45
+ if extra? && Utils.exists?(auth_hash['extra'][key])
46
+ auth_hash['extra'][key]
47
+ end
48
+ end
49
+
50
+ def extra?
51
+ auth_hash['extra'] && auth_hash['extra'].is_a?(Hash)
52
+ end
53
+
54
+ def info?
55
+ auth_hash['info'] && auth_hash['info'].is_a?(Hash)
56
+ end
57
+
58
+ def avatar_url
59
+ @avatar_url ||= info('image')
60
+ end
61
+
62
+ def city_name
63
+ @city_name ||= begin
64
+ loc = info('location')
65
+ loc.nil? ? nil : loc.split(',').pop().strip
66
+ end
67
+ end
68
+
69
+ def access_token
70
+ @access_token ||= credentials['token']
71
+ end
72
+
73
+ def credentials
74
+ @credentials ||= (auth_hash['credentials'] || {})
75
+ end
76
+
77
+ def profile_url
78
+ nil
79
+ end
80
+
81
+ # Возвращаемые значения: 1 - женский, 2 - мужской, 0 - без указания пола.
82
+ def gender
83
+ 0
84
+ end
85
+
86
+ def email
87
+ @email ||= info('email')
88
+ end
89
+
90
+ def token_expires_at
91
+ @token_expires_at ||= parse_datetime(credentials['expires_at'])
92
+ end
93
+
94
+ protected
95
+
96
+ def parse_datetime(value)
97
+ return nil if Utils.blank?(value) || value.to_i.zero?
98
+ Time.at(value.to_i).to_datetime
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ module SocialProfile
3
+ module Providers
4
+ class Facebook < Base
5
+ # http://developers.facebook.com/docs/reference/api/#pictures
6
+ #
7
+ def picture_url
8
+ @picture_url ||= begin
9
+ url = info('image').split('?').shift()
10
+ check_url([url, "type=large"].join('?'))
11
+ end
12
+ end
13
+
14
+ def profile_url
15
+ @profile_url ||= begin
16
+ urls = info('urls')
17
+ urls.nil? ? nil : urls['Facebook']
18
+ end
19
+ end
20
+
21
+ # 0 - unknown
22
+ # 1 - female
23
+ # 2 - male
24
+ # Возвращаемые значения: 1 - женский, 2 - мужской, 0 - без указания пола.
25
+ def gender
26
+ @gender ||= case extra('raw_info')['gender']
27
+ when 'male' then 2
28
+ when 'female' then 1
29
+ else 0
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def check_url(url)
36
+ response = Utils.head(url, :follow_redirects => false)
37
+
38
+ if response.code == 302 && response.headers['location']
39
+ [response.headers['location']].flatten.first
40
+ else
41
+ url
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ module SocialProfile
3
+ module Providers
4
+ class Vkontakte < Base
5
+ def picture_url
6
+ @picture_url ||= begin
7
+ if auth_hash['extra'] && auth_hash['extra']['raw_info']
8
+ photo = auth_hash['extra']['raw_info']['photo_big']
9
+ Utils.blank?(photo) ? nil : photo
10
+ end
11
+ end
12
+ end
13
+
14
+ def profile_url
15
+ @profile_url ||= begin
16
+ urls = info('urls')
17
+ urls.nil? ? nil : urls['Vkontakte']
18
+ end
19
+ end
20
+
21
+ # Возвращаемые значения: 1 - женский, 2 - мужской, 0 - без указания пола.
22
+ #
23
+ def gender
24
+ @gender ||= (extra('raw_info')['sex'] || 0).to_i
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module SocialProfile
2
+ class Response
3
+ attr_reader :uri, :response, :body, :headers
4
+
5
+ def initialize(uri, response, options={})
6
+ @uri = uri
7
+ @response = response
8
+ @body = response.body || options[:body]
9
+ @headers = response.to_hash
10
+ end
11
+
12
+ def code
13
+ response.code.to_i
14
+ end
15
+
16
+ def respond_to?(name)
17
+ return true if [:uri, :response, :body, :headers].include?(name)
18
+ response.respond_to?(name)
19
+ end
20
+
21
+ protected
22
+
23
+ def method_missing(name, *args, &block)
24
+ if response.respond_to?(name)
25
+ response.send(name, *args, &block)
26
+ else
27
+ super
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require "uri"
3
+ require "net/http"
4
+ require "ostruct"
5
+
6
+ module SocialProfile
7
+ class Utils
8
+ def self.head(url, options = {})
9
+ uri = URI.parse(url)
10
+ response = nil
11
+
12
+ Net::HTTP.start(uri.host, uri.port) do |http|
13
+ http.open_timeout = 2
14
+ http.read_timeout = 2
15
+ response = http.head(uri.request_uri)
16
+ end
17
+
18
+ Response.new(uri, response, options)
19
+ end
20
+
21
+ def self.blank?(value)
22
+ value.nil? || value.to_s.empty?
23
+ end
24
+
25
+ def self.exists?(value)
26
+ !blank?(value)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module SocialProfile
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require "social_profile/version"
3
+
4
+ module SocialProfile
5
+ autoload :Utils, "social_profile/utils"
6
+ autoload :Response, "social_profile/response"
7
+
8
+ module Providers
9
+ autoload :Base, "social_profile/providers/base"
10
+ autoload :Facebook, "social_profile/providers/facebook"
11
+ autoload :Vkontakte, "social_profile/providers/vkontakte"
12
+ end
13
+
14
+ def self.get(auth_hash, options = {})
15
+ provider = auth_hash["provider"].to_s.downcase if auth_hash && auth_hash["provider"]
16
+
17
+ klass = case provider
18
+ when "facebook" then Providers::Facebook
19
+ when "vkontakte" then Providers::Vkontakte
20
+ else Providers::Base
21
+ end
22
+
23
+ klass.new(auth_hash, options)
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'social_profile/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "social_profile"
8
+ spec.version = SocialProfile::VERSION
9
+ spec.authors = ["Igor Galeta"]
10
+ spec.email = ["galeta.igor@gmail.com"]
11
+ spec.description = %q{Wrapper for Omniauth profile hash}
12
+ spec.summary = %q{Wrapper for Omniauth profile hash}
13
+ spec.homepage = "https://github.com/galetahub/social_profile"
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 "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SocialProfile::Providers::Facebook do
5
+ it "should be a Module" do
6
+ SocialProfile::Providers.should be_a(Module)
7
+ end
8
+
9
+ context "facebook" do
10
+ before(:each) do
11
+ hash = {"provider"=>"facebook", "uid"=>"100000730417342", "info"=>{"nickname"=>"pavel.galeta", "email"=>"email@gmail.com", "name"=>"Pavel Galeta", "first_name"=>"Pavel", "last_name"=>"Galeta", "image"=>"http://graph.facebook.com/100000730417342/picture?type=square", "description"=>"введи в Google super_p", "urls"=>{"Facebook"=>"http://www.facebook.com/pavel.galeta"}, "location"=>"Kyiv, Ukraine", "verified"=>true}, "credentials"=>{"token"=>"AAAC1N4JHfIcBAIDYp0QLdyCMX9LenAS6KNDrGNZAOQ8bOYObHSg3tgxzvEVNLOTCZBOHDUCcHDxINgluKw52CLxMMZAxHaPXqAwZABMqhgZDZD", "expires_at"=>1369429974, "expires"=>true}, "extra"=>{"raw_info"=>{"id"=>"100000730417342", "name"=>"Pavel Galeta", "first_name"=>"Pavel", "last_name"=>"Galeta", "link"=>"http://www.facebook.com/pavel.galeta", "username"=>"pavel.galeta", "hometown"=>{"id"=>"108505112504762", "name"=>"Myronivka"}, "location"=>{"id"=>"111227078906045", "name"=>"Kyiv, Ukraine"}, "bio"=>"введи в Google super_p", "quotes"=>"Мир становится всё web'анутее и web'анутее ...", "work"=>[], "favorite_teams"=>[], "favorite_athletes"=>[], "education"=>[], "gender"=>"male", "email"=>"mail@gmail.com", "timezone"=>2, "locale"=>"en_US", "languages"=>[], "verified"=>true, "updated_time"=>"2013-03-24T21:02:30+0000"}}}
12
+ @profile = SocialProfile.get(hash)
13
+ end
14
+
15
+ it "should be a facebook profile" do
16
+ @profile.should be_a(SocialProfile::Providers::Facebook)
17
+ end
18
+
19
+ it "should parse profile" do
20
+ @profile.name.should == "Pavel Galeta"
21
+ @profile.email.should == "email@gmail.com"
22
+ @profile.picture_url.should == "http://profile.ak.fbcdn.net/hprofile-ak-prn1/41634_100000730417342_973_n.jpg"
23
+ @profile.gender.should == 2
24
+ @profile.profile_url.should == "http://www.facebook.com/pavel.galeta"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SocialProfile::Providers::Vkontakte do
5
+ it "should be a Module" do
6
+ SocialProfile::Providers.should be_a(Module)
7
+ end
8
+
9
+ context "vkontakte" do
10
+ before(:each) do
11
+ hash = {"info"=>{"name"=>"Павел Галета", "location"=>"Ukraine, Kiev", "urls"=>{"Vkontakte"=>"http://vkontakte.ru/id2592709"}, "nickname"=>"", "birth_date"=>"10.3.1987", "last_name"=>"Галета", "image"=>"http://cs10901.vkontakte.ru/u2592709/e_f8b6e3b9.jpg", "first_name"=>"Павел"}, "uid"=>2592709, "credentials"=>{"token"=>"7a1cfdd37a3b72167a3b7216ac7a1347bde7a3b7a3a7216c95a735a210be6d4"}, "extra"=>{"raw_info"=>{"timezone"=>2, "sex"=>"2", "photo_big"=>"http://cs10901.vkontakte.ru/u2592709/a_d8f124ce.jpg"}}, "provider"=>"vkontakte"}
12
+ @profile = SocialProfile.get(hash)
13
+ end
14
+
15
+ it "should be a facebook profile" do
16
+ @profile.should be_a(SocialProfile::Providers::Vkontakte)
17
+ end
18
+
19
+ it "should parse profile" do
20
+ @profile.name.should == "Павел Галета"
21
+ @profile.picture_url.should == "http://cs10901.vkontakte.ru/u2592709/a_d8f124ce.jpg"
22
+ @profile.gender.should == 2
23
+ @profile.profile_url.should == "http://vkontakte.ru/id2592709"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require "rspec"
3
+ require "social_profile"
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,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_profile
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-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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
+ description: Wrapper for Omniauth profile hash
47
+ email:
48
+ - galeta.igor@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/social_profile.rb
60
+ - lib/social_profile/providers/base.rb
61
+ - lib/social_profile/providers/facebook.rb
62
+ - lib/social_profile/providers/vkontakte.rb
63
+ - lib/social_profile/response.rb
64
+ - lib/social_profile/utils.rb
65
+ - lib/social_profile/version.rb
66
+ - social_profile.gemspec
67
+ - spec/providers/facebook_spec.rb
68
+ - spec/providers/vkontakte_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: https://github.com/galetahub/social_profile
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.25
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Wrapper for Omniauth profile hash
95
+ test_files:
96
+ - spec/providers/facebook_spec.rb
97
+ - spec/providers/vkontakte_spec.rb
98
+ - spec/spec_helper.rb