thecount 0.1

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/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle/*
3
+ .DS_Store
4
+ Gemfile.lock
5
+ coverage/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=nested
3
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+ gem 'rspec', '~> 2.0.1'
3
+ gem 'json'
4
+ gem 'rr'
5
+
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ TheCount
2
+ Copyright (c) 2011 Ian Chan, http://github.com/chanian/thecount
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ TheCount Gem
2
+ ====================
3
+ Provides a simple interface for counting and reporting arbitrary things on the web
4
+
5
+ ![Mascot](http://4.bp.blogspot.com/-TCPvrnG-KCs/TfMjpWepAKI/AAAAAAAAJWA/49r4o_q6JaA/s1600/TheCount.jpg)
6
+
7
+
8
+ Sample Usage
9
+ -------------------
10
+ # Setup a counting request
11
+ data = TheCount::count do |config|
12
+ # What do we want to count
13
+ config[:strategies] = [ TheCount::Twitter, TheCount::Facebook, TheCount::LinkedIn ]
14
+
15
+ # Where do we want to count it
16
+ config[:args] = { :url => "http://techcrunch.com/2011/06/16/rebecca-black-friday-video-no-longer-available-on-youtube/" }
17
+ )
18
+
19
+ # Print out all the values
20
+ data.each { |count|
21
+ p "#{count.service_name} -> #{count.value}"
22
+ }
23
+ # (at the time of this example)
24
+ # twitter -> 900
25
+ # facebook -> 3000
26
+ # linkedin -> 444
27
+
28
+ Supported Services
29
+ -------------------
30
+ - Twitter Link Shares
31
+ - Facebook Likes
32
+ - Facebook Comment threads
33
+ - LinkedIn Shares
34
+ - Google Buzzes
35
+ - Digg diggs
36
+
37
+ Tests
38
+ -------------------
39
+ To run the test suite run
40
+
41
+ rake test
42
+
43
+ Notes
44
+ -------------------
45
+ This library essentially makes calls to the otherwise jsonp endpoint to the respective service. I've also purposely gone through the client api's like this to avoid the need to auth.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TheCount
6
+ class Digg < TheCount::Countable
7
+ def initialize
8
+ @service_name = "digg"
9
+ @unit = "digg"
10
+ @value = 0
11
+ end
12
+
13
+ def count(data)
14
+ url = data[:url]
15
+ begin
16
+ url = "http://widgets.digg.com/buttons/count?url=#{url}"
17
+ @value = JSON.parse(open(url).string.split("(")[1].split(");")[0])["diggs"].to_i
18
+ rescue
19
+ @value = 0
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TheCount
6
+ class Disqus < TheCount::Countable
7
+ def initialize
8
+ @service_name = 'disqus'
9
+ @unit = 'comment'
10
+ @value = 0
11
+ end
12
+
13
+ def count(data)
14
+ post_id = data[:post_id]
15
+ domain = data[:domain]
16
+ base_url = "http://www.#{domain}.com/"
17
+ begin
18
+ url = "http://#{domain}.disqus.com/count.js?q=1&1=1,#{post_id}%20#{CGI::escape(base_url)}%3Fp%3D#{post_id}"
19
+ @value = open(url).string.split('comments": ')[2].split("}")[0].to_i
20
+ rescue Exception => e
21
+ @value = 0
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TheCount
6
+ class Facebook < TheCount::Countable
7
+ def initialize
8
+ @service_name = "facebook"
9
+ @unit = "like"
10
+ @value = 0
11
+ end
12
+
13
+ def count(data)
14
+ url = data[:url]
15
+ url = "https://graph.facebook.com/#{url}"
16
+ begin
17
+ @value = JSON.parse(open(url).string)["shares"].to_i
18
+ rescue
19
+ @value = 0
20
+ end
21
+ end
22
+
23
+ # Some additional Facebook services
24
+ class Sends < Countable
25
+ def initialize
26
+ @service_name = "facbeook"
27
+ @unit = 'send'
28
+ @value = 0
29
+ end
30
+ end
31
+
32
+ class Comments < Countable
33
+ def initialize
34
+ @service_name = "facebook"
35
+ @unit = "comment"
36
+ @value = 0
37
+ end
38
+
39
+ def count(data)
40
+ url = data[:url]
41
+ query = CGI::escape("\"index_link_stat_url\":\"select url,commentsbox_count from link_stat where url=\\\"#{url}\\\"\"")
42
+ url = "https://api-read.facebook.com/restserver.php?format=json&method=fql.multiquery&queries=%7B#{query}%7D"
43
+ begin
44
+ @value = JSON.parse(open(url).string)[0]["fql_result_set"][0]["commentsbox_count"].to_i
45
+ rescue Exception => e
46
+ @value = 0
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ # Since Google is in between Buzz and +1, lets
6
+ # make no assumptions on a default and fully qualify both
7
+ module TheCount
8
+ class Google
9
+ class Buzz < TheCount::Countable
10
+ def initialize
11
+ @service_name = "google"
12
+ @unit = "buzz"
13
+ @value = 0
14
+ end
15
+ def count(data)
16
+ url = "http://www.google.com/buzz/api/buzzThis/buzzCounter?url="
17
+ url = "#{url}#{CGI::escape(data[:url])}"
18
+ begin
19
+ @value = JSON.parse(open(url).string.split('(')[1].split(')')[0])[data[:url]].to_i
20
+ rescue Exception => e
21
+ @value = 0
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TheCount
6
+ class LinkedIn < TheCount::Countable
7
+ def initialize
8
+ @service_name = "linkedin"
9
+ @unit = "share"
10
+ @value = 0
11
+ end
12
+
13
+ def count(data)
14
+ url = data[:url]
15
+ begin
16
+ url = "http://www.linkedin.com/countserv/count/share?url=#{url}"
17
+ @value = JSON.parse(open(url).string.split("(")[1].split(");")[0])["count"].to_i
18
+ rescue
19
+ @value = 0
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TheCount
6
+ class Twitter < TheCount::Countable
7
+ def initialize
8
+ @service_name = "twitter"
9
+ @unit = "tweet"
10
+ @value = 0
11
+ end
12
+
13
+ def count(data)
14
+ url = data[:url]
15
+ begin
16
+ url = "http://urls.api.twitter.com/1/urls/count.json?url=#{url}"
17
+ @value = JSON.parse(open(url).string)["count"].to_i
18
+ rescue Exception => e
19
+ p e
20
+ @value = 0
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module TheCount
2
+ class Countable
3
+ attr_reader :unit, :value, :service_name
4
+
5
+ def count(url)
6
+ raise "This countable object must have a count strategy implemented"
7
+ end
8
+ end
9
+ end
data/lib/thecount.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'thecount/countable'
2
+ require 'thecount/countable/twitter'
3
+ require 'thecount/countable/facebook'
4
+ require 'thecount/countable/linkedin'
5
+ require 'thecount/countable/disqus'
6
+ require 'thecount/countable/google'
7
+ require 'thecount/countable/digg'
8
+
9
+ module TheCount
10
+ def self.count
11
+ config = {}
12
+ yield config
13
+
14
+ strategies = config[:strategies]
15
+ args = config[:args]
16
+
17
+ strategies = [strategies] unless strategies.kind_of?(Array)
18
+ strategies.collect { |s|
19
+ service = s.new
20
+ service.count(args)
21
+ service
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::Digg do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults" do
7
+ digg = TheCount::Digg.new
8
+
9
+ digg.service_name.should eql "digg"
10
+ digg.unit.should eql "digg"
11
+ digg.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ digg = TheCount::Digg.new
17
+ digg.count({ :url => 'http://reddit.com' })
18
+ digg.value.should > 0
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::Disqus do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults" do
7
+ t = TheCount::Disqus.new
8
+
9
+ t.service_name.should eql "disqus"
10
+ t.unit.should eql "comment"
11
+ t.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ t = TheCount::Disqus.new
17
+ t.count({ :domain => 'techcrunch',
18
+ :post_id => 58217
19
+ })
20
+ t.value.should > 0
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::Facebook do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults, and default to 'like'" do
7
+ fb = TheCount::Facebook.new
8
+
9
+ fb.service_name.should eql "facebook"
10
+ fb.unit.should eql "like"
11
+ fb.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ fb = TheCount::Facebook.new
17
+ fb.count({ :url => 'http://reddit.com' })
18
+ fb.value.should > 0
19
+ end
20
+ end
21
+
22
+ describe TheCount::Facebook::Comments do
23
+ describe "#initialize" do
24
+ it "should set the correct defaults" do
25
+ fb = TheCount::Facebook::Comments.new
26
+
27
+ fb.service_name.should eql "facebook"
28
+ fb.unit.should eql "comment"
29
+ fb.value.should be 0
30
+ end
31
+ end
32
+ describe "#count" do
33
+ it "should return a non 0 integer for the count" do
34
+ url = 'http://techcrunch.com/2011/06/16/rebecca-black-friday-video-no-longer-available-on-youtube/'
35
+ fb = TheCount::Facebook::Comments.new
36
+ fb.count({ :url => url })
37
+ fb.value.should > 0
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::Google::Buzz do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults" do
7
+ goog = TheCount::Google::Buzz.new
8
+
9
+ goog.service_name.should eql "google"
10
+ goog.unit.should eql "buzz"
11
+ goog.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ goog = TheCount::Google::Buzz.new
17
+ goog.count({ :url => 'http://www.google.com/buzz' })
18
+ goog.value.should > 0
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::LinkedIn do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults" do
7
+ li = TheCount::LinkedIn.new
8
+
9
+ li.service_name.should eql "linkedin"
10
+ li.unit.should eql "share"
11
+ li.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ li = TheCount::LinkedIn.new
17
+ li.count({ :url => 'http://google.com' })
18
+ li.value.should > 0
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'helper'
3
+
4
+ describe TheCount::Twitter do
5
+ describe "#initialize" do
6
+ it "should set the correct defaults" do
7
+ t = TheCount::Twitter.new
8
+
9
+ t.service_name.should eql "twitter"
10
+ t.unit.should eql "tweet"
11
+ t.value.should be 0
12
+ end
13
+ end
14
+ describe "#count" do
15
+ it "should return a non 0 integer for the count" do
16
+ t = TheCount::Twitter.new
17
+ t.count({ :url => 'http://google.com' })
18
+ t.value.should > 0
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require 'rr'
2
+ require 'rspec'
3
+ require 'helper'
4
+
5
+ describe TheCount::Countable do
6
+ describe "defaults" do
7
+ it "should provide accessors to the basic variables" do
8
+ c = TheCount::Countable.new
9
+ c.respond_to?('service_name').should be true
10
+ c.respond_to?('unit').should be true
11
+ c.respond_to?('value').should be true
12
+ end
13
+ end
14
+
15
+ describe "#count" do
16
+ it "should raise an exception when explicitly called" do
17
+ lambda {TheCount::Countable.new.count('')}.should raise_error
18
+ end
19
+ end
20
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'thecount'
@@ -0,0 +1,31 @@
1
+ require 'rr'
2
+ require 'rspec'
3
+ require 'helper'
4
+
5
+ describe TheCount do
6
+ describe "#count" do
7
+ context "given a single strategy input" do
8
+ it "should return an single array with an instance of the given stategy" do
9
+ counts = TheCount::count do |config|
10
+ config[:strategies] = TheCount::Twitter
11
+ config[:args] = { :url => 'http://google.com' }
12
+ end
13
+
14
+ counts[0].kind_of?(TheCount::Twitter).should be true
15
+ end
16
+ end
17
+ context "given multiple strategies as input" do
18
+ it "should return an array of strategies" do
19
+ counts = TheCount::count do |config|
20
+ config[:strategies] = [TheCount::Facebook, TheCount::Twitter]
21
+ config[:args] = { :url => 'http://google.com' }
22
+ end
23
+
24
+ counts.kind_of?(Array).should be true
25
+ counts.size.should be 2
26
+ counts[0].kind_of?(TheCount::Facebook).should be true
27
+ counts[1].kind_of?(TheCount::Twitter).should be true
28
+ end
29
+ end
30
+ end
31
+ end
data/thecount.gemspec ADDED
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'thecount'
3
+ gem.version = '0.1'
4
+ gem.date = '2011-06-18'
5
+ gem.summary = "A simple generic counting service"
6
+ gem.description = "Provides a simple interface for counting and reporting arbitrary things on the web"
7
+ gem.author = "Ian Chan"
8
+ gem.email = 'ianchan.can@gmail.com'
9
+ gem.license = 'MIT'
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.require_paths = ['lib']
12
+ gem.homepage = 'http://github.com/chanian/thecount'
13
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thecount
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Ian Chan
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-06-18 00:00:00 -07:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Provides a simple interface for counting and reporting arbitrary things on the web
21
+ email: ianchan.can@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - .gitignore
30
+ - .rspec
31
+ - Gemfile
32
+ - LICENSE
33
+ - README.md
34
+ - Rakefile
35
+ - lib/thecount.rb
36
+ - lib/thecount/countable.rb
37
+ - lib/thecount/countable/digg.rb
38
+ - lib/thecount/countable/disqus.rb
39
+ - lib/thecount/countable/facebook.rb
40
+ - lib/thecount/countable/google.rb
41
+ - lib/thecount/countable/linkedin.rb
42
+ - lib/thecount/countable/twitter.rb
43
+ - spec/countable/digg_spec.rb
44
+ - spec/countable/disqus_spec.rb
45
+ - spec/countable/facebook_spec.rb
46
+ - spec/countable/google_spec.rb
47
+ - spec/countable/linkedin_spec.rb
48
+ - spec/countable/twitter_spec.rb
49
+ - spec/countable_spec.rb
50
+ - spec/helper.rb
51
+ - spec/thecount_spec.rb
52
+ - thecount.gemspec
53
+ has_rdoc: true
54
+ homepage: http://github.com/chanian/thecount
55
+ licenses:
56
+ - MIT
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: A simple generic counting service
83
+ test_files: []
84
+