twitter-redis-identitymap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ 0.0.1
2
+
3
+ Initial commit
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kevin Fullerton
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.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ twitter-redis-identitymap
2
+ =========================
3
+ [![Build Status](https://secure.travis-ci.org/kgfullerton/twitter-redis-identitymap.png?branch=master)][travis]
4
+ [travis]: http://travis-ci.org/kgfullerton/twitter-redis-identitymap
5
+
6
+ ### Redis IdentityMap store for sferik/twitter gem
7
+
8
+ ## Installation
9
+ ```sh
10
+ gem install twitter-redis-identitymap
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ Twitter.configure do |config|
17
+ config.consumer_key = YOUR_CONSUMER_KEY
18
+ config.consumer_secret = YOUR_CONSUMER_SECRET
19
+ config.oauth_token = YOUR_OAUTH_TOKEN
20
+ config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
21
+ config.identity_map = TwitterRedisIdentityMap::Base
22
+ end
23
+ ```
24
+
25
+ This will use the default Redis URL (redis://localhost:6379/0)
26
+
27
+ By default it uses a key namespace of tri:cache: followed by the id to store
28
+
29
+ ## To Do
30
+
31
+ * Allow configuring Redis location and namespace
32
+ * Allow Redis TTL expiring of keys automatically
33
+
34
+ ## Contributing
35
+ In the spirit of [free software][free-sw], **everyone** is encouraged to help
36
+ improve this project.
37
+
38
+ [free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
39
+
40
+ Here are some ways *you* can contribute:
41
+
42
+ * by using alpha, beta, and prerelease versions
43
+ * by reporting bugs
44
+ * by suggesting new features
45
+ * by writing or editing documentation
46
+ * by writing specifications
47
+ * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
48
+ * by refactoring code
49
+ * by fixing [issues][]
50
+ * by reviewing patches
51
+
52
+ [issues]: https://github.com/kgfullerton/twitter-redis-identitymap/issues
53
+
54
+ ## Submitting an Issue
55
+ We use the [GitHub issue tracker][issues] to track bugs and features. Before
56
+ submitting a bug report or feature request, check to make sure it hasn't
57
+ already been submitted. When submitting a bug report, please include a [Gist][]
58
+ that includes a stack trace and any details that may be necessary to reproduce
59
+ the bug, including your gem version, Ruby version, and operating system.
60
+ Ideally, a bug report should include a pull request with failing specs.
61
+
62
+ [gist]: https://gist.github.com/
63
+
64
+ ## Submitting a Pull Request
65
+ 1. [Fork the repository.][fork]
66
+ 2. [Create a topic branch.][branch]
67
+ 3. Add specs for your unimplemented feature or bug fix.
68
+ 4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
69
+ 5. Implement your feature or bug fix.
70
+ 6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
71
+ 7. Run `open coverage/index.html`. If your changes are not completely covered by your tests, return to step 3.
72
+ 8. Add, commit, and push your changes.
73
+ 9. [Submit a pull request.][pr]
74
+
75
+ [fork]: http://help.github.com/fork-a-repo/
76
+ [branch]: http://learn.github.com/p/branching.html
77
+ [pr]: http://help.github.com/send-pull-requests/
78
+
79
+ ## Supported Ruby Versions
80
+ This library aims to support and is [tested against][travis] the following Ruby
81
+ version:
82
+
83
+ * Ruby 1.8.7
84
+ * Ruby 1.9.2
85
+ * Ruby 1.9.3
86
+
87
+ This library may inadvertently work (or seem to work) on other Ruby
88
+ implementations, however support will only be provided for the versions listed
89
+ above.
90
+
91
+ If you would like this library to support another Ruby version, you may
92
+ volunteer to be a maintainer. Being a maintainer entails making sure all tests
93
+ run and pass on that implementation. When something breaks on your
94
+ implementation, you will be personally responsible for providing patches in a
95
+ timely fashion. If critical issues for a particular implementation exist at the
96
+ time of a major release, support for that Ruby version may be dropped.
97
+
98
+ ## Copyright
99
+ Copyright (c) 2012 Kevin Fullerton.
100
+ See [LICENSE][] for details.
101
+ Thanks to the contributors and maintainers of the [Twitter gem][twittergem] who the tests were used from to ensure compatibility
102
+
103
+ [license]: https://github.com/kgfullerton/twitter-redis-identitymap/blob/master/LICENSE.md
104
+ [twittergem]: https://github.com/sferik/twitter
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
@@ -0,0 +1 @@
1
+ require 'twitter-redis-identitymap/base'
@@ -0,0 +1,33 @@
1
+ require 'redis'
2
+
3
+ module TwitterRedisIdentityMap
4
+ class Base
5
+ def initialize(options = nil)
6
+ raise ArgumentError if !options.nil? && !options.is_a?(Hash)
7
+ @options = options.nil? ? {} : options
8
+ @options[:namespace] = "tri:cache:" unless @options[:namespace]
9
+ @redis = @options[:redis].nil? ? Redis.new : Redis.new({:url => @options[:redis]})
10
+ end
11
+
12
+ def fetch(id)
13
+ raise ArgumentError if id.nil?
14
+ id = Marshal.load(id)
15
+ object = nil
16
+ if @redis.exists("#{@options[:namespace]}#{id[:id]}")
17
+ val = @redis.get("#{@options[:namespace]}#{id[:id]}")
18
+ object = Marshal.load(val)
19
+ end
20
+ object
21
+ end
22
+
23
+ def store(attrs, object)
24
+ raise ArgumentError if (attrs.nil? || object.nil?)
25
+ attrs = Marshal.load(attrs)
26
+ @redis.set("#{@options[:namespace]}#{attrs[:id]}", Marshal.dump(object)) == "OK" ? object : nil
27
+ end
28
+
29
+ def redis
30
+ @redis
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module TwitterRedisIdentityMap
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+ PRE = nil
7
+
8
+ class << self
9
+ def to_s
10
+ [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
+ end
12
+ end
13
+ end
14
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'spec'
4
+ end
5
+
6
+ require 'twitter-redis-identitymap'
7
+ require 'twitter'
@@ -0,0 +1,105 @@
1
+ require 'helper'
2
+
3
+ describe TwitterRedisIdentityMap::Base do
4
+ context "base class" do
5
+ before(:each) do
6
+ @map = TwitterRedisIdentityMap::Base.new
7
+ end
8
+
9
+ it "should respond_to fetch" do
10
+ @map.respond_to?(:fetch).should be_true
11
+ end
12
+
13
+ it "should respond_to store" do
14
+ @map.respond_to?(:store).should be_true
15
+ end
16
+
17
+ it "should respond_to redis" do
18
+ @map.respond_to?(:redis).should be_true
19
+ end
20
+ end
21
+
22
+ context "base initializer" do
23
+ it "should return a default redis instance with no parameters" do
24
+ @map = TwitterRedisIdentityMap::Base.new
25
+ @map.redis.should be_a(Redis)
26
+ @map.redis.client.location.should eq("127.0.0.1:6379")
27
+ @map.redis.client.db.should eq(0)
28
+ end
29
+ it "should allow connecting to a different redis instance" do
30
+ options = {:redis => "redis://127.0.0.1:6379/1" }
31
+ @map = TwitterRedisIdentityMap::Base.new(options)
32
+ @map.redis.client.location.should eq("127.0.0.1:6379")
33
+ @map.redis.client.db.should eq(1)
34
+ end
35
+ end
36
+
37
+ context "integration with Twitter::Base" do
38
+ before do
39
+ Twitter.identity_map = TwitterRedisIdentityMap::Base
40
+ Twitter::Base.identity_map.redis.flushdb
41
+ object = Twitter::Base.new(:id => 3)
42
+ @base = Twitter::Base.store(object)
43
+ end
44
+
45
+ after do
46
+ Twitter::Base.identity_map.redis.flushdb
47
+ Twitter.identity_map = nil
48
+ end
49
+
50
+ describe ".identity_map" do
51
+ it "returns an instance of the identity map" do
52
+ Twitter::Base.identity_map.should be_a TwitterRedisIdentityMap::Base
53
+ end
54
+ end
55
+
56
+ describe ".store" do
57
+ it "stores Twitter::Base objects" do
58
+ object = Twitter::Base.new(:id => 1)
59
+ Twitter::Base.identity_map.redis.exists("tri:cache:1").should eq(false)
60
+ Twitter::Base.store(object).should be_a(Twitter::Base)
61
+ Twitter::Base.identity_map.redis.exists("tri:cache:1").should eq(true)
62
+ end
63
+ end
64
+
65
+ describe ".fetch" do
66
+ it "returns existing objects" do
67
+ Twitter::Base.fetch(:id => 3).should be
68
+ end
69
+ it "raises an error on objects that don't exist" do
70
+ expect { Twitter::Base.fetch(:id => 999) }.to raise_error(Twitter::Error::IdentityMapKeyError)
71
+ end
72
+ end
73
+
74
+ describe ".fetch_or_new" do
75
+ it "returns existing objects" do
76
+ Twitter::Base.fetch(:id => 3).should be
77
+ end
78
+ it "creates new objects and stores them" do
79
+ expect { Twitter::Base.fetch(:id => 7) }.to raise_error(Twitter::Error::IdentityMapKeyError)
80
+ Twitter::Base.fetch_or_new(:id => 7).should be
81
+ Twitter::Base.fetch(:id => 7).should be
82
+ end
83
+ end
84
+
85
+ describe "#[]" do
86
+ it "calls methods using [] with symbol" do
87
+ @base[:object_id].should be_an Integer
88
+ end
89
+ it "calls methods using [] with string" do
90
+ @base['object_id'].should be_an Integer
91
+ end
92
+ it "returns nil for missing methods" do
93
+ @base[:foo].should be_nil
94
+ @base['foo'].should be_nil
95
+ end
96
+ end
97
+
98
+ describe "#to_hash" do
99
+ it "returns a hash" do
100
+ @base.to_hash.should be_a Hash
101
+ @base.to_hash[:id].should eq(3)
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/twitter-redis-identitymap/version',__FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.add_dependency 'redis'
5
+
6
+ spec.add_development_dependency 'rake'
7
+ spec.add_development_dependency 'rspec'
8
+ spec.add_development_dependency 'simplecov'
9
+ spec.add_development_dependency 'twitter', '~> 4.0.0'
10
+
11
+ spec.authors = ["Kevin Fullerton"]
12
+ spec.description = %q{A redis IdentityMap store for the twitter gem}
13
+ spec.email = ["github@kenwa-solutions.co.uk"]
14
+ spec.files = %w{CHANGELOG.md LICENSE.md README.md Rakefile twitter-redis-identitymap.gemspec}
15
+ spec.files += Dir.glob("lib/**/*.rb")
16
+ spec.files += Dir.glob("spec/**/*")
17
+ spec.homepage = "http://github.com/kgfullerton/twitter-redis-identitymap"
18
+ spec.name = "twitter-redis-identitymap"
19
+ spec.require_paths = ["lib"]
20
+ spec.required_rubygems_version = Gem::Requirement.new(">= 1.3.6")
21
+ spec.summary = spec.description
22
+ spec.test_files = Dir.glob("spec/**/*")
23
+ spec.version = TwitterRedisIdentityMap::Version
24
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter-redis-identitymap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Fullerton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: twitter
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 4.0.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 4.0.0
94
+ description: A redis IdentityMap store for the twitter gem
95
+ email:
96
+ - github@kenwa-solutions.co.uk
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - CHANGELOG.md
102
+ - LICENSE.md
103
+ - README.md
104
+ - Rakefile
105
+ - twitter-redis-identitymap.gemspec
106
+ - lib/twitter-redis-identitymap/version.rb
107
+ - lib/twitter-redis-identitymap/base.rb
108
+ - lib/twitter-redis-identitymap.rb
109
+ - spec/twitter-redis-identitymap/base_spec.rb
110
+ - spec/helper.rb
111
+ homepage: http://github.com/kgfullerton/twitter-redis-identitymap
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: 1.3.6
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: A redis IdentityMap store for the twitter gem
135
+ test_files:
136
+ - spec/twitter-redis-identitymap/base_spec.rb
137
+ - spec/helper.rb