imdb_party 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/imdb_party.gemspec +26 -33
- data/lib/imdb_party.rb +0 -1
- data/lib/imdb_party/imdb.rb +2 -3
- data/lib/imdb_party/movie.rb +2 -1
- data/test/movie_test.rb +22 -23
- data/test/trailer_test.rb +16 -0
- metadata +48 -64
- data/lib/imdb_party/httparty_icebox.rb +0 -259
data/README.md
CHANGED
@@ -20,6 +20,12 @@
|
|
20
20
|
movie.rating => 8.1
|
21
21
|
movie.certification => "PG-13"
|
22
22
|
|
23
|
+
### Get a movie trailer poster
|
24
|
+
|
25
|
+
movie = imdb.find_movie_by_id("tt1210166")
|
26
|
+
|
27
|
+
movie.trailer_url => "http://ia.media-imdb.com/images/M/MV5BODM1NDMxMTI3M15BMl5BanBnXkFtZTcwMDAzODY1Ng@@._V1_.jpg"
|
28
|
+
|
23
29
|
### Find the top 250 movies of all time
|
24
30
|
|
25
31
|
imdb.top_250 => [{:title => "Shawshank Redemption", :year => "1994", :imdb_id => "tt0111161"}, {:title => "The Godfather", ...}]
|
@@ -27,3 +33,15 @@
|
|
27
33
|
### Get the currently popular tv shows
|
28
34
|
|
29
35
|
imdb.popular_shows => [{:title => "Glee", :year => "2009", :imdb_id => "tt1327801"}, {:title => "Dexter", ...}]
|
36
|
+
|
37
|
+
## Running the tests
|
38
|
+
|
39
|
+
### Install dependencies
|
40
|
+
|
41
|
+
gem install jeweler
|
42
|
+
gem install shoulda
|
43
|
+
gem install httparty
|
44
|
+
|
45
|
+
### Run the tests
|
46
|
+
|
47
|
+
rake test
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/imdb_party.gemspec
CHANGED
@@ -1,50 +1,42 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "imdb_party"
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jon Maddox"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2013-07-17"
|
13
|
+
s.description = "IMDB client using the IMDB API that their iPhone app uses"
|
14
|
+
s.email = "jon@mustacheinc.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
"lib/imdb_party/movie.rb",
|
31
|
-
"lib/imdb_party/person.rb",
|
32
|
-
"test/movie_test.rb",
|
33
|
-
"test/person_test.rb",
|
34
|
-
"test/search_test.rb",
|
35
|
-
"test/test_helper.rb"
|
36
|
-
]
|
37
|
-
s.homepage = %q{http://github.com/maddox/imdb_party}
|
38
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
-
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version = %q{1.4.2}
|
41
|
-
s.summary = %q{IMDB client using the IMDB API that their iPhone app uses}
|
42
|
-
s.test_files = [
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"imdb_party.gemspec",
|
26
|
+
"lib/imdb_party.rb",
|
27
|
+
"lib/imdb_party/imdb.rb",
|
28
|
+
"lib/imdb_party/movie.rb",
|
29
|
+
"lib/imdb_party/person.rb",
|
43
30
|
"test/movie_test.rb",
|
44
|
-
|
45
|
-
|
46
|
-
|
31
|
+
"test/person_test.rb",
|
32
|
+
"test/search_test.rb",
|
33
|
+
"test/test_helper.rb",
|
34
|
+
"test/trailer_test.rb"
|
47
35
|
]
|
36
|
+
s.homepage = "http://github.com/maddox/imdb_party"
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = "1.8.23"
|
39
|
+
s.summary = "IMDB client using the IMDB API that their iPhone app uses"
|
48
40
|
|
49
41
|
if s.respond_to? :specification_version then
|
50
42
|
s.specification_version = 3
|
@@ -61,3 +53,4 @@ Gem::Specification.new do |s|
|
|
61
53
|
s.add_dependency(%q<httparty>, [">= 0"])
|
62
54
|
end
|
63
55
|
end
|
56
|
+
|
data/lib/imdb_party.rb
CHANGED
@@ -2,7 +2,6 @@ require 'rubygems'
|
|
2
2
|
require 'httparty'
|
3
3
|
|
4
4
|
directory = File.expand_path(File.dirname(__FILE__))
|
5
|
-
require File.join(directory, 'imdb_party', 'httparty_icebox')
|
6
5
|
require File.join(directory, 'imdb_party', 'imdb')
|
7
6
|
require File.join(directory, 'imdb_party', 'movie')
|
8
7
|
require File.join(directory, 'imdb_party', 'person')
|
data/lib/imdb_party/imdb.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module ImdbParty
|
2
2
|
class Imdb
|
3
3
|
include HTTParty
|
4
|
-
|
5
|
-
|
4
|
+
# include HTTParty::Icebox
|
5
|
+
# cache :store => 'file', :timeout => 120, :location => Dir.tmpdir
|
6
6
|
|
7
7
|
base_uri 'app.imdb.com'
|
8
8
|
format :json
|
@@ -36,7 +36,6 @@ module ImdbParty
|
|
36
36
|
movie_results = []
|
37
37
|
|
38
38
|
results = self.class.get("http://www.imdb.com/xml/find", :query => default_find_by_title_params).parsed_response
|
39
|
-
puts results.inspect
|
40
39
|
|
41
40
|
keys = ["title_popular", "title_exact", "title_approx", "title_substring"]
|
42
41
|
|
data/lib/imdb_party/movie.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module ImdbParty
|
2
2
|
class Movie
|
3
|
-
attr_accessor :imdb_id, :title, :directors, :writers, :tagline, :company, :plot, :runtime, :rating, :poster_url, :release_date, :certification, :genres, :actors, :trailers
|
3
|
+
attr_accessor :imdb_id, :title, :directors, :writers, :tagline, :company, :plot, :runtime, :rating, :poster_url, :release_date, :certification, :genres, :actors, :trailers, :trailer_url
|
4
4
|
|
5
5
|
def initialize(options={})
|
6
6
|
@imdb_id = options["tconst"]
|
@@ -13,6 +13,7 @@ module ImdbParty
|
|
13
13
|
@release_date = options["release_date"]["normal"] if options["release_date"] && options["release_date"]["normal"]
|
14
14
|
@certification = options["certificate"]["certificate"] if options["certificate"] && options["certificate"]["certificate"]
|
15
15
|
@genres = options["genres"] || []
|
16
|
+
@trailer_url = options["trailer"]["slates"][0]["url"] if(options["trailer"] && options["trailer"]["slates"] && options["trailer"]["slates"][0])
|
16
17
|
|
17
18
|
# parse directors
|
18
19
|
@directors = options["directors_summary"] ? options["directors_summary"].map { |d| Person.new(d) } : []
|
data/test/movie_test.rb
CHANGED
@@ -6,73 +6,72 @@ class MovieTest < Test::Unit::TestCase
|
|
6
6
|
@imdb = ImdbParty::Imdb.new
|
7
7
|
@movie = @imdb.find_movie_by_id("tt0382932")
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
should "have a title" do
|
11
11
|
assert_equal "Ratatouille", @movie.title
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
should "have an imdb_id" do
|
15
15
|
assert_equal "tt0382932", @movie.imdb_id
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
should "have a tagline" do
|
19
19
|
assert_equal "Dinner is served... Summer 2007", @movie.tagline
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
should "have a plot" do
|
23
|
-
assert_equal "
|
23
|
+
assert_equal "With dreams of becoming a chef, a culinary genius in the form of a rat, makes an unusual alliance with a young kitchen worker at a famed restaurant.", @movie.plot
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
should "have a runtime" do
|
27
27
|
assert_equal "111 min", @movie.runtime
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
should "have a rating" do
|
31
|
-
assert_equal 8
|
31
|
+
assert_equal 8, @movie.rating
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
should "have a poster_url" do
|
35
35
|
assert_match /http:\/\/ia.media-imdb.com\/images\/.*/, @movie.poster_url
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
should "have a release date" do
|
39
|
-
assert_equal
|
39
|
+
assert_equal "2007-06-29", @movie.release_date
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
should "have a certification" do
|
43
43
|
assert_equal "G", @movie.certification
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
should "have trailers" do
|
47
47
|
assert_equal Hash, @movie.trailers.class
|
48
|
-
assert_equal
|
49
|
-
assert_equal "http://www.totaleclips.com/Player/Bounce.aspx?eclipid=e27826&bitrateid=461&vendorid=102&type=.mp4", @movie.trailers[@movie.trailers.keys.first]
|
48
|
+
assert_equal 0, @movie.trailers.keys.size
|
50
49
|
end
|
51
|
-
|
50
|
+
|
52
51
|
should "have genres" do
|
53
52
|
assert_equal Array, @movie.genres.class
|
54
53
|
assert_equal 4, @movie.genres.size
|
55
54
|
end
|
56
|
-
|
55
|
+
|
57
56
|
should "have actors" do
|
58
57
|
assert_equal Array, @movie.actors.class
|
59
58
|
assert_equal 4, @movie.actors.size
|
60
59
|
end
|
61
|
-
|
60
|
+
|
62
61
|
should "have directors" do
|
63
62
|
assert_equal Array, @movie.directors.class
|
64
63
|
assert_equal 2, @movie.directors.size
|
65
64
|
end
|
66
|
-
|
65
|
+
|
67
66
|
should "have writers" do
|
68
67
|
assert_equal Array, @movie.writers.class
|
69
68
|
assert_equal 2, @movie.writers.size
|
70
69
|
end
|
71
|
-
|
70
|
+
|
72
71
|
end
|
73
|
-
|
72
|
+
|
74
73
|
context "a movie while anonymized" do
|
75
|
-
setup do
|
74
|
+
setup do
|
76
75
|
@imdb = ImdbParty::Imdb.new(:anonymize => true)
|
77
76
|
@movie = @imdb.find_movie_by_id("tt0382932")
|
78
77
|
end
|
@@ -81,5 +80,5 @@ class MovieTest < Test::Unit::TestCase
|
|
81
80
|
assert_equal "Ratatouille", @movie.title
|
82
81
|
end
|
83
82
|
end
|
84
|
-
|
83
|
+
|
85
84
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TrailerTest < Test::Unit::TestCase
|
4
|
+
context "a trailer" do
|
5
|
+
setup do
|
6
|
+
@imdb = ImdbParty::Imdb.new
|
7
|
+
@movie = @imdb.find_movie_by_id("tt1210166")
|
8
|
+
end
|
9
|
+
|
10
|
+
should "have a trailer_url" do
|
11
|
+
assert_match /http:\/\/ia.media-imdb.com\/images\/.*/, @movie.trailer_url
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
metadata
CHANGED
@@ -1,61 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: imdb_party
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
- 1
|
10
|
-
version: 0.7.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Jon Maddox
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: shoulda
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
33
22
|
type: :development
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: httparty
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
25
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httparty
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
47
38
|
type: :runtime
|
48
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
49
46
|
description: IMDB client using the IMDB API that their iPhone app uses
|
50
47
|
email: jon@mustacheinc.com
|
51
48
|
executables: []
|
52
|
-
|
53
49
|
extensions: []
|
54
|
-
|
55
|
-
extra_rdoc_files:
|
50
|
+
extra_rdoc_files:
|
56
51
|
- LICENSE
|
57
52
|
- README.md
|
58
|
-
files:
|
53
|
+
files:
|
59
54
|
- .document
|
60
55
|
- LICENSE
|
61
56
|
- README.md
|
@@ -63,7 +58,6 @@ files:
|
|
63
58
|
- VERSION
|
64
59
|
- imdb_party.gemspec
|
65
60
|
- lib/imdb_party.rb
|
66
|
-
- lib/imdb_party/httparty_icebox.rb
|
67
61
|
- lib/imdb_party/imdb.rb
|
68
62
|
- lib/imdb_party/movie.rb
|
69
63
|
- lib/imdb_party/person.rb
|
@@ -71,39 +65,29 @@ files:
|
|
71
65
|
- test/person_test.rb
|
72
66
|
- test/search_test.rb
|
73
67
|
- test/test_helper.rb
|
74
|
-
|
68
|
+
- test/trailer_test.rb
|
75
69
|
homepage: http://github.com/maddox/imdb_party
|
76
70
|
licenses: []
|
77
|
-
|
78
71
|
post_install_message:
|
79
72
|
rdoc_options: []
|
80
|
-
|
81
|
-
require_paths:
|
73
|
+
require_paths:
|
82
74
|
- lib
|
83
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
76
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
|
89
|
-
|
90
|
-
- 0
|
91
|
-
version: "0"
|
92
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
82
|
none: false
|
94
|
-
requirements:
|
95
|
-
- -
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
segments:
|
99
|
-
- 0
|
100
|
-
version: "0"
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
101
87
|
requirements: []
|
102
|
-
|
103
88
|
rubyforge_project:
|
104
|
-
rubygems_version: 1.
|
89
|
+
rubygems_version: 1.8.23
|
105
90
|
signing_key:
|
106
91
|
specification_version: 3
|
107
92
|
summary: IMDB client using the IMDB API that their iPhone app uses
|
108
93
|
test_files: []
|
109
|
-
|
@@ -1,259 +0,0 @@
|
|
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 'fileutils'
|
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("TvdbResponse", :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| Marshal.dump(value, file) }
|
211
|
-
true
|
212
|
-
end
|
213
|
-
def get(key)
|
214
|
-
data = Marshal.load(File.new(@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.
|