twitter-cache 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -24
- data/Rakefile +5 -0
- data/lib/twitter/cache/config.rb +20 -0
- data/lib/twitter/cache/helpers.rb +13 -0
- data/lib/twitter/cache/redis.rb +36 -0
- data/lib/twitter/cache/version.rb +5 -0
- data/lib/twitter/cache/wrapper.rb +67 -0
- data/lib/twitter/cache.rb +22 -0
- data/lib/twitter-cache.rb +1 -1
- data/twitter-cache.gemspec +4 -4
- metadata +8 -50
- data/lib/twitter_cache/client.rb +0 -82
- data/lib/twitter_cache/config.rb +0 -14
- data/lib/twitter_cache/helpers.rb +0 -11
- data/lib/twitter_cache/redis.rb +0 -34
- data/lib/twitter_cache/version.rb +0 -3
- data/lib/twitter_cache.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b33820ed5e0c4c0de73bc2d7d9e5bd66e25ebb42
|
4
|
+
data.tar.gz: 6d1004ae84dc7b09acb2cd6c251dc141fae73f12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66522d777898e8a9744f613c3ee813e88ae7afc09cb0871279ec376e5d220479464af1034fe827a55d16c3a0f9ea74682762183f1d2b5ec74c58f98409a64b00
|
7
|
+
data.tar.gz: ace040c32cb309cc327948b3ae558fe05a96772da881fcc5fb8b2f30119e4716730033c8bb42fc80b5d5139481f21608510b66e2bdbf96c2c6df94e5fb4c5a73
|
data/README.md
CHANGED
@@ -24,12 +24,9 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
TwitterCache.configure do |config|
|
27
|
-
config.
|
28
|
-
consumer_key: 'CONSUMER_KEY',
|
29
|
-
consumer_secret: 'CONSUMER_SECRET'
|
30
|
-
}
|
31
|
-
config.redis = 'redis://127.0.0.1:6379/' # default ENV['REDIS_URL']
|
27
|
+
config.redis = 'redis://127.0.0.1:6379/' # default: ENV['REDIS_URL']
|
32
28
|
config.ttl = 60 * 30 # sec
|
29
|
+
config.namespace = 'myapp' # default: twitter-cache-gem
|
33
30
|
config.user_instance do |raw|
|
34
31
|
User.new(id: raw.id, nickname: raw.screen_name,
|
35
32
|
image: raw.profile_image_url_https.to_s)
|
@@ -39,35 +36,24 @@ end
|
|
39
36
|
|
40
37
|
### Usage
|
41
38
|
|
42
|
-
|
39
|
+
create cache client
|
43
40
|
|
44
41
|
```ruby
|
45
|
-
twitter =
|
46
|
-
|
47
|
-
|
48
|
-
Authorized client:
|
49
|
-
|
50
|
-
```ruby
|
51
|
-
twitter = TwitterCache::Client.new(access_token: 'token', access_token_secret: 'secret')
|
52
|
-
|
53
|
-
# or with block
|
54
|
-
|
55
|
-
twitter = TwitterCache::Client.new do |c|
|
56
|
-
c.access_token = 'token'
|
57
|
-
c.access_token_secret = 'secret'
|
58
|
-
end
|
42
|
+
twitter = Twitter::REST::Client.new(consumer_key: 'key', ...)
|
43
|
+
cache = Twitter::Cache.new(twitter)
|
59
44
|
```
|
60
45
|
|
61
46
|
Get friends:
|
62
47
|
|
63
48
|
```ruby
|
49
|
+
|
64
50
|
# for authorized user
|
65
|
-
|
66
|
-
|
51
|
+
cache.friends(per: 200, page: 1)
|
52
|
+
cache.friends(per: 200, page: :randam)
|
67
53
|
|
68
54
|
# for other user
|
69
|
-
|
70
|
-
|
55
|
+
cache.friends(123456, per: 200, page: 1)
|
56
|
+
cache.friends(123456, per: 200, page: :random)
|
71
57
|
```
|
72
58
|
|
73
59
|
## Development
|
data/Rakefile
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Twitter
|
2
|
+
module Cache
|
3
|
+
class Config
|
4
|
+
attr_accessor :redis, :ttl, :namespace
|
5
|
+
|
6
|
+
def namespace
|
7
|
+
@namespace ||= 'twitter-cache-gem'
|
8
|
+
end
|
9
|
+
|
10
|
+
def user_instance(&block)
|
11
|
+
@user_instance = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def convert_user(raw)
|
15
|
+
return raw unless @user_instance
|
16
|
+
@user_instance.call(raw)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'redis-namespace'
|
2
|
+
require 'oj'
|
3
|
+
|
4
|
+
module Twitter
|
5
|
+
module Cache
|
6
|
+
class Redis
|
7
|
+
include Twitter::Cache::Helpers
|
8
|
+
attr_reader :client
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
redis_url = ENV['REDIS_URL'] || config.redis
|
12
|
+
redis = ::Redis.new(url: redis_url)
|
13
|
+
@client = ::Redis::Namespace.new(config.namespace, redis: redis)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
client.send(name, *args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(key, ttl: nil)
|
21
|
+
stored = client.get(key)
|
22
|
+
return Oj.load(stored, symbolize_keys: true) if stored
|
23
|
+
return nil unless block_given?
|
24
|
+
|
25
|
+
value = yield
|
26
|
+
set(key, value, ttl: ttl)
|
27
|
+
value
|
28
|
+
end
|
29
|
+
|
30
|
+
def set(key, value, ttl: nil)
|
31
|
+
client.set(key, Oj.dump(value))
|
32
|
+
client.expire(key, ttl) if ttl
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Twitter
|
2
|
+
module Cache
|
3
|
+
class Wrapper
|
4
|
+
include Twitter::Cache::Helpers
|
5
|
+
attr_reader :twitter
|
6
|
+
|
7
|
+
def initialize(twitter)
|
8
|
+
@twitter = twitter
|
9
|
+
end
|
10
|
+
|
11
|
+
def user(id = current_id)
|
12
|
+
cache.get(user_key(id), ttl: config.ttl) do
|
13
|
+
config.convert_user(twitter.user(id))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def friend_ids(id = current_id)
|
18
|
+
cache.get(friends_key(id), ttl: config.ttl) do
|
19
|
+
twitter.friend_ids(id).to_a
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def friends(id = current_id, per: 100, page: 0)
|
24
|
+
ids = friend_ids(id)
|
25
|
+
case page
|
26
|
+
when :rand, :random
|
27
|
+
users(ids.sample(per))
|
28
|
+
else
|
29
|
+
start = per * page
|
30
|
+
users(ids[start, per])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def users(ids)
|
35
|
+
users = ids.map { |id| cache.get(user_key(id)) }.compact
|
36
|
+
return users if ids.count == users.count
|
37
|
+
fetch_users(ids)
|
38
|
+
end
|
39
|
+
|
40
|
+
def current_user
|
41
|
+
@current_user ||= twitter.current_user
|
42
|
+
end
|
43
|
+
|
44
|
+
def current_id
|
45
|
+
current_user.id
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def user_key(id)
|
51
|
+
"user:#{id}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def friends_key(id)
|
55
|
+
"friends:#{id}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def fetch_users(ids)
|
59
|
+
users = twitter.users(ids).map { |raw| config.convert_user(raw) }
|
60
|
+
users.each do |user|
|
61
|
+
cache.set(user_key(user.id), user, ttl: config.ttl)
|
62
|
+
end
|
63
|
+
users
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'twitter/cache/version'
|
2
|
+
require 'twitter/cache/helpers'
|
3
|
+
require 'twitter/cache/config'
|
4
|
+
require 'twitter/cache/redis'
|
5
|
+
require 'twitter/cache/wrapper'
|
6
|
+
|
7
|
+
module Twitter
|
8
|
+
module Cache
|
9
|
+
def self.configure
|
10
|
+
yield(config)
|
11
|
+
config.freeze
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.config
|
15
|
+
@config ||= Config.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.new(twitter)
|
19
|
+
Twitter::Cache::Wrapper.new(twitter)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/twitter-cache.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require '
|
1
|
+
require 'twitter/cache'
|
data/twitter-cache.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'twitter/cache/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'twitter-cache'
|
8
|
-
spec.version =
|
8
|
+
spec.version = Twitter::Cache::VERSION
|
9
9
|
spec.authors = ['masarakki']
|
10
10
|
spec.email = ['masaki@hisme.net']
|
11
11
|
|
@@ -21,11 +21,11 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
23
23
|
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
-
%w(rspec rubocop guard guard-rspec guard-rubocop rspec-its activemodel coveralls
|
24
|
+
%w(rspec rubocop guard guard-rspec guard-rubocop rspec-its activemodel coveralls pry).each do |gem|
|
25
25
|
spec.add_development_dependency gem
|
26
26
|
end
|
27
27
|
|
28
|
-
%w(
|
28
|
+
%w(redis-namespace oj).each do |gem|
|
29
29
|
spec.add_dependency gem
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitter-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masarakki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -150,20 +150,6 @@ dependencies:
|
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
-
- !ruby/object:Gem::Dependency
|
154
|
-
name: webmock
|
155
|
-
requirement: !ruby/object:Gem::Requirement
|
156
|
-
requirements:
|
157
|
-
- - ">="
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
version: '0'
|
160
|
-
type: :development
|
161
|
-
prerelease: false
|
162
|
-
version_requirements: !ruby/object:Gem::Requirement
|
163
|
-
requirements:
|
164
|
-
- - ">="
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version: '0'
|
167
153
|
- !ruby/object:Gem::Dependency
|
168
154
|
name: pry
|
169
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -178,34 +164,6 @@ dependencies:
|
|
178
164
|
- - ">="
|
179
165
|
- !ruby/object:Gem::Version
|
180
166
|
version: '0'
|
181
|
-
- !ruby/object:Gem::Dependency
|
182
|
-
name: dotenv
|
183
|
-
requirement: !ruby/object:Gem::Requirement
|
184
|
-
requirements:
|
185
|
-
- - ">="
|
186
|
-
- !ruby/object:Gem::Version
|
187
|
-
version: '0'
|
188
|
-
type: :development
|
189
|
-
prerelease: false
|
190
|
-
version_requirements: !ruby/object:Gem::Requirement
|
191
|
-
requirements:
|
192
|
-
- - ">="
|
193
|
-
- !ruby/object:Gem::Version
|
194
|
-
version: '0'
|
195
|
-
- !ruby/object:Gem::Dependency
|
196
|
-
name: twitter
|
197
|
-
requirement: !ruby/object:Gem::Requirement
|
198
|
-
requirements:
|
199
|
-
- - ">="
|
200
|
-
- !ruby/object:Gem::Version
|
201
|
-
version: '0'
|
202
|
-
type: :runtime
|
203
|
-
prerelease: false
|
204
|
-
version_requirements: !ruby/object:Gem::Requirement
|
205
|
-
requirements:
|
206
|
-
- - ">="
|
207
|
-
- !ruby/object:Gem::Version
|
208
|
-
version: '0'
|
209
167
|
- !ruby/object:Gem::Dependency
|
210
168
|
name: redis-namespace
|
211
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -253,12 +211,12 @@ files:
|
|
253
211
|
- bin/console
|
254
212
|
- bin/setup
|
255
213
|
- lib/twitter-cache.rb
|
256
|
-
- lib/
|
257
|
-
- lib/
|
258
|
-
- lib/
|
259
|
-
- lib/
|
260
|
-
- lib/
|
261
|
-
- lib/
|
214
|
+
- lib/twitter/cache.rb
|
215
|
+
- lib/twitter/cache/config.rb
|
216
|
+
- lib/twitter/cache/helpers.rb
|
217
|
+
- lib/twitter/cache/redis.rb
|
218
|
+
- lib/twitter/cache/version.rb
|
219
|
+
- lib/twitter/cache/wrapper.rb
|
262
220
|
- twitter-cache.gemspec
|
263
221
|
homepage: https://github.com/masarakki/twitter-cache
|
264
222
|
licenses:
|
data/lib/twitter_cache/client.rb
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'twitter'
|
2
|
-
|
3
|
-
module TwitterCache
|
4
|
-
class Client
|
5
|
-
include TwitterCache::Helpers
|
6
|
-
attr_accessor :access_token, :access_token_secret
|
7
|
-
|
8
|
-
def initialize(access_token: nil, access_token_secret: nil)
|
9
|
-
@access_token = access_token
|
10
|
-
@access_token_secret = access_token_secret
|
11
|
-
yield(self) if block_given?
|
12
|
-
end
|
13
|
-
|
14
|
-
def user(id = current_id)
|
15
|
-
cache.get(user_key(id), ttl: config.ttl) do
|
16
|
-
config.convert_user(twitter.user(id))
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def friend_ids(id = current_id)
|
21
|
-
cache.get(friends_key(id), ttl: config.ttl) do
|
22
|
-
twitter.friend_ids(id).to_a
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def friends(id = current_id, per: 100, page: 0)
|
27
|
-
ids = friend_ids(id)
|
28
|
-
case page
|
29
|
-
when :rand, :random
|
30
|
-
users(ids.sample(per))
|
31
|
-
else
|
32
|
-
start = per * page
|
33
|
-
users(ids[start, per])
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def users(ids)
|
38
|
-
users = ids.map { |id| cache.get(user_key(id)) }.compact
|
39
|
-
return users if ids.count == users.count
|
40
|
-
fetch_users(ids)
|
41
|
-
end
|
42
|
-
|
43
|
-
def current_user
|
44
|
-
@current_user ||= twitter.current_user
|
45
|
-
end
|
46
|
-
|
47
|
-
def current_id
|
48
|
-
current_user.id
|
49
|
-
end
|
50
|
-
|
51
|
-
def twitter
|
52
|
-
@twitter ||= ::Twitter::REST::Client.new(config.twitter) do |conf|
|
53
|
-
if token_given?
|
54
|
-
conf.access_token = access_token
|
55
|
-
conf.access_token_secret = access_token_secret
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
protected
|
61
|
-
|
62
|
-
def token_given?
|
63
|
-
access_token && access_token_secret
|
64
|
-
end
|
65
|
-
|
66
|
-
def user_key(id)
|
67
|
-
"user:#{id}"
|
68
|
-
end
|
69
|
-
|
70
|
-
def friends_key(id)
|
71
|
-
"friends:#{id}"
|
72
|
-
end
|
73
|
-
|
74
|
-
def fetch_users(ids)
|
75
|
-
users = twitter.users(ids).map { |raw| config.convert_user(raw) }
|
76
|
-
users.each do |user|
|
77
|
-
cache.set(user_key(user.id), user, ttl: config.ttl)
|
78
|
-
end
|
79
|
-
users
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
data/lib/twitter_cache/config.rb
DELETED
data/lib/twitter_cache/redis.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'redis-namespace'
|
2
|
-
require 'oj'
|
3
|
-
|
4
|
-
module TwitterCache
|
5
|
-
class Redis
|
6
|
-
include TwitterCache::Helpers
|
7
|
-
attr_reader :client
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
redis_url = ENV['REDIS_URL'] || config.redis
|
11
|
-
redis = ::Redis.new(url: redis_url)
|
12
|
-
@client = ::Redis::Namespace.new(:twitter_cache_gem, redis: redis)
|
13
|
-
end
|
14
|
-
|
15
|
-
def method_missing(name, *args)
|
16
|
-
client.send(name, *args)
|
17
|
-
end
|
18
|
-
|
19
|
-
def get(key, ttl: nil)
|
20
|
-
stored = client.get(key)
|
21
|
-
return Oj.load(stored, symbolize_keys: true) if stored
|
22
|
-
return nil unless block_given?
|
23
|
-
|
24
|
-
value = yield
|
25
|
-
set(key, value, ttl: ttl)
|
26
|
-
value
|
27
|
-
end
|
28
|
-
|
29
|
-
def set(key, value, ttl: nil)
|
30
|
-
client.set(key, Oj.dump(value))
|
31
|
-
client.expire(ttl) if ttl
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
data/lib/twitter_cache.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'twitter_cache/version'
|
2
|
-
require 'twitter_cache/helpers'
|
3
|
-
require 'twitter_cache/config'
|
4
|
-
require 'twitter_cache/redis'
|
5
|
-
require 'twitter_cache/client'
|
6
|
-
|
7
|
-
module TwitterCache
|
8
|
-
def self.configure
|
9
|
-
yield(config)
|
10
|
-
config.freeze
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.config
|
14
|
-
@config ||= Config.new
|
15
|
-
end
|
16
|
-
end
|