pinky 0.1.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +29 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +20 -0
- data/README.md +27 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/pinky.rb +8 -0
- data/lib/pinky/associations.rb +12 -0
- data/lib/pinky/cachable_model.rb +23 -0
- data/lib/pinky/energizer_bunny/connection.rb +78 -0
- data/lib/pinky/energizer_bunny/subscription.rb +37 -0
- data/lib/pinky/model.rb +28 -0
- data/lib/pinky/model_fetch_methods.rb +35 -0
- data/lib/pinky/model_natural_key_methods.rb +34 -0
- data/pinky.gemspec +87 -0
- data/spec/pinky/associations_spec.rb +44 -0
- data/spec/pinky/cachable_model_spec.rb +52 -0
- data/spec/pinky/model_fetch_methods_spec.rb +51 -0
- data/spec/pinky/model_natural_key_methods_spec.rb +46 -0
- data/spec/pinky/model_spec.rb +53 -0
- data/spec/spec_helper.rb +49 -0
- metadata +217 -0
data/.document
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use jruby-1.7.0@pinky --install --create
|
data/Gemfile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
|
9
|
+
gem 'httparty'
|
10
|
+
|
11
|
+
gem 'json'
|
12
|
+
|
13
|
+
platform :jruby do
|
14
|
+
# energizer
|
15
|
+
gem 'hot_bunnies'
|
16
|
+
gem 'mailbox'
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test, :development do
|
20
|
+
#gem 'fakeweb'
|
21
|
+
gem 'timecop'
|
22
|
+
gem "rspec", ">= 2.11.0"
|
23
|
+
end
|
24
|
+
|
25
|
+
group :development do
|
26
|
+
gem "rdoc", "~> 3.12"
|
27
|
+
gem "bundler", "> 1.0.0"
|
28
|
+
gem "jeweler", "~> 1.8.4"
|
29
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
hot_bunnies (1.3.8-java)
|
7
|
+
httparty (0.9.0)
|
8
|
+
multi_json (~> 1.0)
|
9
|
+
multi_xml
|
10
|
+
jeweler (1.8.4)
|
11
|
+
bundler (~> 1.0)
|
12
|
+
git (>= 1.2.5)
|
13
|
+
rake
|
14
|
+
rdoc
|
15
|
+
jretlang (0.3.0)
|
16
|
+
json (1.7.5)
|
17
|
+
json (1.7.5-java)
|
18
|
+
mailbox (0.2.7)
|
19
|
+
jretlang
|
20
|
+
multi_json (1.3.6)
|
21
|
+
multi_xml (0.5.1)
|
22
|
+
rake (0.9.2.2)
|
23
|
+
rdoc (3.12)
|
24
|
+
json (~> 1.4)
|
25
|
+
rspec (2.11.0)
|
26
|
+
rspec-core (~> 2.11.0)
|
27
|
+
rspec-expectations (~> 2.11.0)
|
28
|
+
rspec-mocks (~> 2.11.0)
|
29
|
+
rspec-core (2.11.1)
|
30
|
+
rspec-expectations (2.11.3)
|
31
|
+
diff-lcs (~> 1.1.3)
|
32
|
+
rspec-mocks (2.11.3)
|
33
|
+
timecop (0.5.2)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
java
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
bundler (> 1.0.0)
|
41
|
+
hot_bunnies
|
42
|
+
httparty
|
43
|
+
jeweler (~> 1.8.4)
|
44
|
+
json
|
45
|
+
mailbox
|
46
|
+
rdoc (~> 3.12)
|
47
|
+
rspec (>= 2.11.0)
|
48
|
+
timecop
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Joel Friedman
|
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,27 @@
|
|
1
|
+
pinky
|
2
|
+
=====
|
3
|
+
|
4
|
+
In memory API caching made easier
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
Coming soon...
|
10
|
+
|
11
|
+
Contributing to pinky
|
12
|
+
---------------------
|
13
|
+
|
14
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
15
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
16
|
+
* Fork the project.
|
17
|
+
* Start a feature/bugfix branch.
|
18
|
+
* Commit and push until you are happy with your contribution.
|
19
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
20
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
21
|
+
|
22
|
+
Copyright
|
23
|
+
---------
|
24
|
+
|
25
|
+
Copyright (c) 2012 Joel Friedman. See LICENSE.txt for
|
26
|
+
further details.
|
27
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "pinky"
|
18
|
+
gem.platform = 'java'
|
19
|
+
gem.homepage = "http://github.com/trunkclub/pinky"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = %Q{in memory API caching made easy}
|
22
|
+
gem.description = %Q{Cache your api objects in member easier, with associations.}
|
23
|
+
gem.email = "asher.friedman@gmail.com"
|
24
|
+
gem.authors = ["Joel Friedman"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec)
|
31
|
+
|
32
|
+
task :default => :spec
|
33
|
+
|
34
|
+
require 'rdoc/task'
|
35
|
+
Rake::RDocTask.new do |rdoc|
|
36
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
37
|
+
|
38
|
+
rdoc.rdoc_dir = 'rdoc'
|
39
|
+
rdoc.title = "pinky #{version}"
|
40
|
+
rdoc.rdoc_files.include('README*')
|
41
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
42
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/pinky.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Pinky
|
2
|
+
module Associations
|
3
|
+
def has_one klass, opts = {}
|
4
|
+
klass_name = klass.name.split('::').last.downcase
|
5
|
+
find_by = opts[:lookup_by] || "#{klass_name}_id"
|
6
|
+
association_name = opts[:as] || klass_name
|
7
|
+
define_method association_name do
|
8
|
+
klass.find send(find_by)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Pinky
|
2
|
+
module CachableModel
|
3
|
+
def self.extended base
|
4
|
+
base.send :include, ModelNaturalKeyMethods unless base.include? ModelNaturalKeyMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
def update_cache_with item_hash, action
|
8
|
+
item = new item_hash
|
9
|
+
cache.delete item.natural_key
|
10
|
+
cache[item.natural_key] = item unless action.to_sym == :destroy
|
11
|
+
item
|
12
|
+
end
|
13
|
+
|
14
|
+
def clear_cache
|
15
|
+
@cache = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def cache
|
20
|
+
@cache ||= Hash.new { |cache_hash, nat_key| update_cache_with from_wire(nat_key), :create }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.expand_path('../subscription', __FILE__)
|
2
|
+
|
3
|
+
module Pinky
|
4
|
+
module EnergizerBunny
|
5
|
+
class Connection
|
6
|
+
|
7
|
+
def initialize config, logger = Rails.logger
|
8
|
+
@config = config
|
9
|
+
@logger = logger
|
10
|
+
return unless enabled?
|
11
|
+
at_exit { close }
|
12
|
+
@exchanges = Hash.new { |exchanges, topic_key| exchanges[topic_key] = create_exchange topic_key }
|
13
|
+
@queues = Hash.new { |queues, topic_key| queues[topic_key] = create_queue topic_key }
|
14
|
+
@subscriptions = []
|
15
|
+
create_connection
|
16
|
+
end
|
17
|
+
|
18
|
+
def subscribe topic_key, subscription_opts = {}, &block
|
19
|
+
return unless enabled?
|
20
|
+
@subscriptions << Subscription.new(@queues[topic_key], subscription_opts, @logger, block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def connected?
|
24
|
+
@connection && @connection.open?
|
25
|
+
end
|
26
|
+
|
27
|
+
def enabled?
|
28
|
+
@config[:enabled]
|
29
|
+
end
|
30
|
+
|
31
|
+
def connection_url
|
32
|
+
@config[:broker][:url]
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def create_exchange topic_key
|
37
|
+
exchange_hash = @config[:exchanges][topic_key]
|
38
|
+
channel.exchange exchange_hash[:name], exchange_hash[:opts]
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_connection
|
42
|
+
url = connection_url
|
43
|
+
raise Exception.new('Please set a broker url for RabbitMQ') if enabled? && url.nil?
|
44
|
+
@logger.info "Connecting bunny to: #{url}"
|
45
|
+
@connection = HotBunnies.connect :uri => url
|
46
|
+
end
|
47
|
+
|
48
|
+
#TODO: is having one channel bad, should this be one per exchange?
|
49
|
+
def channel
|
50
|
+
@channel ||= @connection.create_channel
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_queue topic_key
|
54
|
+
queue_hash = @config[:queues][topic_key]
|
55
|
+
raise Exception.new("Cannot find queue configuration for queue: #{topic_key}") if queue_hash.nil?
|
56
|
+
channel.queue(queue_hash[:name], queue_hash[:opts]).tap do |queue|
|
57
|
+
queue_bind_opts = queue_hash[:bind_opts] || {}
|
58
|
+
Array(queue_hash[:bindings]).each do |binding|
|
59
|
+
binding_opts = queue_bind_opts.merge(binding[:binding_opts] || {})
|
60
|
+
queue.bind @exchanges[binding[:exchange_topic_key]], binding_opts
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def close
|
66
|
+
@logger.info 'Closing rabbit connection'
|
67
|
+
@subscriptions.each &:close
|
68
|
+
return unless connected?
|
69
|
+
if @channel
|
70
|
+
@channel.close
|
71
|
+
@channel = nil
|
72
|
+
end
|
73
|
+
@connection.close
|
74
|
+
@connection = nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Pinky
|
2
|
+
module EnergizerBunny
|
3
|
+
class Subscription
|
4
|
+
include Mailbox
|
5
|
+
mailbox_thread_pool_size 1
|
6
|
+
|
7
|
+
def initialize queue, subscription_opts, logger, handle_message
|
8
|
+
@queue, @subscription_opts, @logger, @handle_message = queue, subscription_opts, logger, handle_message
|
9
|
+
listen!
|
10
|
+
end
|
11
|
+
|
12
|
+
def close
|
13
|
+
return if @subscription.nil?
|
14
|
+
@subscription.shutdown!
|
15
|
+
@subscription = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def listen!
|
20
|
+
@subscription = @queue.subscribe(@subscription_opts) do |headers, msg|
|
21
|
+
handle_message_on_other_thread headers, msg
|
22
|
+
headers.ack
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
mailslot :exception => :log_error
|
28
|
+
def handle_message_on_other_thread headers, msg
|
29
|
+
@handle_message.call headers.properties.headers, msg
|
30
|
+
end
|
31
|
+
|
32
|
+
def log_error e
|
33
|
+
@logger.error "An error has occured on another thread: #{e}#{$/}#{e.backtrace}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/pinky/model.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Pinky
|
2
|
+
module Model
|
3
|
+
def self.included base
|
4
|
+
base.extend ModelFetchMethods unless base.is_a? ModelFetchMethods
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :cached_at
|
9
|
+
|
10
|
+
def initialize hash
|
11
|
+
@hash = hash
|
12
|
+
hash.keys.each do |method|
|
13
|
+
define_singleton_method method do
|
14
|
+
@hash[method]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@cached_at = DateTime.now
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
module ClassMethods
|
22
|
+
# TODO
|
23
|
+
#def preload
|
24
|
+
#end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Pinky
|
2
|
+
module ModelFetchMethods
|
3
|
+
def self.extended base
|
4
|
+
base.send :include, ModelNaturalKeyMethods unless base.include? ModelNaturalKeyMethods
|
5
|
+
base.extend CachableModel unless base.is_a? CachableModel
|
6
|
+
end
|
7
|
+
|
8
|
+
def find natural_key
|
9
|
+
cache[natural_key.to_s]
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch_url url, fetch_opts = {}
|
13
|
+
@fetch_url = url
|
14
|
+
@fetch_opts = fetch_opts
|
15
|
+
@response_key = @fetch_opts.delete :response_key
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def from_wire natural_key
|
20
|
+
url = _fetch_url_for natural_key
|
21
|
+
response = HTTParty.get url, @fetch_opts
|
22
|
+
response = JSON.parse(response.body)
|
23
|
+
raise Exception.new "Error fetching from #{url}#{$/}#{response['errors'].join ','}" unless response['success']
|
24
|
+
response = response['response']
|
25
|
+
response = response.fetch @response_key if @response_key
|
26
|
+
raise Exception.new "More than one model was returned" if response.size != 1
|
27
|
+
response.first
|
28
|
+
end
|
29
|
+
|
30
|
+
def _fetch_url_for natural_key
|
31
|
+
raise Exception.new "You must specify a fetch_url for #{self.name}" unless @fetch_url
|
32
|
+
@fetch_url.dup.sub /:natural_key/, natural_key.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Pinky
|
2
|
+
module ModelNaturalKeyMethods
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
def natural_key
|
8
|
+
natural_key_methods.map { |method_name| send method_name }.join natural_key_separator
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def natural_key_methods
|
13
|
+
self.class.natural_key or raise Exception.new('You must specify a natural_key, ex: natural_key :first_name, :last_name')
|
14
|
+
end
|
15
|
+
|
16
|
+
def natural_key_separator
|
17
|
+
self.class.natural_key_separator
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def natural_key *methods
|
22
|
+
return @natural_key if methods.nil? || methods.empty?
|
23
|
+
@natural_key = methods
|
24
|
+
end
|
25
|
+
|
26
|
+
def natural_key_separator sep = nil
|
27
|
+
return @natural_key_separator || '.' if sep.nil?
|
28
|
+
@natural_key_separator = sep
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/pinky.gemspec
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "pinky"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
s.platform = "java"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["Joel Friedman"]
|
13
|
+
s.date = "2012-11-07"
|
14
|
+
s.description = "Cache your api objects in member easier, with associations."
|
15
|
+
s.email = "asher.friedman@gmail.com"
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".rvmrc",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/pinky.rb",
|
30
|
+
"lib/pinky/associations.rb",
|
31
|
+
"lib/pinky/cachable_model.rb",
|
32
|
+
"lib/pinky/energizer_bunny/connection.rb",
|
33
|
+
"lib/pinky/energizer_bunny/subscription.rb",
|
34
|
+
"lib/pinky/model.rb",
|
35
|
+
"lib/pinky/model_fetch_methods.rb",
|
36
|
+
"lib/pinky/model_natural_key_methods.rb",
|
37
|
+
"pinky.gemspec",
|
38
|
+
"spec/pinky/associations_spec.rb",
|
39
|
+
"spec/pinky/cachable_model_spec.rb",
|
40
|
+
"spec/pinky/model_fetch_methods_spec.rb",
|
41
|
+
"spec/pinky/model_natural_key_methods_spec.rb",
|
42
|
+
"spec/pinky/model_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
s.homepage = "http://github.com/trunkclub/pinky"
|
46
|
+
s.licenses = ["MIT"]
|
47
|
+
s.require_paths = ["lib"]
|
48
|
+
s.rubygems_version = "1.8.24"
|
49
|
+
s.summary = "in memory API caching made easy"
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0"])
|
56
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
57
|
+
s.add_runtime_dependency(%q<hot_bunnies>, [">= 0"])
|
58
|
+
s.add_runtime_dependency(%q<mailbox>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<timecop>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<rspec>, [">= 2.11.0"])
|
61
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
62
|
+
s.add_development_dependency(%q<bundler>, ["> 1.0.0"])
|
63
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<httparty>, [">= 0"])
|
66
|
+
s.add_dependency(%q<json>, [">= 0"])
|
67
|
+
s.add_dependency(%q<hot_bunnies>, [">= 0"])
|
68
|
+
s.add_dependency(%q<mailbox>, [">= 0"])
|
69
|
+
s.add_dependency(%q<timecop>, [">= 0"])
|
70
|
+
s.add_dependency(%q<rspec>, [">= 2.11.0"])
|
71
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
72
|
+
s.add_dependency(%q<bundler>, ["> 1.0.0"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
74
|
+
end
|
75
|
+
else
|
76
|
+
s.add_dependency(%q<httparty>, [">= 0"])
|
77
|
+
s.add_dependency(%q<json>, [">= 0"])
|
78
|
+
s.add_dependency(%q<hot_bunnies>, [">= 0"])
|
79
|
+
s.add_dependency(%q<mailbox>, [">= 0"])
|
80
|
+
s.add_dependency(%q<timecop>, [">= 0"])
|
81
|
+
s.add_dependency(%q<rspec>, [">= 2.11.0"])
|
82
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
83
|
+
s.add_dependency(%q<bundler>, ["> 1.0.0"])
|
84
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path '../../spec_helper', __FILE__
|
2
|
+
|
3
|
+
module Pinky
|
4
|
+
describe Associations do
|
5
|
+
class Employee; end
|
6
|
+
class FooBar; end
|
7
|
+
class Dude; end
|
8
|
+
member_klass = Class.new do
|
9
|
+
extend Associations
|
10
|
+
|
11
|
+
has_one Employee
|
12
|
+
has_one FooBar, :lookup_by => :employee_id
|
13
|
+
has_one Dude, :as => :the_dude
|
14
|
+
|
15
|
+
attr_reader :employee_id
|
16
|
+
def initialize employee_id
|
17
|
+
@employee_id = employee_id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
it 'looks up employee by employee_id' do
|
23
|
+
member = member_klass.new 123
|
24
|
+
Employee.should_receive(:find).with(123)
|
25
|
+
FooBar.should_not_receive(:find)
|
26
|
+
|
27
|
+
member.employee
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'looks up FooBar by employee_id' do
|
31
|
+
member = member_klass.new 999
|
32
|
+
FooBar.should_receive(:find).with(999)
|
33
|
+
Employee.should_not_receive(:find)
|
34
|
+
|
35
|
+
member.foobar
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'allows for defining the association method name' do
|
39
|
+
member = member_klass.new 999
|
40
|
+
member.respond_to?(:dude).should be_false
|
41
|
+
member.respond_to?(:the_dude).should be_true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path '../../spec_helper', __FILE__
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module Pinky
|
5
|
+
describe CachableModel do
|
6
|
+
member_klass = Class.new do
|
7
|
+
extend Pinky::CachableModel
|
8
|
+
natural_key :id
|
9
|
+
|
10
|
+
def self.cache_hash; cache end
|
11
|
+
|
12
|
+
def initialize hash; @hash = hash end
|
13
|
+
def id; @hash[:id] end
|
14
|
+
def last_name; @hash[:last_name] end
|
15
|
+
end
|
16
|
+
|
17
|
+
before do
|
18
|
+
@hash = {
|
19
|
+
:id => 4255,
|
20
|
+
:token => 'fakeToken123',
|
21
|
+
:first_name => 'Joel',
|
22
|
+
:last_name => 'Friedman',
|
23
|
+
:email => 'joel@example'
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
after { member_klass.clear_cache }
|
28
|
+
|
29
|
+
context 'udpate_cache' do
|
30
|
+
it 'adds a new item to cache if action is :create' do
|
31
|
+
member_klass.update_cache_with @hash, :create
|
32
|
+
member_klass.cache_hash.key? 4255
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'updates the entry if action is :update' do
|
36
|
+
member_klass.cache_hash['4255'] = member_klass.new(@hash)
|
37
|
+
member_klass.update_cache_with @hash.merge(:last_name => 'Foster'), :update
|
38
|
+
|
39
|
+
member_klass.cache_hash.keys.should == ['4255']
|
40
|
+
member_klass.cache_hash['4255'].last_name.should == 'Foster'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'removes the entry if action is :destroy' do
|
44
|
+
member_klass.cache_hash['4255'] = member_klass.new(@hash)
|
45
|
+
member_klass.update_cache_with @hash, :destroy
|
46
|
+
|
47
|
+
member_klass.cache_hash.empty?.should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.expand_path '../../spec_helper', __FILE__
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module Pinky
|
5
|
+
describe ModelFetchMethods do
|
6
|
+
member_klass = Class.new do
|
7
|
+
extend Pinky::ModelFetchMethods
|
8
|
+
natural_key :id
|
9
|
+
fetch_url 'http://fake.com/member?id=:natural_key', :response_key => 'members',
|
10
|
+
:headers => { 'Accept' => 'version=1' },
|
11
|
+
:query => { 'token' => '123' }
|
12
|
+
|
13
|
+
def initialize hash; @hash = hash end
|
14
|
+
def id; @hash['id'] end
|
15
|
+
end
|
16
|
+
|
17
|
+
before do
|
18
|
+
@response = {
|
19
|
+
:success => true,
|
20
|
+
:response => {
|
21
|
+
:members => [
|
22
|
+
{:id => 4255, :token => 'fakeToken123', :first_name => 'Joel', :last_name => 'Friedman', :email => 'joel@example' }
|
23
|
+
]
|
24
|
+
},
|
25
|
+
:errors => nil
|
26
|
+
}.to_json
|
27
|
+
end
|
28
|
+
|
29
|
+
context '#find' do
|
30
|
+
before do
|
31
|
+
url = 'http://fake.com/member?id=4255'
|
32
|
+
HTTParty.should_receive(:get).with(url,
|
33
|
+
:headers => { 'Accept' => 'version=1' },
|
34
|
+
:query => { 'token' => '123' }
|
35
|
+
).once.and_return(stub(:body => @response))
|
36
|
+
end
|
37
|
+
|
38
|
+
after { member_klass.clear_cache }
|
39
|
+
|
40
|
+
it 'not raise an exception' do
|
41
|
+
expect { member_klass.find 4255 }.to_not raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'caches the object' do
|
45
|
+
member_klass.find 4255
|
46
|
+
member_klass.find 4255
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path '../../spec_helper', __FILE__
|
2
|
+
|
3
|
+
module Pinky
|
4
|
+
describe ModelNaturalKeyMethods do
|
5
|
+
|
6
|
+
context 'with natural_key declared' do
|
7
|
+
klass = Class.new do
|
8
|
+
include Pinky::ModelNaturalKeyMethods
|
9
|
+
natural_key :id, :foo
|
10
|
+
|
11
|
+
attr_reader :id, :foo, :bar
|
12
|
+
def initialize id, foo, bar
|
13
|
+
@id, @foo, @bar = id, foo, bar
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
it 'should create a natural key' do
|
19
|
+
instance = klass.new(123, 'hey', 'world')
|
20
|
+
instance.natural_key.should == '123.hey'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'can change the key separator' do
|
24
|
+
klass.natural_key_separator '---'
|
25
|
+
instance = klass.new(123, 'hey', 'world')
|
26
|
+
instance.natural_key.should == '123---hey'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'without natural_key declared' do
|
31
|
+
klass = Class.new do
|
32
|
+
include Pinky::ModelNaturalKeyMethods
|
33
|
+
|
34
|
+
attr_reader :id
|
35
|
+
def initialize
|
36
|
+
@id = 123
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error' do
|
41
|
+
instance = klass.new
|
42
|
+
expect { instance.natural_key }.to raise_error(Exception)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path '../../spec_helper', __FILE__
|
2
|
+
|
3
|
+
module Pinky
|
4
|
+
describe Model do
|
5
|
+
member_klass = Class.new do
|
6
|
+
include Pinky::Model
|
7
|
+
natural_key :id
|
8
|
+
|
9
|
+
def full_name
|
10
|
+
"#{first_name} #{last_name}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
@hash = {
|
16
|
+
:id => 4255,
|
17
|
+
:token => 'fakeToken123',
|
18
|
+
:first_name => 'Joel',
|
19
|
+
:last_name => 'Friedman',
|
20
|
+
:email => 'joel@example.com'
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'initialization' do
|
25
|
+
before do
|
26
|
+
@member = member_klass.new @hash
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'can be reconstructed from a hash' do
|
30
|
+
expect { member_klass.new @hash }.to_not raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should respond to token' do
|
34
|
+
@member.respond_to?(:token).should be_true
|
35
|
+
@member.token.should == 'fakeToken123'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'allows for methods to be created from attributes' do
|
39
|
+
@member.full_name.should == 'Joel Friedman'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'knows when it was created in the the cache' do
|
43
|
+
now = DateTime.new 2012, 05, 12, 06, 30, 22
|
44
|
+
|
45
|
+
Timecop.freeze(now) do
|
46
|
+
member = member_klass.new @hash
|
47
|
+
member.cached_at.should == now
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'rspec'
|
12
|
+
require 'timecop'
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
+
require 'pinky'
|
17
|
+
|
18
|
+
module Pinky
|
19
|
+
module ObjectExtensions
|
20
|
+
def dbg_msg label, x, location
|
21
|
+
"#{label} from #{location}:\n #{x.respond_to?(:pretty_inspect) ? x.pretty_inspect : x.inspect}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def dbg_type x
|
25
|
+
dbg x.class.name, "type of #{x}", caller[0]
|
26
|
+
x
|
27
|
+
end
|
28
|
+
|
29
|
+
def dbg_puts x
|
30
|
+
puts x
|
31
|
+
x
|
32
|
+
end
|
33
|
+
|
34
|
+
def dbg x, label = 'value', context = nil
|
35
|
+
context ||= caller[0]
|
36
|
+
puts dbg_msg(label, x, context)
|
37
|
+
x
|
38
|
+
end
|
39
|
+
|
40
|
+
def dbgv x, label = 'value'
|
41
|
+
dbg x, "*********************************** #{label}"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Object
|
48
|
+
include Pinky::ObjectExtensions
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pinky
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Joel Friedman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
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: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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: hot_bunnies
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
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: mailbox
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
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: timecop
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '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: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 2.11.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.11.0
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rdoc
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.12'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '3.12'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: bundler
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>'
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 1.0.0
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>'
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 1.0.0
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: jeweler
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ~>
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 1.8.4
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 1.8.4
|
158
|
+
description: Cache your api objects in member easier, with associations.
|
159
|
+
email: asher.friedman@gmail.com
|
160
|
+
executables: []
|
161
|
+
extensions: []
|
162
|
+
extra_rdoc_files:
|
163
|
+
- LICENSE.txt
|
164
|
+
- README.md
|
165
|
+
files:
|
166
|
+
- .document
|
167
|
+
- .rvmrc
|
168
|
+
- Gemfile
|
169
|
+
- Gemfile.lock
|
170
|
+
- LICENSE.txt
|
171
|
+
- README.md
|
172
|
+
- Rakefile
|
173
|
+
- VERSION
|
174
|
+
- lib/pinky.rb
|
175
|
+
- lib/pinky/associations.rb
|
176
|
+
- lib/pinky/cachable_model.rb
|
177
|
+
- lib/pinky/energizer_bunny/connection.rb
|
178
|
+
- lib/pinky/energizer_bunny/subscription.rb
|
179
|
+
- lib/pinky/model.rb
|
180
|
+
- lib/pinky/model_fetch_methods.rb
|
181
|
+
- lib/pinky/model_natural_key_methods.rb
|
182
|
+
- pinky.gemspec
|
183
|
+
- spec/pinky/associations_spec.rb
|
184
|
+
- spec/pinky/cachable_model_spec.rb
|
185
|
+
- spec/pinky/model_fetch_methods_spec.rb
|
186
|
+
- spec/pinky/model_natural_key_methods_spec.rb
|
187
|
+
- spec/pinky/model_spec.rb
|
188
|
+
- spec/spec_helper.rb
|
189
|
+
homepage: http://github.com/trunkclub/pinky
|
190
|
+
licenses:
|
191
|
+
- MIT
|
192
|
+
post_install_message:
|
193
|
+
rdoc_options: []
|
194
|
+
require_paths:
|
195
|
+
- lib
|
196
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
197
|
+
none: false
|
198
|
+
requirements:
|
199
|
+
- - ! '>='
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
segments:
|
203
|
+
- 0
|
204
|
+
hash: 2326550898121456815
|
205
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
206
|
+
none: false
|
207
|
+
requirements:
|
208
|
+
- - ! '>='
|
209
|
+
- !ruby/object:Gem::Version
|
210
|
+
version: '0'
|
211
|
+
requirements: []
|
212
|
+
rubyforge_project:
|
213
|
+
rubygems_version: 1.8.24
|
214
|
+
signing_key:
|
215
|
+
specification_version: 3
|
216
|
+
summary: in memory API caching made easy
|
217
|
+
test_files: []
|