popularity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +21 -0
  5. data/Gemfile.lock +126 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.rdoc +92 -0
  8. data/Rakefile +54 -0
  9. data/VERSION +1 -0
  10. data/lib/.DS_Store +0 -0
  11. data/lib/popularity.rb +92 -0
  12. data/lib/popularity/crawler.rb +76 -0
  13. data/lib/popularity/facebook.rb +26 -0
  14. data/lib/popularity/github.rb +28 -0
  15. data/lib/popularity/google_plus.rb +22 -0
  16. data/lib/popularity/medium.rb +33 -0
  17. data/lib/popularity/pinterest.rb +21 -0
  18. data/lib/popularity/reddit_comment.rb +32 -0
  19. data/lib/popularity/reddit_post.rb +47 -0
  20. data/lib/popularity/reddit_share.rb +72 -0
  21. data/lib/popularity/results_container.rb +70 -0
  22. data/lib/popularity/search.rb +79 -0
  23. data/lib/popularity/soundcloud.rb +44 -0
  24. data/lib/popularity/twitter.rb +21 -0
  25. data/popularity.gemspec +124 -0
  26. data/spec/cassettes/facebook.yml +50 -0
  27. data/spec/cassettes/github-valid.yml +105 -0
  28. data/spec/cassettes/google_plus.yml +89 -0
  29. data/spec/cassettes/medium-valid.yml +110 -0
  30. data/spec/cassettes/pinterest.yml +44 -0
  31. data/spec/cassettes/reddit-comment.yml +207 -0
  32. data/spec/cassettes/reddit-post.yml +174 -0
  33. data/spec/cassettes/reddit.yml +183 -0
  34. data/spec/cassettes/result-container-test.yml +97 -0
  35. data/spec/cassettes/search-multi.yml +1211 -0
  36. data/spec/cassettes/search.yml +598 -0
  37. data/spec/cassettes/soundcloud-valid.yml +682 -0
  38. data/spec/cassettes/twitter.yml +64 -0
  39. data/spec/cassettes/unknown-reddit-post.yml +68 -0
  40. data/spec/facebook_spec.rb +23 -0
  41. data/spec/github_spec.rb +39 -0
  42. data/spec/google_plus_spec.rb +27 -0
  43. data/spec/medium_spec.rb +47 -0
  44. data/spec/pinterest_spec.rb +27 -0
  45. data/spec/reddit_comment_spec.rb +60 -0
  46. data/spec/reddit_post_spec.rb +64 -0
  47. data/spec/reddit_spec.rb +89 -0
  48. data/spec/results_container_spec.rb +61 -0
  49. data/spec/search_spec.rb +79 -0
  50. data/spec/soundcloud_spec.rb +66 -0
  51. data/spec/spec_helper.rb +95 -0
  52. data/spec/twitter_spec.rb +19 -0
  53. data/test/facebook_test.rb +28 -0
  54. data/test/fixtures/vcr_cassettes/facebook.yml +50 -0
  55. data/test/fixtures/vcr_cassettes/google.yml +425 -0
  56. data/test/helper.rb +34 -0
  57. data/test/test_non_specific.rb +27 -0
  58. data/test/test_twitter.rb +28 -0
  59. metadata +242 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 626966520a15ec4f37c183a2a380b5010f105f85
