shared_count_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c296f7db9d808d5a5ca56f2e65ef1c3c982a020a
4
+ data.tar.gz: 13a35672bdee8df4aad645357ebe1e2494ab41a1
5
+ SHA512:
6
+ metadata.gz: 8e400c6453447b54f0653887acd6c5bce1eaeb7f8f4d85f8266fb90f7fc05b97b477e7ad75a89dc400ebf835eee22e0df3c93e74bdca6484d509dedba7a97f72
7
+ data.tar.gz: 367c36c0cfe875c0c18cb68f09cd7067ed2a45bad3acbb26f15d0df539fa5e07b63775c5daad1736498acaf4ecc74dbad0ddc99fda30d908d8b6e9ac8c98be93
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .rbenv-gemsets
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shared_count_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Cristian Rasch
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
+ # SharedCountApi
2
+
3
+ Thin wrapper around the SharedCount API in vanilla Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shared_count_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shared_count_api
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "shared_count_api"
23
+ client = SharedCountApi::Client.new("http://slashdot.org")
24
+ client.twitter # => 24381
25
+ client.facebook_share_count # => 2705
26
+ client.google_plus_one # => 16633
27
+ # you get the drill ;)
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,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ t.verbose = true
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,3 @@
1
+ module SharedCountApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,95 @@
1
+ require_relative "shared_count_api/version"
2
+ require "net/http"
3
+ require "json"
4
+ require "ostruct"
5
+
6
+ module SharedCountApi
7
+ class Error < StandardError
8
+ attr_reader :type, :error
9
+
10
+ def initialize(type, message)
11
+ @type, @message = type, message
12
+ end
13
+ end
14
+
15
+ INVALID_URL = Error.new("invalid_url", "Not a valid URL.")
16
+
17
+ class Client
18
+ HTTP_ENDPOINT = "http://api.sharedcount.com/".freeze
19
+ HTTPS_ENDPOINT = "https://sharedcount.appspot.com/".freeze
20
+
21
+ def initialize(url, use_ssl = false)
22
+ @url, @use_ssl = url, use_ssl
23
+ end
24
+
25
+ def stumble_upon
26
+ response["StumbleUpon"]
27
+ end
28
+
29
+ def reddit
30
+ response["Reddit"]
31
+ end
32
+
33
+ def facebook_like_count
34
+ facebook_metrics["like_count"]
35
+ end
36
+
37
+ def facebook_share_count
38
+ facebook_metrics["share_count"]
39
+ end
40
+
41
+ def delicious
42
+ response["Delicious"]
43
+ end
44
+
45
+ def google_plus_one
46
+ response["GooglePlusOne"]
47
+ end
48
+
49
+ def buzz
50
+ response["Buzz"]
51
+ end
52
+
53
+ def twitter
54
+ response["Twitter"]
55
+ end
56
+
57
+ def diggs
58
+ response["Diggs"]
59
+ end
60
+
61
+ def pinterest
62
+ response["Pinterest"]
63
+ end
64
+
65
+ def linked_in
66
+ response["LinkedIn"]
67
+ end
68
+
69
+ private
70
+
71
+ def facebook_metrics
72
+ @facebook_metrics ||= response["Facebook"].is_a?(Hash) ? response["Facebook"] : Hash.new(0)
73
+ end
74
+
75
+ def response
76
+ @response ||= begin
77
+ endpoint = @use_ssl ? HTTPS_ENDPOINT : HTTP_ENDPOINT
78
+ begin
79
+ uri = URI("#{endpoint}?url=#{@url}")
80
+ res = Net::HTTP.get_response(uri)
81
+
82
+ case res
83
+ when Net::HTTPUnauthorized, Net::HTTPBadRequest then
84
+ json = JSON.parse(res.body)
85
+ raise Error.new(json["Type"], json["Error"])
86
+ when Net::HTTPSuccess then
87
+ JSON.parse(res.body)
88
+ end
89
+ rescue URI::InvalidURIError
90
+ raise INVALID_URL
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shared_count_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shared_count_api"
8
+ spec.version = SharedCountApi::VERSION
9
+ spec.authors = ["Cristian Rasch"]
10
+ spec.email = ["cristianrasch@gmail.com"]
11
+ spec.description = %q{Thin wrapper around the SharedCount API in vanilla Ruby}
12
+ spec.summary = %q{This library wraps the SharedCount API exposing its data through POROs (Plain Old Ruby Objects)}
13
+ spec.homepage = "https://github.com/wecodeio/shared_count_api"
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 "minitest", "~> 5.0.8"
22
+ spec.add_development_dependency "minitest-line", "~> 0.5.0"
23
+ spec.add_development_dependency "webmock", "~> 1.16.0"
24
+ end
@@ -0,0 +1,118 @@
1
+ require "bundler/setup"
2
+ require "minitest/autorun"
3
+ require "minitest/pride"
4
+ require "webmock/minitest"
5
+ require "uri"
6
+ require "json"
7
+ require "pathname"
8
+
9
+ require_relative "../lib/shared_count_api"
10
+
11
+ describe SharedCountApi::Client do
12
+ let(:url) { "http://slashdot.org" }
13
+ let(:body) do
14
+ { StumbleUpon: 603192, Reddit: 0,
15
+ Facebook: { commentsbox_count: 15, click_count: 27523, total_count: 335835, comment_count: 100278,
16
+ like_count: 84709, share_count: 150848},
17
+ Delicious: 11969, GooglePlusOne: 76352, Buzz: 0, Twitter: 22199, Diggs: 0, Pinterest: 16081,
18
+ LinkedIn: 1532 }
19
+ end
20
+ subject { SharedCountApi::Client.new(url) }
21
+
22
+ describe "when the free API quota has been exceeded" do
23
+ before do
24
+ body = { "Error" => "Free API quota exceeded by more than 50%. Quota will reset tomorrow. Contact SharedCount to inquire about paid plans. http://sharedcount.com/quota",
25
+ "Type" => "quota_exceeded" }
26
+ stub_request(:get, Regexp.new(SharedCountApi::Client::HTTP_ENDPOINT)).
27
+ to_return(body: JSON.generate(body), status: 401, headers: { "X-IP-Quota-Remaining" => 0 })
28
+
29
+ end
30
+
31
+ it "raises the appropriate error" do
32
+ proc {
33
+ subject.stumble_upon
34
+ }.must_raise SharedCountApi::Error
35
+ end
36
+ end
37
+
38
+ describe "when the provided URL is invalid" do
39
+ before do
40
+ body = { "Error" => "Not a valid URL.", "Type" => "invalid_url" }
41
+ stub_request(:get, Regexp.new(SharedCountApi::Client::HTTP_ENDPOINT)).
42
+ to_return(body: JSON.generate(body), status: 400)
43
+
44
+ end
45
+
46
+ let(:url) { "invalid url" }
47
+
48
+ it "raises the appropriate error" do
49
+ proc {
50
+ subject.stumble_upon
51
+ }.must_raise SharedCountApi::Error
52
+ end
53
+ end
54
+
55
+ describe "when security is of the utmost importance" do
56
+ subject { SharedCountApi::Client.new(url, true) }
57
+ before do
58
+ stub_request(:get, Regexp.new(SharedCountApi::Client::HTTPS_ENDPOINT)).
59
+ to_return(body: JSON.generate(body))
60
+ end
61
+
62
+ it "makes requests over SSL" do
63
+ subject.stumble_upon.must_equal body[:StumbleUpon]
64
+ end
65
+ end
66
+
67
+ describe "when the request succeeds" do
68
+ before do
69
+ stub_request(:get, Regexp.new(SharedCountApi::Client::HTTP_ENDPOINT)).
70
+ to_return(body: JSON.generate(body))
71
+ end
72
+
73
+ it "includes the number of StumbleUpon shares" do
74
+ subject.stumble_upon.must_equal body[:StumbleUpon]
75
+ end
76
+
77
+ it "includes the number of Reddit shares" do
78
+ subject.reddit.must_equal body[:Reddit]
79
+ end
80
+
81
+ it "includes the number of Facebook likes" do
82
+ subject.facebook_like_count.must_equal body[:Facebook][:like_count]
83
+ end
84
+
85
+ it "includes the number of Facebook shares" do
86
+ subject.facebook_share_count.must_equal body[:Facebook][:share_count]
87
+ end
88
+
89
+ it "includes the number of Delicious shares" do
90
+ subject.delicious.must_equal body[:Delicious]
91
+ end
92
+
93
+ it "includes the number of Google +1s" do
94
+ subject.google_plus_one.must_equal body[:GooglePlusOne]
95
+ end
96
+
97
+ it "includes the number of Buzz shares" do
98
+ subject.buzz.must_equal body[:Buzz]
99
+ end
100
+
101
+ it "includes the number of Twitter shares" do
102
+ subject.twitter.must_equal body[:Twitter]
103
+ end
104
+
105
+ it "includes the number of Digg shares" do
106
+ subject.diggs.must_equal body[:Diggs]
107
+ end
108
+
109
+ it "includes the number of Pinterest shares" do
110
+ subject.pinterest.must_equal body[:Pinterest]
111
+ end
112
+
113
+ it "includes the number of LinkedIn shares" do
114
+ subject.linked_in.must_equal body[:LinkedIn]
115
+ end
116
+ end
117
+ end
118
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shared_count_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cristian Rasch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.8
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-line
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.16.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.16.0
55
+ description: Thin wrapper around the SharedCount API in vanilla Ruby
56
+ email:
57
+ - cristianrasch@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .ruby-version
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/shared_count_api.rb
69
+ - lib/shared_count_api/version.rb
70
+ - shared_count_api.gemspec
71
+ - spec/shared_count_api_spec.rb
72
+ homepage: https://github.com/wecodeio/shared_count_api
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: This library wraps the SharedCount API exposing its data through POROs (Plain
96
+ Old Ruby Objects)
97
+ test_files:
98
+ - spec/shared_count_api_spec.rb