social_shares 0.2.7 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9eab70ae26105c23d80e83cd6a724440fe9a47d
4
- data.tar.gz: 96f8dcb9dbcbc5c4ce1705df0fdc3eb1b9a4f77d
3
+ metadata.gz: 3642b7fd39221e877acaa31168a1cf7e87f9868b
4
+ data.tar.gz: fb6d78c291949dbff0fc77c9ee2536e8914ce6bf
5
5
  SHA512:
6
- metadata.gz: 235d154c6b9fc6718e718451fea185d49d5d49fb4a4f04c8bac506936055eceb3c7179a18286054982b779144434bfeff4e05a7a5a705e31e4be2a8134e9f99a
7
- data.tar.gz: 7cedea104f5bfe46e8be93c7a81a5060befeacb225b276aa416e67a512a44e90a22fe026ace435170714bc249c4774b1610af668b170d5db12dcd1d8de1ab685
6
+ metadata.gz: d5c6d41eda0fe528a92a42c972fedde5b88235dcf9e0f767c88a499f964d2e3c554a1295a94bebd5aa8a85cf3a4a5732c38ca6eeb38f317126f386d8ce26d917
7
+ data.tar.gz: 20ac77025ed29fee54a1368d05f3c6f8ac35290c9f3e8ca379b929cb34fd69d768615c68538487ababd6adcc24e2ba820a841bfc8e16b8a69e1837d57c298845
data/README.md CHANGED
@@ -23,7 +23,6 @@ Russian:
23
23
  * [vkontakte](http://vkontakte.ru/)
24
24
  * [mail.ru(aka moi mir)](http://my.mail.ru/)
25
25
  * [odnoklassniki](http://www.odnoklassniki.ru/)
26
- * [yandex](http://yandex.ru/) (currently on beta, most of time returns 0)
27
26
 
28
27
  Chinese:
29
28
  * [weibo](http://www.weibo.com)
@@ -67,7 +66,7 @@ Fetch all shares by one method (#all, #all!):
67
66
  ```ruby
68
67
  # in case of exception it will return nil
69
68
  :000 > SocialShares.all url
70
- => {:vkontakte=>nil, :facebook=>399027, :google=>28346, :twitter=>1836, :mail_ru=>37, :odnoklassniki=>1, :reddit=>2361, :linkedin=>33, :pinterest=>21011, :stumbleupon=>43035, :weibo=>12760, :buffer=>1662, :yandex=>0}
69
+ => {:vkontakte=>nil, :facebook=>399027, :google=>28346, :twitter=>1836, :mail_ru=>37, :odnoklassniki=>1, :reddit=>2361, :linkedin=>33, :pinterest=>21011, :stumbleupon=>43035, :weibo=>12760, :buffer=>1662}
71
70
 
72
71
  # and this will raise it
73
72
  :000 > SocialShares.all! url
@@ -112,7 +111,33 @@ Does any network have at least one link?
112
111
  ```
113
112
  Note that #has_any? is faster than (#total > 0), because it stops on first network that has at least 1 sharing
114
113
 
115
- Instalation
114
+ Configuring
115
+ -----
116
+ You can specify timeout and open_timeout for each social network
117
+ ```ruby
118
+ SocialShares.config = {
119
+ twitter: {timeout: 4, open_timeout: 7},
120
+ facebook: {timeout: 10, open_timeout: 15}
121
+ }
122
+ ```
123
+
124
+ - `:open_timeout` is the timeout for opening the connection. This is useful if you are calling servers with slow or shaky response times. Default value is 3 seconds.
125
+ - `:timeout` is the timeout for reading the answer. This is useful to make sure you will not get stuck half way in the reading process, or get stuck reading a 5 MB file when you're expecting 5 KB of JSON. Default value is 3 seconds.
126
+
127
+ If you use Rails, you can create file `config/initializers/social_shares.rb` and fill it with this config.
128
+
129
+ Try it by yourself before installation
130
+ -----
131
+ Send request through shell to test numbers. Please do NOT use this url in your projects.
132
+ ```bash
133
+ curl -X POST -d '{"url": "http://www.apple.com", "networks": ["facebook", "google", "reddit"]}' https://social-shares-api-cedar-14.herokuapp.com/
134
+ ```
135
+ You will see:
136
+ ```bash
137
+ {"facebook":312412,"google":46088,"reddit":114}
138
+ ```
139
+
140
+ Installation
116
141
  -----
117
142
  Include the gem in your Gemfile:
118
143
  ```
data/lib/social_shares.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
+ require 'social_shares/string_helper'
3
4
  require 'social_shares/version'
4
5
  require 'social_shares/base'
5
6
  require 'social_shares/facebook'
@@ -14,15 +15,20 @@ require 'social_shares/pinterest'
14
15
  require 'social_shares/stumbleupon'
15
16
  require 'social_shares/weibo'
16
17
  require 'social_shares/buffer'
17
- require 'social_shares/yandex'
18
18
 
19
19
  module SocialShares
20
20
  class << self
21
+ include SocialShares::StringHelper
22
+
23
+ def config=(val)
24
+ SocialShares::Base.config = val
25
+ end
26
+
21
27
  SUPPORTED_NETWORKS = [
22
28
  :vkontakte,
23
29
  :facebook,
24
30
  :google,
25
- :twitter,
31
+ # :twitter,
26
32
  :mail_ru,
27
33
  :odnoklassniki,
28
34
  :reddit,
@@ -31,7 +37,6 @@ module SocialShares
31
37
  :stumbleupon,
32
38
  :weibo,
33
39
  :buffer,
34
- :yandex,
35
40
  ]
36
41
 
37
42
  def supported_networks
@@ -40,12 +45,12 @@ module SocialShares
40
45
 
41
46
  SUPPORTED_NETWORKS.each do |network_name|
42
47
  define_method(network_name) do |url|
43
- class_name = network_name.to_s.split('_').map(&:capitalize).join
48
+ class_name = to_camel_case(network_name.to_s)
44
49
  SocialShares.const_get(class_name).new(url).shares
45
50
  end
46
51
 
47
52
  define_method("#{network_name}!") do |url|
48
- class_name = network_name.to_s.split('_').map(&:capitalize).join
53
+ class_name = to_camel_case(network_name.to_s)
49
54
  SocialShares.const_get(class_name).new(url).shares!
50
55
  end
51
56
  end
@@ -1,6 +1,17 @@
1
1
  module SocialShares
2
2
  class Base
3
- TIMEOUT = OPEN_TIMEOUT = 3
3
+ include SocialShares::StringHelper
4
+ DEFAULT_TIMEOUT = 3
5
+ DEFAULT_OPEN_TIMEOUT = 3
6
+
7
+ @@config = {}
8
+
9
+ class << self
10
+ def config=(val)
11
+ @@config = val
12
+ end
13
+ end
14
+
4
15
  attr_accessor :checked_url
5
16
 
6
17
  def initialize(checked_url)
@@ -21,12 +32,24 @@ module SocialShares
21
32
 
22
33
  protected
23
34
 
35
+ def config_name
36
+ to_underscore(self.class.name.split('::').last).to_sym
37
+ end
38
+
39
+ def timeout
40
+ (@@config[config_name] || {})[:timeout] || DEFAULT_TIMEOUT
41
+ end
42
+
43
+ def open_timeout
44
+ (@@config[config_name] || {})[:open_timeout] || DEFAULT_OPEN_TIMEOUT
45
+ end
46
+
24
47
  def get(url, params)
25
- RestClient::Resource.new(url, timeout: TIMEOUT, open_timeout: OPEN_TIMEOUT).get(params)
48
+ RestClient::Resource.new(url, timeout: timeout, open_timeout: open_timeout).get(params)
26
49
  end
27
50
 
28
51
  def post(url, params, headers = {})
29
- RestClient::Resource.new(url, timeout: TIMEOUT, open_timeout: OPEN_TIMEOUT).post(params, headers)
52
+ RestClient::Resource.new(url, timeout: timeout, open_timeout: open_timeout).post(params, headers)
30
53
  end
31
54
  end
32
55
  end
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'https://api.bufferapp.com/1/links/shares.json'
4
4
 
5
5
  def shares!
6
- response = get(URL, {:params => {:url => checked_url}})
6
+ response = get(URL, params: { url: checked_url })
7
+
7
8
  JSON.parse(response)['shares']
8
9
  end
9
10
  end
@@ -4,10 +4,11 @@ module SocialShares
4
4
 
5
5
  def shares!
6
6
  response = get(URL, {
7
- :params => {
8
- :q => "SELECT share_count FROM link_stat WHERE url='#{checked_url}'"
7
+ params: {
8
+ q: "SELECT share_count FROM link_stat WHERE url='#{checked_url}'"
9
9
  }
10
10
  })
11
+
11
12
  JSON.parse(response)['data'][0]['share_count'] || 0
12
13
  end
13
14
  end
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ'
4
4
 
5
5
  def shares!
6
- response = post(URL, JSON.dump(params), {content_type: :json, accept: :json})
6
+ response = post(URL, JSON.dump(params), { content_type: :json, accept: :json })
7
+
7
8
  JSON.parse(response)[0]['result']['metadata']['globalCounts']['count'].to_i
8
9
  end
9
10
 
@@ -4,11 +4,12 @@ module SocialShares
4
4
 
5
5
  def shares!
6
6
  response = get(URL, {
7
- :params => {
8
- :url => checked_url,
9
- :format => 'json'
10
- }
11
- })
7
+ params: {
8
+ url: checked_url,
9
+ format: 'json'
10
+ }
11
+ })
12
+
12
13
  JSON.parse(response)['count']
13
14
  end
14
15
  end
@@ -10,6 +10,7 @@ module SocialShares
10
10
  url_list: checked_url
11
11
  }
12
12
  })
13
+
13
14
  matches = /shares":(\d+)/.match(response)
14
15
  matches ? matches[-1].to_i : 0
15
16
  end
@@ -10,6 +10,7 @@ module SocialShares
10
10
  ref: checked_url
11
11
  }
12
12
  })
13
+
13
14
  /'(\d+)'\)/.match(response)[-1].to_i
14
15
  end
15
16
  end
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'http://api.pinterest.com/v1/urls/count.json'
4
4
 
5
5
  def shares!
6
- response = get(URL, {:params => {:url => checked_url}})
6
+ response = get(URL, params: { url: checked_url })
7
+
7
8
  /count":(\d+)/.match(response)[-1].to_i
8
9
  end
9
10
  end
@@ -3,9 +3,10 @@ module SocialShares
3
3
  URL = 'http://www.reddit.com/api/info.json'
4
4
 
5
5
  def shares!
6
- response = get(URL, {:params => {:url => checked_url}})
6
+ response = get(URL, params: { url: checked_url })
7
+
7
8
  children_data = JSON.parse(response)['data']['children']
8
- children_data.map{|c| c['data']['score']}.reduce(:+) || 0
9
+ children_data.map { |c| c['data']['score'] }.reduce(:+) || 0
9
10
  end
10
11
  end
11
12
  end
@@ -0,0 +1,11 @@
1
+ module SocialShares
2
+ module StringHelper
3
+ def to_underscore(str)
4
+ str.dup.tap { |s| (s.gsub!(/(.)([A-Z])/,'\1_\2') || s).downcase! }
5
+ end
6
+
7
+ def to_camel_case(str)
8
+ str.split('_').map(&:capitalize).join
9
+ end
10
+ end
11
+ end
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'http://www.stumbleupon.com/services/1.01/badge.getinfo'
4
4
 
5
5
  def shares!
6
- response = get(URL, {:params => {:url => checked_url}})
6
+ response = get(URL, params: { url: checked_url })
7
+
7
8
  JSON.parse(response)['result']['views'] || 0
8
9
  end
9
10
  end
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'http://cdn.api.twitter.com/1/urls/count.json'
4
4
 
5
5
  def shares!
6
- response = get(URL, {:params => {:url => checked_url}})
6
+ response = get(URL, params: { url: checked_url })
7
+
7
8
  JSON.parse(response)['count']
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module SocialShares
2
- VERSION = '0.2.7'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -4,12 +4,13 @@ module SocialShares
4
4
 
5
5
  def shares!
6
6
  response = get(URL, {
7
- :params => {
8
- :act => 'count',
9
- :index => 1,
10
- :url => checked_url,
11
- }
12
- })
7
+ params: {
8
+ act: 'count',
9
+ index: 1,
10
+ url: checked_url
11
+ }
12
+ })
13
+
13
14
  /VK.Share.count\(1,\s(\d+)\)/.match(response)[1].to_i
14
15
  end
15
16
  end
@@ -5,12 +5,13 @@ module SocialShares
5
5
 
6
6
  def shares!
7
7
  response = get(URL_FOR_SHARE, {
8
- :params => {
9
- :_ => '1414437609900',
10
- :source => '8003029170',
11
- :url_short => short_url
12
- }
13
- })
8
+ params: {
9
+ _: '1414437609900',
10
+ source: '8003029170',
11
+ url_short: short_url
12
+ }
13
+ })
14
+
14
15
  JSON.parse(response)['urls'].first['share_counts'].to_i
15
16
  end
16
17
 
@@ -3,7 +3,8 @@ module SocialShares
3
3
  URL = 'http://wow.ya.ru/ajax/share-counter.xml'
4
4
 
5
5
  def shares!
6
- response = RestClient.get(URL, {params: {url: checked_url}})
6
+ response = RestClient.get(URL, params: { url: checked_url })
7
+
7
8
  /(\d+)/.match(response)[-1].to_i
8
9
  end
9
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: social_shares
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Kozmenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-04 00:00:00.000000000 Z
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -102,6 +102,7 @@ files:
102
102
  - lib/social_shares/odnoklassniki.rb
103
103
  - lib/social_shares/pinterest.rb
104
104
  - lib/social_shares/reddit.rb
105
+ - lib/social_shares/string_helper.rb
105
106
  - lib/social_shares/stumbleupon.rb
106
107
  - lib/social_shares/twitter.rb
107
108
  - lib/social_shares/version.rb