sharer 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -4,4 +4,8 @@
4
4
 
5
5
  ## v0.0.3
6
6
 
7
- * change to object
7
+ * change to object
8
+
9
+ ## v0.0.8
10
+
11
+ * removed digg.com
data/README.md CHANGED
@@ -15,14 +15,14 @@ Sharer's Git repo is available on GitHub, which can be browsed at:
15
15
 
16
16
  ```ruby
17
17
  http://github.com/14113/sharer
18
- ```
18
+ ```
19
19
 
20
20
  and cloned with:
21
21
 
22
22
  ```ruby
23
23
  git clone git://github.com/14113/sharer.git
24
- ```
25
-
24
+ ```
25
+
26
26
  ## Usage
27
27
 
28
28
  Call Sharer in an Model and pass the url of the website.
@@ -30,6 +30,7 @@ Call Sharer in an Model and pass the url of the website.
30
30
  ```ruby
31
31
  site = Sharer::Site.new("google.com")
32
32
  number_of_likes = site.facebook_likes
33
+ number_of_shares = site.facebook_shares
33
34
  number_of_tweets_buttons = site.twitter_button
34
35
  number_of_linkedin_shares = site.linked_in_share
35
36
 
data/lib/sharer.rb CHANGED
@@ -4,16 +4,25 @@ require "net/https"
4
4
  require 'xmlrpc/client'
5
5
  require 'rexml/document'
6
6
 
7
+ # == Sharer module is gem that detects the number of sharing sites
8
+ #
9
+ # A minimal implementation could be:
10
+ # site = Sharer::Site.new("google.com")
11
+ # @result = site.find_all
12
+ #
13
+ # This also provides the methods
14
+ # like +facebook_likes+ or +twitter_shares+
15
+ # and another...
7
16
  module Sharer
8
-
17
+
9
18
  class Site
10
-
19
+
11
20
  def initialize url
12
21
  @url = url
13
22
  add_url_protocol
14
- end
15
-
16
- # Find the number of likes by url
23
+ end
24
+
25
+ # Find the number of facebook likes
17
26
  def facebook_likes
18
27
  return nil unless valid_url?
19
28
  path = "http://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22#{@url}%22"
@@ -21,69 +30,48 @@ module Sharer
21
30
  root = REXML::Document.new(content)
22
31
  root.elements[1].elements["link_stat/like_count"].text.to_i
23
32
  end
24
-
25
- # Find the number of shares
33
+
34
+ # Find the number of facebook shares
26
35
  def facebook_shares
27
36
  return nil unless valid_url?
28
37
  path = "http://graph.facebook.com/?id=#{@url}"
29
38
  content = content(path)
30
39
  content(path).match(/\"shares\":([0-9]*),/)[1].to_i
31
40
  end
32
-
33
- # Find the number of tweeter buttons by url
41
+
42
+ # Find the number of tweeter buttons
34
43
  def twitter_button
35
44
  return nil unless valid_url?
36
45
  path = "http://urls.api.twitter.com/1/urls/count.json?url=#{@url}"
37
46
  content(path).match(/\"count\":([0-9]*),/)[1].to_i
38
47
  end
39
-
40
- # Find the number of linkedin shares by url
48
+
49
+ # Find the number of linkedin shares
41
50
  def linked_in_share
42
51
  return nil unless valid_url?
43
52
  path = "http://www.linkedin.com/countserv/count/share?url=#{@url}&callback=myCallback&format=json"
44
53
  content(path).match(/\"count\":([0-9]*),/)[1].to_i
45
54
  end
46
-
47
- # Find the number of diggs
48
- def diggs
49
- return nil unless valid_url?
50
- url = @url.gsub("http://www.","")
51
- path = "http://widgets.digg.com/buttons/count?url=#{url}"
52
- content(path).match(/\"diggs\": ([0-9]*),/)[1].to_i
53
- end
54
-
55
- # Find shareing by url
55
+
56
+ # Returns the hash with all sites.
57
+ # Method using threads
56
58
  def find_all
57
59
  return nil unless valid_url?
58
-
60
+
59
61
  data = {}
60
62
  threads = []
61
-
62
- threads << Thread.new do
63
- data["facebook_likes"] = facebook_likes
64
- end
65
-
66
- threads << Thread.new do
67
- data["facebook_shares"] = facebook_shares
68
- end
69
-
70
- threads << Thread.new do
71
- data["twitter_tweets"] = twitter_button
72
- end
73
-
74
- threads << Thread.new do
75
- data["linked_in_shared"] = linked_in_share
76
- end
77
-
78
- threads << Thread.new do
79
- data["diggs"] = diggs
63
+ #TODO: use object!
64
+ [:facebook_likes,:facebook_shares,:twitter_button,:linked_in_share].each do |method|
65
+ threads << Thread.new do
66
+ data[method] = send(method)
67
+ end
80
68
  end
81
69
 
82
70
  threads.each {|t| t.join }
83
71
  data
84
72
  end
85
-
86
-
73
+
74
+
87
75
  private
88
76
  # Valid url
89
77
  def valid_url?
@@ -91,7 +79,7 @@ module Sharer
91
79
  #(@url =~ URI::regexp).nil?
92
80
  true
93
81
  end
94
-
82
+
95
83
  # Content by url
96
84
  def content path
97
85
  begin
@@ -100,8 +88,10 @@ module Sharer
100
88
  print exception.backtrace.join("\n")
101
89
  end
102
90
  end
103
-
91
+
92
+ # Edit url
104
93
  def add_url_protocol
94
+ #TODO: use better regex
105
95
  unless @url[/^www./]
106
96
  @url = 'www.' + @url
107
97
  end
@@ -109,6 +99,6 @@ module Sharer
109
99
  @url = 'http://' + @url
110
100
  end
111
101
  end
112
-
102
+
113
103
  end
114
104
  end
@@ -1,3 +1,3 @@
1
1
  module Sharer
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sharer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-28 00:00:00.000000000Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70128927696320 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70128927696320
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &70128927695840 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70128927695840
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  description: If your application needs to count the number of shared, this gem can
37
47
  help you.
38
48
  email:
@@ -68,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
78
  version: '0'
69
79
  segments:
70
80
  - 0
71
- hash: 2065689228042247499
81
+ hash: 3641520689904256952
72
82
  required_rubygems_version: !ruby/object:Gem::Requirement
73
83
  none: false
74
84
  requirements:
@@ -77,10 +87,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
87
  version: '0'
78
88
  segments:
79
89
  - 0
80
- hash: 2065689228042247499
90
+ hash: 3641520689904256952
81
91
  requirements: []
82
92
  rubyforge_project: sharer
83
- rubygems_version: 1.8.10
93
+ rubygems_version: 1.8.25
84
94
  signing_key:
85
95
  specification_version: 3
86
96
  summary: ''