virility 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,3 @@
1
+ --color
2
+ --format documentation
3
+ --profile
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@virility --create
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+
5
+ # Specify your gem's dependencies in virility.gemspec
6
+ gemspec
@@ -0,0 +1,29 @@
1
+ Copyright (c) 2012 Jay Sanders
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.
23
+
24
+
25
+ THE BEER-WARE LICENSE (Revision 42):
26
+
27
+ <mindtonic@gmail.com> wrote this gem. As long as you retain this notice you
28
+ can do whatever you want with this stuff. If we meet some day, and you think
29
+ this stuff is worth it, you can buy me a beer in return. ~ Jay Sanders
@@ -0,0 +1,45 @@
1
+ # Virility
2
+
3
+ Virility calls upon the API's of many popular social services such as Facebook, Twitter and Pinterest to collect the number of likes, tweets, pins etc. of a particular URL. Written with a modular construction, Virility makes it easy to drop new data collection strategies into the framework so that you can collect all of your statistics in one easy location.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'virility'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install virility
18
+
19
+ ## Usage
20
+
21
+ stats = Virility::Excitation.new(the_url_you_want_to_collect_data_for)
22
+ stats.get_virility
23
+
24
+ Currently there is support for the following social resources:
25
+ * Facebook
26
+ * Twitter
27
+ * Delicious
28
+ * Pinterest
29
+ * Google Plus One
30
+ * Stumble Upon
31
+
32
+ More detailed information coming soon.
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2012 Jay Sanders. See LICENSE.txt for
45
+ further details.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require 'cgi'
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ require "virility/version"
6
+ require 'virility/excitation'
7
+ require 'virility/context'
8
+ Dir["#{File.dirname(__FILE__)}/virility/strategies/**/*.rb"].each {|f| require f}
9
+
10
+ module Virility
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,14 @@
1
+ module Virility
2
+ class Context
3
+ include HTTParty
4
+ attr_accessor :url, :response, :counts
5
+
6
+ def initialize url
7
+ @url = url
8
+ end
9
+
10
+ def get_virility
11
+ raise "Abstract Method get_virility called on #{self.class} - Please define this method"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,92 @@
1
+ module Virility
2
+ class Excitation
3
+ attr_accessor :url, :results, :strategies
4
+
5
+ #
6
+ # Initialization
7
+ #
8
+
9
+ def initialize url
10
+ @url = encode url
11
+ @strategies = {}
12
+ @results = {}
13
+ collect_strategies
14
+ end
15
+
16
+ #
17
+ # Get Virility from all of the Strategies
18
+ #
19
+
20
+ def get_virility
21
+ @strategies.each do |name, strategy|
22
+ begin
23
+ @results[symbolize_for_key(strategy)] = strategy.get_virility
24
+ rescue => e
25
+ puts "[virility] #{strategy.class.to_s} => #{e}"
26
+ end
27
+ end
28
+ @results
29
+ end
30
+
31
+ def get_response(strategy)
32
+ @strategies[strategy].response if @strategies[strategy]
33
+ end
34
+
35
+ #
36
+ # Gather all of the Strategies
37
+ #
38
+
39
+ def collect_strategies
40
+ Dir["#{File.dirname(__FILE__)}/strategies/**/*.rb"].each { |klass| @strategies[get_class_string(klass).to_sym] = Virility.const_get(camelize(get_class_string(klass))).new(@url) }
41
+ end
42
+
43
+ #
44
+ # URL Encoding / Decoding Methods
45
+ #
46
+
47
+ def encode url
48
+ CGI.escape url
49
+ end
50
+
51
+ def url
52
+ CGI.unescape @url
53
+ end
54
+
55
+ def escaped_url
56
+ @url
57
+ end
58
+
59
+ #
60
+ # Camelize
61
+ #
62
+
63
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
64
+ if first_letter_in_uppercase
65
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
66
+ else
67
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
68
+ end
69
+ end
70
+
71
+ #
72
+ # Convert Class Name To Appropriate Key Symbol
73
+ #
74
+
75
+ def symbolize_for_key(klass)
76
+ klass.class.to_s.gsub(/Virility::/, '').downcase.to_sym
77
+ end
78
+
79
+ def get_class_string(klass)
80
+ File.basename(klass).gsub(/\.rb/,'')
81
+ end
82
+
83
+ #
84
+ # Reflection
85
+ #
86
+
87
+ def attributes
88
+ {:url => @url}
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,10 @@
1
+ module Virility
2
+ class Delicious < Context
3
+
4
+ def get_virility
5
+ @response = self.class.get("http://feeds.delicious.com/v2/json/urlinfo/data?url=#{@url}")
6
+ @counts = @response.parsed_response.first
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Virility
2
+ class Facebook < Context
3
+ BASE_URL = "https://api.facebook.com/method/fql.query?query=SELECT+url%2C+normalized_url%2C+share_count%2C+like_count%2C+comment_count%2C+total_count%2C+commentsbox_count%2C+comments_fbid%2C+click_count+FROM+link_stat+WHERE+url%3D"
4
+
5
+ def get_virility
6
+ @response = self.class.get("#{BASE_URL}%22#{@url}%22")
7
+ @counts = @response.parsed_response["fql_query_response"]["link_stat"]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Virility
2
+ class Pinterest < Context
3
+
4
+ parser(
5
+ Proc.new do |body, format|
6
+ MultiJson.decode(body.scan(/({.+})/).flatten.first)
7
+ end
8
+ )
9
+
10
+ def get_virility
11
+ @response = self.class.get("http://api.pinterest.com/v1/urls/count.json?url=#{@url}")
12
+ @counts = @response.parsed_response
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # http://stackoverflow.com/questions/7403553/how-do-i-get-the-counter-of-a-google-plus-1-button
2
+ module Virility
3
+ class PlusOne < Context
4
+
5
+ parser(
6
+ Proc.new do |body, format|
7
+ {'shares' => body.scan(/c: (\d+)/).flatten.first}
8
+ end
9
+ )
10
+
11
+ def get_virility
12
+ @response = self.class.get("https://plusone.google.com/_/+1/fastbutton?url=#{@url}")
13
+ @counts = @response.parsed_response
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Virility
2
+ class StumbleUpon < Context
3
+
4
+ parser(
5
+ Proc.new do |body, format|
6
+ MultiJson.decode(body)
7
+ end
8
+ )
9
+
10
+ def get_virility
11
+ @response = self.class.get("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=#{@url}")
12
+ @counts = @response.parsed_response
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module Virility
2
+ class Twitter < Context
3
+
4
+ def get_virility
5
+ @response = self.class.get("http://urls.api.twitter.com/1/urls/count.json?url=#{@url}")
6
+ @counts = @response.parsed_response
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Virility
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Context" do
4
+ before(:each) do
5
+ @url = "http://creativeallies.com"
6
+ end
7
+
8
+ #
9
+ # Initialization
10
+ #
11
+
12
+ context "initialization" do
13
+ it "should raise an error if a URL is not set" do
14
+ lambda {Virility::Context.new}.should raise_error
15
+ end
16
+
17
+ it "should set the url" do
18
+ Virility::Excitation.new(@url).url.should == @url
19
+ end
20
+ end
21
+
22
+ #
23
+ # Interface
24
+ #
25
+
26
+ context "interface" do
27
+ it "should raise an error on get_virility" do
28
+ lambda { Virility::Context.new(@url).get_virility }.should raise_error
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Excitation" do
4
+ before(:each) do
5
+ @url = "http://www.dzone.com/snippets/uri-encoding-ruby"
6
+ end
7
+
8
+ #
9
+ # Initialization
10
+ #
11
+
12
+ context "initialization" do
13
+ it "should raise an error if a URL is not set" do
14
+ lambda {Virility::Excitation.new}.should raise_error
15
+ end
16
+
17
+ it "should automatically encode the url" do
18
+ Virility::Excitation.new(@url).attributes[:url].should == "http%3A%2F%2Fwww.dzone.com%2Fsnippets%2Furi-encoding-ruby"
19
+ end
20
+ end
21
+
22
+ #
23
+ # Get Virility
24
+ #
25
+
26
+ # context "get_virility" do
27
+ # it "should not raise an error" do
28
+ # lambda {Virility::Excitation.new(@url).get_virility}.should_not raise_error
29
+ # end
30
+ # end
31
+
32
+ #
33
+ # Collect Strategies
34
+ #
35
+
36
+ context "collect_strategies" do
37
+ it "should assign a hash to the strategies variable" do
38
+ Virility::Excitation.new(@url).strategies.should be_a_kind_of Hash
39
+ end
40
+
41
+ it "strategies should be inherited from the Context" do
42
+ Virility::Excitation.new(@url).strategies.first.last.should be_a_kind_of Virility::Context
43
+ end
44
+
45
+ it "should load all of the strategies" do
46
+ Virility::Excitation.new(@url).strategies.count.should == Dir[File.join('lib', 'virility', 'strategies', '**', '*')].count { |file| File.file?(file) }
47
+ end
48
+ end
49
+
50
+ #
51
+ # Encode
52
+ #
53
+
54
+ context "encode" do
55
+ it "should encode the url" do
56
+ v = Virility::Excitation.new(@url)
57
+ v.encode(@url).should == "http%3A%2F%2Fwww.dzone.com%2Fsnippets%2Furi-encoding-ruby"
58
+ end
59
+ end
60
+
61
+ #
62
+ # URL
63
+ #
64
+
65
+ context "url" do
66
+ it "should return the unencoded url" do
67
+ Virility::Excitation.new(@url).url.should == @url
68
+ end
69
+ end
70
+
71
+ #
72
+ # Escaped URL
73
+ #
74
+
75
+ context "escaped_url" do
76
+ it "should return the encoded url" do
77
+ Virility::Excitation.new(@url).escaped_url.should == "http%3A%2F%2Fwww.dzone.com%2Fsnippets%2Furi-encoding-ruby"
78
+ end
79
+ end
80
+
81
+ #
82
+ # Symbolize For Key
83
+ #
84
+
85
+ context "symbolize_for_key" do
86
+ it "should return a symbol with the name of the class" do
87
+ Virility::Excitation.new(@url).symbolize_for_key(Virility::Excitation.new(@url)).should == :excitation
88
+ end
89
+ end
90
+
91
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'virility'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ config.filter_run :focus => true
12
+ config.run_all_when_everything_filtered = true
13
+ config.mock_with :rspec
14
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Virility" do
4
+ it "should test the Virility API from beginning to end"
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'virility/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ # Details
8
+ gem.name = "virility"
9
+ gem.version = Virility::VERSION
10
+ gem.authors = ["Jay Sanders"]
11
+ gem.email = ["mindtonic@gmail.com"]
12
+ gem.description = "Virility leverages the API's of many popular social services to collect data about the virility of a particular URL."
13
+ gem.summary = "Virility calls upon the API's of many popular social services such as Facebook, Twitter and Pinterest to collect the number of likes, tweets and pins of a particular URL. Written with a modular construction, Virility makes it easy to drop new data collection strategies into the framework so that you can collect all of your statistics in one easy location."
14
+ gem.homepage = ""
15
+ # Files
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ # Development
21
+ gem.add_development_dependency "rspec", "~> 2.6"
22
+ # Dependencies
23
+ gem.add_dependency "httparty", "~> 0.9.0"
24
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virility
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jay Sanders
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
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: '2.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.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.9.0
46
+ description: Virility leverages the API's of many popular social services to collect
47
+ data about the virility of a particular URL.
48
+ email:
49
+ - mindtonic@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .rvmrc
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - lib/virility.rb
62
+ - lib/virility/context.rb
63
+ - lib/virility/excitation.rb
64
+ - lib/virility/strategies/delicious.rb
65
+ - lib/virility/strategies/facebook.rb
66
+ - lib/virility/strategies/pinterest.rb
67
+ - lib/virility/strategies/plus_one.rb
68
+ - lib/virility/strategies/stumble_upon.rb
69
+ - lib/virility/strategies/twitter.rb
70
+ - lib/virility/version.rb
71
+ - spec/context_spec.rb
72
+ - spec/excitation_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/virility_spec.rb
75
+ - virility.gemspec
76
+ homepage: ''
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: 2283656730351254401
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: 2283656730351254401
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Virility calls upon the API's of many popular social services such as Facebook,
106
+ Twitter and Pinterest to collect the number of likes, tweets and pins of a particular
107
+ URL. Written with a modular construction, Virility makes it easy to drop new data
108
+ collection strategies into the framework so that you can collect all of your statistics
109
+ in one easy location.
110
+ test_files:
111
+ - spec/context_spec.rb
112
+ - spec/excitation_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spec/virility_spec.rb