twitter-cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a83a641108791a5a3691a56ccae188dbb41762ac
4
+ data.tar.gz: 92d8d426440bcaf8dae90b22aa8a1cfa35f99112
5
+ SHA512:
6
+ metadata.gz: 72c516252444d1331c5de0e09281261db8de3d43a8440e1dd042b576c0e449ae82f7a2875811fe3fb0a50a84d89528c90501789fa0f61b531b2ec537e2fd0187
7
+ data.tar.gz: 2e3d64e0477a01912e63de74b43c7347303d30d3f550f37f8e01fed06c83fbae3343f68262634c4ecb66fa24252b531588b9081aeb260b608c457ca2b24a16cc
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ Exclude:
3
+ - twitter-cache.gemspec
4
+ Style/Documentation:
5
+ Enabled: false
6
+ Style/FileName:
7
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ env:
5
+ - CONSUMER_KEY=key CONSUMER_SECRET=secret ACCESS_TOKEN=token ACCESS_TOKEN_SECRET=token_secret
6
+ services:
7
+ - redis
8
+ script:
9
+ - bundle exec rake spec
10
+ - bundle exec rubocop
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twitter-cache.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,17 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ rspec = dsl.rspec
6
+ watch(rspec.spec_helper) { rspec.spec_dir }
7
+ watch(rspec.spec_support) { rspec.spec_dir }
8
+ watch(rspec.spec_files)
9
+
10
+ ruby = dsl.ruby
11
+ dsl.watch_spec_files_for(ruby.lib_files)
12
+ end
13
+
14
+ guard :rubocop, cli: '--auto-correct' do
15
+ watch(/.+\.rb$/)
16
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
17
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 masarakki
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # TwitterCache
2
+
3
+ easy access and cache to twitter friends.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'twitter-cache'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install twitter-cache
20
+
21
+ ## Usage
22
+
23
+ ### Setup
24
+
25
+ ```ruby
26
+ TwitterCache.configure do |config|
27
+ config.twitter = {
28
+ consumer_key: 'CONSUMER_KEY',
29
+ consumer_secret: 'CONSUMER_SECRET'
30
+ }
31
+ config.redis = 'redis://127.0.0.1:6379/' # default ENV['REDIS_URL']
32
+ config.ttl = 60 * 30 # sec
33
+ config.user_instance do |raw|
34
+ User.new(id: raw.id, nickname: raw.screen_name,
35
+ image: raw.profile_image_url_https.to_s)
36
+ end
37
+ end
38
+ ```
39
+
40
+ ### Usage
41
+
42
+ ```ruby
43
+ twitter = TwitterCache::Client.new
44
+ # or with authorization
45
+ twitter = TwitterCache::Client.new(access_token: 'token', access_token_secret: 'secre
46
+ t')
47
+
48
+ # for authorized user
49
+ twitter.friends(per: 200, page: 1)
50
+ twitter.friends(per: 200, page: :randam)
51
+
52
+ # for other user
53
+ twitter.friends(123456, per: 200, page: 1)
54
+ twitter.friends(123456, per: 200, page: :random)
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/masarakki/twitter_friends/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'twitter-cache'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'dotenv'
14
+ Dotenv.load
15
+
16
+ require 'pry'
17
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'twitter_cache'
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,71 @@
1
+ require 'twitter'
2
+
3
+ module TwitterCache
4
+ class Client
5
+ include TwitterCache::Helpers
6
+ attr_reader :tokens
7
+
8
+ def initialize(tokens = nil)
9
+ @tokens = tokens
10
+ end
11
+
12
+ def user(id = current_id)
13
+ cache.get(user_key(id), ttl: config.ttl) do
14
+ config.convert_user(twitter.user(id))
15
+ end
16
+ end
17
+
18
+ def friend_ids(id = current_id)
19
+ cache.get(friends_key(id), ttl: config.ttl) do
20
+ twitter.friend_ids(id).to_a
21
+ end
22
+ end
23
+
24
+ def friends(id = current_id, per: 100, page: 0)
25
+ ids = friend_ids(id)
26
+ case page
27
+ when :rand, :random
28
+ users(ids.sample(per))
29
+ else
30
+ start = per * page
31
+ users(ids[start, per])
32
+ end
33
+ end
34
+
35
+ def users(ids)
36
+ users = ids.map { |id| cache.get(user_key(id)) }.compact
37
+ return users if ids.count == users.count
38
+ fetch_users(ids)
39
+ end
40
+
41
+ def current_user
42
+ @current_user ||= twitter.current_user
43
+ end
44
+
45
+ def current_id
46
+ current_user.id
47
+ end
48
+
49
+ def twitter
50
+ @twitter ||= ::Twitter::REST::Client.new(config.twitter.dup.merge(tokens))
51
+ end
52
+
53
+ protected
54
+
55
+ def user_key(id)
56
+ "user:#{id}"
57
+ end
58
+
59
+ def friends_key(id)
60
+ "friends:#{id}"
61
+ end
62
+
63
+ def fetch_users(ids)
64
+ users = twitter.users(ids).map { |raw| config.convert_user(raw) }
65
+ users.each do |user|
66
+ cache.set(user_key(user.id), user, ttl: config.ttl)
67
+ end
68
+ users
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,14 @@
1
+ module TwitterCache
2
+ class Config
3
+ attr_accessor :twitter, :redis, :ttl
4
+
5
+ def user_instance(&block)
6
+ @user_instance = block
7
+ end
8
+
9
+ def convert_user(raw)
10
+ return raw unless @user_instance
11
+ @user_instance.call(raw)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module TwitterCache
2
+ module Helpers
3
+ def config
4
+ TwitterCache.config
5
+ end
6
+
7
+ def cache
8
+ @cache ||= TwitterCache::Redis.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
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
@@ -0,0 +1,3 @@
1
+ module TwitterCache
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twitter_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'twitter-cache'
8
+ spec.version = TwitterCache::VERSION
9
+ spec.authors = ['masarakki']
10
+ spec.email = ['masaki@hisme.net']
11
+
12
+ spec.summary = 'easy access and cache twitter friends'
13
+ spec.description = 'easy access and cache twitter friends'
14
+ spec.homepage = 'https://github.com/masarakki/twitter-cache'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ %w(rspec rubocop guard guard-rspec guard-rubocop rspec-its activemodel coveralls webmock pry dotenv).each do |gem|
25
+ spec.add_development_dependency gem
26
+ end
27
+
28
+ %w(twitter redis-namespace oj).each do |gem|
29
+ spec.add_dependency gem
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,287 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - masarakki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-its
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activemodel
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: coveralls
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ 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
+ - !ruby/object:Gem::Dependency
210
+ name: redis-namespace
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: oj
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ description: easy access and cache twitter friends
238
+ email:
239
+ - masaki@hisme.net
240
+ executables: []
241
+ extensions: []
242
+ extra_rdoc_files: []
243
+ files:
244
+ - ".gitignore"
245
+ - ".rspec"
246
+ - ".rubocop.yml"
247
+ - ".travis.yml"
248
+ - Gemfile
249
+ - Guardfile
250
+ - LICENSE.txt
251
+ - README.md
252
+ - Rakefile
253
+ - bin/console
254
+ - bin/setup
255
+ - lib/twitter-cache.rb
256
+ - lib/twitter_cache.rb
257
+ - lib/twitter_cache/client.rb
258
+ - lib/twitter_cache/config.rb
259
+ - lib/twitter_cache/helpers.rb
260
+ - lib/twitter_cache/redis.rb
261
+ - lib/twitter_cache/version.rb
262
+ - twitter-cache.gemspec
263
+ homepage: https://github.com/masarakki/twitter-cache
264
+ licenses:
265
+ - MIT
266
+ metadata: {}
267
+ post_install_message:
268
+ rdoc_options: []
269
+ require_paths:
270
+ - lib
271
+ required_ruby_version: !ruby/object:Gem::Requirement
272
+ requirements:
273
+ - - ">="
274
+ - !ruby/object:Gem::Version
275
+ version: '0'
276
+ required_rubygems_version: !ruby/object:Gem::Requirement
277
+ requirements:
278
+ - - ">="
279
+ - !ruby/object:Gem::Version
280
+ version: '0'
281
+ requirements: []
282
+ rubyforge_project:
283
+ rubygems_version: 2.2.2
284
+ signing_key:
285
+ specification_version: 4
286
+ summary: easy access and cache twitter friends
287
+ test_files: []