4
+ data.tar.gz: 00cf045aff8dcdfd8883ff3384ecb7bb253979ed
5
+ SHA512:
6
+ metadata.gz: 437afdbe8bd81499712c32c1df0015a5adea309b8d0f548e8d830505942f855b11dd0ce8208caf0f0acb6066f12f27f8ad849e3a0b40925c7af6dc22c57fe4f1
7
+ data.tar.gz: 302858361aebd23d0565a593ddf94a9b9fa9e5d260b03f4dd1bbd605e2525af5dc439261ff86f2d49076fa7b8d0216a60c4914e100e34c125b19fbf93076fe28
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'open_uri_redirections', '~> 0'
4
+ gem 'json', '~> 1.8'
5
+ gem 'unirest', '~> 1'
6
+
7
+ group :development, :test do
8
+ gem "pry", '~> 0.10'
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0"
13
+ gem "simplecov", "~> 0"
14
+ gem 'awesome_print', '~> 1.6'
15
+ end
16
+
17
+ group :test do
18
+ gem "rspec", "~> 3.2"
19
+ gem "vcr", "~> 2.9"
20
+ gem "webmock", "~> 1.21"
21
+ end
@@ -0,0 +1,126 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (4.2.1)
5
+ i18n (~> 0.7)
6
+ json (~> 1.7, >= 1.7.7)
7
+ minitest (~> 5.1)
8
+ thread_safe (~> 0.3, >= 0.3.4)
9
+ tzinfo (~> 1.1)
10
+ addressable (2.3.8)
11
+ awesome_print (1.6.1)
12
+ builder (3.2.2)
13
+ coderay (1.1.0)
14
+ crack (0.4.2)
15
+ safe_yaml (~> 1.0.0)
16
+ descendants_tracker (0.0.4)
17
+ thread_safe (~> 0.3, >= 0.3.1)
18
+ diff-lcs (1.2.5)
19
+ docile (1.1.5)
20
+ faraday (0.9.1)
21
+ multipart-post (>= 1.2, < 3)
22
+ git (1.2.9.1)
23
+ github_api (0.12.3)
24
+ addressable (~> 2.3)
25
+ descendants_tracker (~> 0.0.4)
26
+ faraday (~> 0.8, < 0.10)
27
+ hashie (>= 3.3)
28
+ multi_json (>= 1.7.5, < 2.0)
29
+ nokogiri (~> 1.6.3)
30
+ oauth2
31
+ hashie (3.4.1)
32
+ highline (1.7.1)
33
+ i18n (0.7.0)
34
+ jeweler (2.0.1)
35
+ builder
36
+ bundler (>= 1.0)
37
+ git (>= 1.2.5)
38
+ github_api
39
+ highline (>= 1.6.15)
40
+ nokogiri (>= 1.5.10)
41
+ rake
42
+ rdoc
43
+ json (1.8.2)
44
+ jwt (1.4.1)
45
+ method_source (0.8.2)
46
+ mime-types (1.25.1)
47
+ mini_portile (0.6.2)
48
+ minitest (5.5.1)
49
+ multi_json (1.11.0)
50
+ multi_xml (0.5.5)
51
+ multipart-post (2.0.0)
52
+ nokogiri (1.6.6.2)
53
+ mini_portile (~> 0.6.0)
54
+ oauth2 (1.0.0)
55
+ faraday (>= 0.8, < 0.10)
56
+ jwt (~> 1.0)
57
+ multi_json (~> 1.3)
58
+ multi_xml (~> 0.5)
59
+ rack (~> 1.2)
60
+ open_uri_redirections (0.2.1)
61
+ pry (0.10.1)
62
+ coderay (~> 1.1.0)
63
+ method_source (~> 0.8.1)
64
+ slop (~> 3.4)
65
+ rack (1.6.0)
66
+ rake (10.4.2)
67
+ rdoc (3.12.2)
68
+ json (~> 1.4)
69
+ rest-client (1.6.8)
70
+ mime-types (~> 1.16)
71
+ rdoc (>= 2.4.2)
72
+ rspec (3.2.0)
73
+ rspec-core (~> 3.2.0)
74
+ rspec-expectations (~> 3.2.0)
75
+ rspec-mocks (~> 3.2.0)
76
+ rspec-core (3.2.3)
77
+ rspec-support (~> 3.2.0)
78
+ rspec-expectations (3.2.1)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.2.0)
81
+ rspec-mocks (3.2.1)
82
+ diff-lcs (>= 1.2.0, < 2.0)
83
+ rspec-support (~> 3.2.0)
84
+ rspec-support (3.2.2)
85
+ safe_yaml (1.0.4)
86
+ shoulda (3.5.0)
87
+ shoulda-context (~> 1.0, >= 1.0.1)
88
+ shoulda-matchers (>= 1.4.1, < 3.0)
89
+ shoulda-context (1.2.1)
90
+ shoulda-matchers (2.8.0)
91
+ activesupport (>= 3.0.0)
92
+ simplecov (0.9.2)
93
+ docile (~> 1.1.0)
94
+ multi_json (~> 1.0)
95
+ simplecov-html (~> 0.9.0)
96
+ simplecov-html (0.9.0)
97
+ slop (3.6.0)
98
+ thread_safe (0.3.5)
99
+ tzinfo (1.2.2)
100
+ thread_safe (~> 0.1)
101
+ unirest (1.1.2)
102
+ addressable (~> 2.3.5)
103
+ json (~> 1.8.1)
104
+ rest-client (~> 1.6.7)
105
+ vcr (2.9.3)
106
+ webmock (1.21.0)
107
+ addressable (>= 2.3.6)
108
+ crack (>= 0.3.2)
109
+
110
+ PLATFORMS
111
+ ruby
112
+
113
+ DEPENDENCIES
114
+ awesome_print (~> 1.6)
115
+ bundler (~> 1.0)
116
+ jeweler (~> 2.0)
117
+ json (~> 1.8)
118
+ open_uri_redirections (~> 0)
119
+ pry (~> 0.10)
120
+ rdoc (~> 3.12)
121
+ rspec (~> 3.2)
122
+ shoulda
123
+ simplecov (~> 0)
124
+ unirest (~> 1)
125
+ vcr (~> 2.9)
126
+ webmock (~> 1.21)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Jeff Keen
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.
@@ -0,0 +1,92 @@
1
+ # Popularity
2
+
3
+ ### Popularity searches social networks for urls and returns the metrics.
4
+
5
+ Current support includes:
6
+
7
+ Network | Stats | Restriction
8
+ --------- | ------ | ---------
9
+ Facebook | `shares`, `comments`
10
+ Twitter | `tweets`
11
+ Pinterest | `pins`
12
+ GooglePlus | `plusones`
13
+ Reddit | `comments`, `score`
14
+ Reddit Comments | `score` | only for reddit comment urls
15
+ Github | `stars` | only for github repo urls
16
+ Medium | `recommends` | only for medium posts
17
+ Soundcloud | `plays`, `likes`, `comments`, `downloads` | only for soundcloud urls
18
+
19
+ ```ruby
20
+
21
+ > search = Popularity.search("http://google.com")
22
+ > search.facebook.shares #=> share count
23
+ > search.facebook.comments #=> comment count
24
+ > search.twitter.tweets #=> tweet count
25
+ > search.pinterests.pins #=> pin count
26
+ > search.reddit.posts #=> post count
27
+ > search.reddit.comments #=> comment count across all posts
28
+ > search.reddit.score #=> reddit score total across all posts
29
+ > search.total #=> All of the above numbers added up
30
+
31
+ ```
32
+
33
+ #### Combining multiple URLs
34
+
35
+ Sometimes multiple URLs refer to the same thing. Maybe your URLs changed, but you don't want to lose out on counting those votes for the new location. Or you tweeted your blog address, but posted a link to soundcloud on reddit.
36
+
37
+ ```ruby
38
+ > search = Popularity.search("http://jeffkeen.me/p/I-know-its-you/", "http://soundcloud.com/jeffkeen/i-know-its-you")
39
+ > search.facebook.shares #=> share count across all urls
40
+ > search.facebook.comments #=> comment count across all urls
41
+ > search.twitter.tweets #=> tweet count across all urls
42
+ > search.pinterests.pins #=> pin count across all urls
43
+ > search.reddit.posts #=> post count across all urls
44
+ > search.reddit.comments #=> comment count across all posts and urls
45
+ > search.reddit.score #=> reddit score total across all posts and urls
46
+ > search.total #=> All of the above numbers added up
47
+ ```
48
+
49
+ JSON:
50
+
51
+ ```ruby
52
+ > search.to_json
53
+ #=> spits out by network, by url, and total stats
54
+ ```
55
+
56
+ ##### Digging into the details
57
+
58
+ To see which results a url came back with:
59
+ ```ruby
60
+ search = Popularity.search("https://medium.com/@jeffkeen/call-me-sometime-64ed463c02f0")
61
+ search.sources #=> [:facebook, :reddit, :google_plus, :pinterest, :twitter, :medium]
62
+ ```
63
+ ```ruby
64
+ search.results #=> returns the individual network result classes, i.e. RedditPost, Medium, Soundcloud, Facebook
65
+ ```
66
+
67
+ All of the searches done through Popularity.search are done asynchronously, but you don't need to worry about any of that because the method still blocks, so you can use it just like any other syncrhonous method.
68
+
69
+ To just query one network, interact with the class directly.
70
+
71
+ ```ruby
72
+ p = Popularity::RedditComment.new("http://www.reddit.com/r/:subreddit/comments/:id/:name/:comment")
73
+ p.score #=> comment score
74
+ ```
75
+
76
+
77
+
78
+ ### Contributing to Popularity
79
+
80
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
81
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
82
+ * Fork the project.
83
+ * Start a feature/bugfix branch.
84
+ * Commit and push until you are happy with your contribution.
85
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
86
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
87
+
88
+ ### Copyright
89
+
90
+ Copyright (c) 2015 Jeff Keen. See LICENSE.txt for
91
+ further details.
92
+
@@ -0,0 +1,54 @@
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://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "popularity"
18
+ gem.homepage = "http://github.com/jkeen/popularity"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Popularity searches social networks for urls and returns the metrics.L}
21
+ gem.description = %Q{}
22
+ gem.email = "jeff@keen.me"
23
+ gem.authors = ["Jeff Keen"]
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
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['test'].execute
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "popularity #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
52
+
53
+
54
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
Binary file
@@ -0,0 +1,92 @@
1
+
2
+ require 'popularity/crawler'
3
+ require 'popularity/search'
4
+ require 'popularity/results_container'
5
+ require 'popularity/facebook'
6
+ require 'popularity/twitter'
7
+ require 'popularity/pinterest'
8
+ require 'popularity/google_plus'
9
+ require 'popularity/medium'
10
+ require 'popularity/reddit_post'
11
+ require 'popularity/reddit_comment'
12
+ require 'popularity/reddit_share'
13
+ require 'popularity/soundcloud'
14
+ require 'popularity/github'
15
+
16
+ module Popularity
17
+ TYPES = [Facebook, Twitter, Pinterest, GooglePlus, RedditShare, RedditPost, RedditComment, Medium, Soundcloud, Github]
18
+
19
+ def self.search(*urls)
20
+ response = {}
21
+
22
+ MultiSearch.new(:urls => urls)
23
+ end
24
+
25
+ def self.select_types(url)
26
+ # github.com stats only valid for github urls, etc
27
+ selected_types = Popularity::TYPES.collect { |n|
28
+ network = n.new(@url)
29
+ network if network.valid?
30
+ }.compact
31
+ end
32
+
33
+ class MultiSearch
34
+ attr_accessor :searches
35
+ attr_accessor :sources
36
+
37
+ def initialize(options = {})
38
+ @searches = options[:urls].collect { |url| Search.new(url) }
39
+
40
+ @searches.each do |search|
41
+ search.results.each do |result|
42
+ add_search_result(result)
43
+ end
44
+ end
45
+ end
46
+
47
+ def results
48
+ searches.collect(&:results).reduce(:+)
49
+ end
50
+
51
+ def to_json(options = {})
52
+ json = {}
53
+ self.searches.collect do |search|
54
+ json[search.url] = search.to_json
55
+ end
56
+
57
+ self.sources.collect do |source|
58
+ json[source.to_s] = self.send(source.to_sym).to_json
59
+ end
60
+
61
+ json["total"] = total
62
+
63
+ json
64
+ end
65
+
66
+ def total
67
+ total = 0
68
+ self.searches.each do |a|
69
+ total += a.total
70
+ end
71
+
72
+ total
73
+ end
74
+
75
+ protected
76
+
77
+ def add_search_result(result)
78
+ container = self.instance_variable_get("@#{result.name}")
79
+
80
+ unless container
81
+ @sources ||= []
82
+ @sources << result.name.to_sym
83
+ container = Popularity::ResultsContainer.new
84
+ self.instance_variable_set "@#{result.name}", container
85
+ self.define_singleton_method(result.name.to_sym) { container }
86
+ end
87
+
88
+ container.add_result(result)
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,76 @@
1
+ require 'open-uri'
2
+ require 'open_uri_redirections'
3
+ require 'json'
4
+ require 'unirest'
5
+
6
+ module Popularity
7
+ class Crawler
8
+ attr_reader :url
9
+
10
+ def initialize(url)
11
+ @url = url
12
+ end
13
+
14
+ def request_url
15
+ raise NotImplemented
16
+ end
17
+
18
+ def response
19
+ @response ||= fetch
20
+ end
21
+
22
+ def has_response?
23
+ response #fetch it
24
+
25
+ return false if response.nil?
26
+ return false if response.empty?
27
+
28
+ true
29
+ end
30
+
31
+ def valid?
32
+ true # to be overridden in subclasses
33
+ end
34
+
35
+ def async_done?
36
+ @async_done
37
+ end
38
+
39
+ def host
40
+ URI.parse(@url).host.gsub('www.', '')
41
+ end
42
+
43
+ def response_json
44
+ @json ||= JSON.parse(response)
45
+ end
46
+
47
+ def name
48
+ self.class.to_s.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase
49
+ end
50
+
51
+ def to_json(options = {})
52
+ as_json(options)
53
+ end
54
+
55
+ def fetch_async(&block)
56
+ return false unless valid?
57
+
58
+ Unirest.get(request_url) do |response|
59
+ @async_done = true
60
+ @response = response.raw_body
61
+ block.call(response.code, response.raw_body) if block_given?
62
+ end
63
+ end
64
+
65
+ def fetch
66
+ return false unless valid?
67
+
68
+ begin
69
+ response = Unirest.get(request_url)
70
+ @response = response.raw_body
71
+ rescue OpenURI::HTTPError, Timeout::Error, SocketError
72
+ nil
73
+ end
74
+ end
75
+ end
76
+ end