social_counter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.0"
11
+ gem "jeweler", "~> 1.8.7"
12
+ end
13
+
14
+ gem 'json'
15
+ gem 'nokogiri'
16
+ gem 'thor'
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ faraday (0.8.8)
7
+ multipart-post (~> 1.2.0)
8
+ git (1.2.6)
9
+ github_api (0.10.1)
10
+ addressable
11
+ faraday (~> 0.8.1)
12
+ hashie (>= 1.2)
13
+ multi_json (~> 1.4)
14
+ nokogiri (~> 1.5.2)
15
+ oauth2
16
+ hashie (2.0.5)
17
+ highline (1.6.19)
18
+ httpauth (0.2.0)
19
+ jeweler (1.8.8)
20
+ builder
21
+ bundler (~> 1.0)
22
+ git (>= 1.2.5)
23
+ github_api (= 0.10.1)
24
+ highline (>= 1.6.15)
25
+ nokogiri (= 1.5.10)
26
+ rake
27
+ rdoc
28
+ json (1.8.1)
29
+ jwt (0.1.8)
30
+ multi_json (>= 1.5)
31
+ multi_json (1.8.2)
32
+ multi_xml (0.5.5)
33
+ multipart-post (1.2.0)
34
+ nokogiri (1.5.10)
35
+ oauth2 (0.9.2)
36
+ faraday (~> 0.8)
37
+ httpauth (~> 0.2)
38
+ jwt (~> 0.1.4)
39
+ multi_json (~> 1.0)
40
+ multi_xml (~> 0.5)
41
+ rack (~> 1.2)
42
+ rack (1.5.2)
43
+ rake (10.1.0)
44
+ rdoc (3.12.2)
45
+ json (~> 1.4)
46
+ thor (0.18.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ bundler (~> 1.0)
53
+ jeweler (~> 1.8.7)
54
+ json
55
+ nokogiri
56
+ rdoc (~> 3.12)
57
+ thor
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 keitarou.oonishi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # social_counter
2
+
3
+ ## Install
4
+ gem install social_counter
5
+
6
+ ## Usage Command Line
7
+
8
+ social_counter -u [page url]
9
+
10
+ ## Usage Ruby Program
11
+ require 'social_counter'
12
+
13
+ @sc = SocialCounter.new('http://google.com')
14
+ p @sc.title
15
+ p @sc.twitter_count
16
+ p @sc.facebook_count
17
+ p @sc.hatena_count
18
+
19
+ ## Support Social Services
20
+ - Twitter
21
+ - Facebook
22
+ - Hatena Bookmark
23
+ - Google Plus
24
+ - Pocket
25
+ - Linkedin
26
+ - Delicious
27
+ - Pinterest
28
+
29
+ ## Copyright
30
+ Copyright (c) 2013 keitarou.oonishi. See LICENSE.txt for
31
+ further details.
32
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "social_counter"
18
+ gem.homepage = "http://github.com/keitarou/social_counter"
19
+ gem.license = "MIT"
20
+ gem.summary = "social network share counter"
21
+ gem.description = "social network share counter"
22
+ gem.email = "keitarou.oonishi@gmail.com"
23
+ gem.authors = ["keitarou.oonishi"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "social_counter #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ #coding: utf-8
3
+
4
+ require 'thor'
5
+ require 'social_counter'
6
+
7
+ class Command < Thor
8
+ default_command :all
9
+
10
+ desc 'all', 'all social share count'
11
+ option :url, :type => :string, :aliases => :'-u', :desc => "url"
12
+ def all
13
+ sc = SocialCounter.new(options[:url])
14
+
15
+ puts sc.title
16
+ puts sc.url
17
+
18
+ sc.all.each do |name, count|
19
+ puts "#{name}: #{count.to_s}"
20
+ end
21
+ end
22
+ end
23
+
24
+ Command.start
@@ -0,0 +1,104 @@
1
+ class SocialCounter
2
+ require 'open-uri'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'nokogiri'
6
+
7
+ attr_accessor :url
8
+
9
+ def initialize url
10
+ @url = url
11
+ end
12
+
13
+ def title
14
+ html = Nokogiri::HTML open(@url)
15
+ html.search('title').text
16
+ end
17
+
18
+ def twitter_count
19
+ request_url = "http://urls.api.twitter.com/1/urls/count.json?url=#{@url}"
20
+ JSON.parser.new(open(request_url).read).parse["count"].to_i
21
+ end
22
+
23
+ def facebook_count
24
+ request_url = "https://graph.facebook.com/?id=#{@url}"
25
+ JSON.parser.new(open(request_url).read).parse["shares"].to_i
26
+ end
27
+
28
+ def hatena_count
29
+ request_url = "http://api.b.st-hatena.com/entry.count?url=#{@url}"
30
+ open(request_url).read.to_i
31
+ end
32
+
33
+ def google_count
34
+ key = 'AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ'
35
+ api = URI.parse("https://clients6.google.com/rpc?key=#{key}")
36
+
37
+ request = Net::HTTP::Post.new(api.request_uri, {
38
+ 'Content-Type' =>'application/json'
39
+ })
40
+ request.body = [{
41
+ :jsonrpc => "2.0",
42
+ :method => "pos.plusones.get",
43
+ :apiVersion => "v1",
44
+ :key => "p",
45
+ :id => "p",
46
+ :params => {
47
+ :id => @url,
48
+ :userId => "@viewer",
49
+ :groupId => "@self",
50
+ :nolog => true,
51
+ },
52
+ }].to_json
53
+
54
+ response = Net::HTTP.start(api.host, api.port, :use_ssl => true) { |http|
55
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
56
+ http.request(request)
57
+ }
58
+
59
+ json = JSON.parse(response.body)
60
+ return json[0]['result']['metadata']['globalCounts']['count'].round
61
+ end
62
+
63
+ def pocket_count
64
+ request_url = "https://widgets.getpocket.com/v1/button?label=pocket&count=vertical&align=left&v=1&url=#{@url}&title=&src=#{@url}&r=#{rand(100000000).to_s}"
65
+ html = Nokogiri::HTML open(URI.escape request_url)
66
+ html.search('#cnt').text.to_i
67
+ end
68
+
69
+ def linkedin_count
70
+ request_url = "http://www.linkedin.com/countserv/count/share?url=#{@url}&format=json"
71
+ JSON.parser.new(open(request_url).read).parse["count"].to_i
72
+ end
73
+
74
+ def delicious_count
75
+ request_url = "http://feeds.delicious.com/v2/json/urlinfo/data?url=#{@url}"
76
+ json = JSON.parser.new(open(request_url).read).parse
77
+ json.count == 1? json[0]["total_posts"].to_i: 0
78
+ end
79
+
80
+ def pinterest_count
81
+ request_url = "http://api.pinterest.com/v1/urls/count.json?url=#{@url}"
82
+ code = open(request_url).read
83
+ code.match(/"count":\s(\d+)/)
84
+ $1.to_i || 0
85
+ end
86
+
87
+ def all
88
+ data = {}
89
+ self.methods.grep(/(.+)_count/) do
90
+ data[$1] = send("#{$1}_count")
91
+ end
92
+ data
93
+ end
94
+
95
+ alias :t :twitter_count
96
+ alias :f :facebook_count
97
+ alias :h :hatena_count
98
+ alias :g :google_count
99
+ alias :po :pocket_count
100
+ alias :l :linkedin_count
101
+ alias :d :delicious_count
102
+ alias :pi :pinterest_count
103
+
104
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'social_counter'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,60 @@
1
+ require './helper'
2
+
3
+ class TestSocialCounter < Test::Unit::TestCase
4
+ def setup
5
+ @url1 = "http://google.com"
6
+ @social_counter = SocialCounter.new(@url1)
7
+ end
8
+
9
+ def test_url
10
+ assert_equal(@social_counter.url, @url1)
11
+ end
12
+
13
+ def test_title
14
+ p "title: #{@social_counter.title}"
15
+ end
16
+
17
+ def test_twitter_count
18
+ p "twitter count: " << @social_counter.twitter_count.to_s
19
+ p "twitter count: " << @social_counter.t.to_s
20
+ end
21
+
22
+ def test_facebook_count
23
+ p "facebook count: " << @social_counter.facebook_count.to_s
24
+ p "facebook count: " << @social_counter.f.to_s
25
+ end
26
+
27
+ def test_hatena_count
28
+ p "hatena count: " << @social_counter.hatena_count.to_s
29
+ p "hatena count: " << @social_counter.h.to_s
30
+ end
31
+
32
+ def test_google_count
33
+ p "google count: " << @social_counter.google_count.to_s
34
+ p "google count: " << @social_counter.g.to_s
35
+ end
36
+
37
+ def test_pocket_count
38
+ p "pocket count: " << @social_counter.pocket_count.to_s
39
+ p "pocket count: " << @social_counter.po.to_s
40
+ end
41
+
42
+ def test_linkedin_count
43
+ p "linkedin count: " << @social_counter.linkedin_count.to_s
44
+ p "linkedin count: " << @social_counter.l.to_s
45
+ end
46
+
47
+ def test_delicious_count
48
+ p "delicious count: " << @social_counter.delicious_count.to_s
49
+ p "delicious count: " << @social_counter.d.to_s
50
+ end
51
+
52
+ def test_pinterest_count
53
+ p "pinterest count: " << @social_counter.pinterest_count.to_s
54
+ p "pinterest count: " << @social_counter.pi.to_s
55
+ end
56
+
57
+ def test_all
58
+ p @social_counter.all
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - keitarou.oonishi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
47
+ name: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.7
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.7
110
+ description: social network share counter
111
+ email: keitarou.oonishi@gmail.com
112
+ executables:
113
+ - social_counter
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ files:
119
+ - .document
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - VERSION
126
+ - bin/social_counter
127
+ - lib/social_counter.rb
128
+ - test/helper.rb
129
+ - test/test_social_counter.rb
130
+ homepage: http://github.com/keitarou/social_counter
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: -3229483074396673623
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.23
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: social network share counter
158
+ test_files: []