giant_bomb 0.0.1 → 0.1.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.
- data/VERSION +1 -1
- data/giant_bomb.gemspec +64 -0
- data/lib/giant_bomb/httparty_icebox.rb +259 -0
- data/lib/giant_bomb/search.rb +2 -2
- data/lib/giant_bomb.rb +1 -1
- data/test/giant_bomb_test.rb +3 -2
- metadata +3 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/giant_bomb.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
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 = %q{giant_bomb}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jon Maddox"]
|
12
|
+
s.date = %q{2009-10-21}
|
13
|
+
s.description = %q{Simple library to talkto the awesome GiantBomb data}
|
14
|
+
s.email = %q{jon@fanzter.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.textile",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"giant_bomb.gemspec",
|
27
|
+
"lib/giant_bomb.rb",
|
28
|
+
"lib/giant_bomb/game.rb",
|
29
|
+
"lib/giant_bomb/httparty_icebox.rb",
|
30
|
+
"lib/giant_bomb/search.rb",
|
31
|
+
"test/fixtures/search.json",
|
32
|
+
"test/fixtures/sketchy_results.json",
|
33
|
+
"test/giant_bomb_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/fanzter/giant_bomb}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{Simple library to talk to the awesome GiantBomb data}
|
41
|
+
s.test_files = [
|
42
|
+
"test/giant_bomb_test.rb",
|
43
|
+
"test/test_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.4.3"])
|
52
|
+
s.add_development_dependency(%q<fakeweb>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<httparty>, [">= 0.4.3"])
|
56
|
+
s.add_dependency(%q<fakeweb>, [">= 0"])
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<httparty>, [">= 0.4.3"])
|
61
|
+
s.add_dependency(%q<fakeweb>, [">= 0"])
|
62
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,259 @@
|
|
1
|
+
# = Icebox : Caching for HTTParty
|
2
|
+
#
|
3
|
+
# Cache responses in HTTParty models [http://github.com/jnunemaker/httparty]
|
4
|
+
#
|
5
|
+
# === Usage
|
6
|
+
#
|
7
|
+
# class Foo
|
8
|
+
# include HTTParty
|
9
|
+
# include HTTParty::Icebox
|
10
|
+
# cache :store => 'file', :timeout => 600, :location => MY_APP_ROOT.join('tmp', 'cache')
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Modeled after Martyn Loughran's APICache [http://github.com/newbamboo/api_cache]
|
14
|
+
# and Ruby On Rails's caching [http://api.rubyonrails.org/classes/ActiveSupport/Cache.html]
|
15
|
+
#
|
16
|
+
# Author: Karel Minarik [www.karmi.cz]
|
17
|
+
#
|
18
|
+
# === Notes
|
19
|
+
#
|
20
|
+
# Thanks to Amit Chakradeo to point out objects have to be stored marhalled on FS
|
21
|
+
# Thanks to Marlin Forbes to point out query parameters have to be include in the cache key
|
22
|
+
#
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'logger'
|
26
|
+
require 'ftools'
|
27
|
+
require 'tmpdir'
|
28
|
+
require 'pathname'
|
29
|
+
require 'digest/md5'
|
30
|
+
|
31
|
+
module HTTParty #:nodoc:
|
32
|
+
module Icebox
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
|
36
|
+
# Enable caching and set cache options
|
37
|
+
# Returns memoized cache object
|
38
|
+
#
|
39
|
+
# Following options are available, default values are in []:
|
40
|
+
#
|
41
|
+
# +store+:: Storage mechanism for cached data (memory, filesystem, your own) [memory]
|
42
|
+
# +timeout+:: Cache expiration in seconds [60]
|
43
|
+
# +logger+:: Path to logfile or logger instance [STDOUT]
|
44
|
+
#
|
45
|
+
# Any additional options are passed to the Cache constructor
|
46
|
+
#
|
47
|
+
# Usage:
|
48
|
+
#
|
49
|
+
# # Enable caching in HTTParty, in memory, for 1 minute
|
50
|
+
# cache # Use default values
|
51
|
+
#
|
52
|
+
# # Enable caching in HTTParty, on filesystem (/tmp), for 10 minutes
|
53
|
+
# cache :store => 'file', :timeout => 600, :location => '/tmp/'
|
54
|
+
#
|
55
|
+
# # Use your own cache store (see AbstractStore class below)
|
56
|
+
# cache :store => 'memcached', :timeout => 600, :server => '192.168.1.1:1001'
|
57
|
+
#
|
58
|
+
def cache(options={})
|
59
|
+
options[:store] ||= 'memory'
|
60
|
+
options[:timeout] ||= 60
|
61
|
+
logger = options[:logger]
|
62
|
+
@cache ||= Cache.new( options.delete(:store), options )
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
# When included, extend class with +cache+ method
|
68
|
+
# and redefine +get+ method to use cache
|
69
|
+
#
|
70
|
+
def self.included(receiver) #:nodoc:
|
71
|
+
receiver.extend ClassMethods
|
72
|
+
receiver.class_eval do
|
73
|
+
|
74
|
+
# Get reponse from network
|
75
|
+
# TODO: Why alias :new :old is not working here? Returns NoMethodError
|
76
|
+
#
|
77
|
+
def self.get_without_caching(path, options={})
|
78
|
+
perform_request Net::HTTP::Get, path, options
|
79
|
+
end
|
80
|
+
|
81
|
+
# Get response from cache, if available
|
82
|
+
#
|
83
|
+
def self.get_with_caching(path, options={})
|
84
|
+
key = path.clone
|
85
|
+
key << options[:query].to_s if defined? options[:query]
|
86
|
+
|
87
|
+
if cache.exists?(key) and not cache.stale?(key)
|
88
|
+
Cache.logger.debug "CACHE -- GET #{path}#{options[:query]}"
|
89
|
+
return cache.get(key)
|
90
|
+
else
|
91
|
+
Cache.logger.debug "/!\\ NETWORK -- GET #{path}#{options[:query]}"
|
92
|
+
response = get_without_caching(path, options)
|
93
|
+
cache.set(key, response) if response.code == 200
|
94
|
+
return response
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Redefine original HTTParty +get+ method to use cache
|
99
|
+
#
|
100
|
+
def self.get(path, options={})
|
101
|
+
self.get_with_caching(path, options)
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# === Cache container
|
108
|
+
#
|
109
|
+
# Pass a store name ('memory', etc) to initializer
|
110
|
+
#
|
111
|
+
class Cache
|
112
|
+
attr_accessor :store
|
113
|
+
|
114
|
+
def initialize(store, options={})
|
115
|
+
self.class.logger = options[:logger]
|
116
|
+
@store = self.class.lookup_store(store).new(options)
|
117
|
+
end
|
118
|
+
|
119
|
+
def get(key); @store.get encode(key) unless stale?(key); end
|
120
|
+
def set(key, value); @store.set encode(key), value; end
|
121
|
+
def exists?(key); @store.exists? encode(key); end
|
122
|
+
def stale?(key); @store.stale? encode(key); end
|
123
|
+
|
124
|
+
def self.logger; @logger || default_logger; end
|
125
|
+
def self.default_logger; logger = ::Logger.new(STDERR); end
|
126
|
+
|
127
|
+
# Pass a filename (String), IO object, Logger instance or +nil+ to silence the logger
|
128
|
+
def self.logger=(device); @logger = device.kind_of?(::Logger) ? device : ::Logger.new(device); end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
# Return store class based on passed name
|
133
|
+
def self.lookup_store(name)
|
134
|
+
store_name = "#{name.capitalize}Store"
|
135
|
+
return Store::const_get(store_name)
|
136
|
+
rescue NameError => e
|
137
|
+
raise Store::StoreNotFound, "The cache store '#{store_name}' was not found. Did you loaded any such class?"
|
138
|
+
end
|
139
|
+
|
140
|
+
def encode(key); Digest::MD5.hexdigest(key); end
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
# === Cache stores
|
145
|
+
#
|
146
|
+
module Store
|
147
|
+
|
148
|
+
class StoreNotFound < StandardError; end #:nodoc:
|
149
|
+
|
150
|
+
# ==== Abstract Store
|
151
|
+
# Inherit your store from this class
|
152
|
+
# *IMPORTANT*: Do not forget to call +super+ in your +initialize+ method!
|
153
|
+
#
|
154
|
+
class AbstractStore
|
155
|
+
def initialize(options={})
|
156
|
+
raise ArgumentError, "You need to set the :timeout parameter" unless options[:timeout]
|
157
|
+
@timeout = options[:timeout]
|
158
|
+
message = "Cache: Using #{self.class.to_s.split('::').last}"
|
159
|
+
message << " in location: #{options[:location]}" if options[:location]
|
160
|
+
message << " with timeout #{options[:timeout]} sec"
|
161
|
+
Cache.logger.info message unless options[:logger].nil?
|
162
|
+
return self
|
163
|
+
end
|
164
|
+
%w{set get exists? stale?}.each do |method_name|
|
165
|
+
define_method(method_name) { raise NoMethodError, "Please implement method set in your store class" }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# ===== Store objects in memory
|
170
|
+
#
|
171
|
+
Struct.new("Response", :code, :body, :headers) { def to_s; self.body; end }
|
172
|
+
class MemoryStore < AbstractStore
|
173
|
+
def initialize(options={})
|
174
|
+
super; @store = {}; self
|
175
|
+
end
|
176
|
+
def set(key, value)
|
177
|
+
Cache.logger.info("Cache: set (#{key})")
|
178
|
+
@store[key] = [Time.now, value]; true
|
179
|
+
end
|
180
|
+
def get(key)
|
181
|
+
data = @store[key][1]
|
182
|
+
Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
|
183
|
+
data
|
184
|
+
end
|
185
|
+
def exists?(key)
|
186
|
+
!@store[key].nil?
|
187
|
+
end
|
188
|
+
def stale?(key)
|
189
|
+
return true unless exists?(key)
|
190
|
+
Time.now - created(key) > @timeout
|
191
|
+
end
|
192
|
+
private
|
193
|
+
def created(key)
|
194
|
+
@store[key][0]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# ===== Store objects on the filesystem
|
199
|
+
#
|
200
|
+
class FileStore < AbstractStore
|
201
|
+
def initialize(options={})
|
202
|
+
super
|
203
|
+
options[:location] ||= Dir::tmpdir
|
204
|
+
@path = Pathname.new( options[:location] )
|
205
|
+
FileUtils.mkdir_p( @path )
|
206
|
+
self
|
207
|
+
end
|
208
|
+
def set(key, value)
|
209
|
+
Cache.logger.info("Cache: set (#{key})")
|
210
|
+
File.open( @path.join(key), 'w' ) { |file| file << Marshal.dump(value) }
|
211
|
+
true
|
212
|
+
end
|
213
|
+
def get(key)
|
214
|
+
data = Marshal.load(File.read( @path.join(key)))
|
215
|
+
Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
|
216
|
+
data
|
217
|
+
end
|
218
|
+
def exists?(key)
|
219
|
+
File.exists?( @path.join(key) )
|
220
|
+
end
|
221
|
+
def stale?(key)
|
222
|
+
return true unless exists?(key)
|
223
|
+
Time.now - created(key) > @timeout
|
224
|
+
end
|
225
|
+
private
|
226
|
+
def created(key)
|
227
|
+
File.mtime( @path.join(key) )
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
# Major parts of this code are based on architecture of ApiCache.
|
237
|
+
# Copyright (c) 2008 Martyn Loughran
|
238
|
+
#
|
239
|
+
# Other parts are inspired by the ActiveSupport::Cache in Ruby On Rails.
|
240
|
+
# Copyright (c) 2005-2009 David Heinemeier Hansson
|
241
|
+
#
|
242
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
243
|
+
# a copy of this software and associated documentation files (the
|
244
|
+
# "Software"), to deal in the Software without restriction, including
|
245
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
246
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
247
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
248
|
+
# the following conditions:
|
249
|
+
#
|
250
|
+
# The above copyright notice and this permission notice shall be
|
251
|
+
# included in all copies or substantial portions of the Software.
|
252
|
+
#
|
253
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
254
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
255
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
256
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
257
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
258
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
259
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/giant_bomb/search.rb
CHANGED
@@ -2,8 +2,8 @@ module GiantBomb
|
|
2
2
|
class Search
|
3
3
|
include HTTParty
|
4
4
|
format :json
|
5
|
-
|
6
|
-
|
5
|
+
include HTTParty::Icebox
|
6
|
+
cache :store => 'file', :timeout => 120, :location => Dir.tmpdir
|
7
7
|
|
8
8
|
base_uri 'api.giantbomb.com'
|
9
9
|
|
data/lib/giant_bomb.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
3
|
directory = File.expand_path(File.dirname(__FILE__))
|
4
|
-
|
4
|
+
require File.join(directory, 'giant_bomb', 'httparty_icebox')
|
5
5
|
require File.join(directory, 'giant_bomb', 'search')
|
6
6
|
require File.join(directory, 'giant_bomb', 'game')
|
data/test/giant_bomb_test.rb
CHANGED
@@ -79,8 +79,9 @@ class GiantBombTest < Test::Unit::TestCase
|
|
79
79
|
|
80
80
|
context 'with sketchy data' do
|
81
81
|
setup do
|
82
|
-
stub_get('/search/?api_key=key&query=
|
83
|
-
@game = @gb.find_game('
|
82
|
+
stub_get('/search/?api_key=key&query=halo2&resources=game&format=json', 'sketchy_results.json')
|
83
|
+
@game = @gb.find_game('halo2').first
|
84
|
+
puts @game.name
|
84
85
|
end
|
85
86
|
|
86
87
|
should 'handle not having date_added' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: giant_bomb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Maddox
|
@@ -58,8 +58,10 @@ files:
|
|
58
58
|
- README.textile
|
59
59
|
- Rakefile
|
60
60
|
- VERSION
|
61
|
+
- giant_bomb.gemspec
|
61
62
|
- lib/giant_bomb.rb
|
62
63
|
- lib/giant_bomb/game.rb
|
64
|
+
- lib/giant_bomb/httparty_icebox.rb
|
63
65
|
- lib/giant_bomb/search.rb
|
64
66
|
- test/fixtures/search.json
|
65
67
|
- test/fixtures/sketchy_results.json
|