bartzon-zelda 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,7 @@
1
1
  = zelda
2
2
 
3
- * http://github.com/#{github_username}/#{project_name}
3
+ * http://github.com/bartzon/zelda
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- FIX (describe your package)
8
-
9
- == FEATURES/PROBLEMS:
10
-
11
- * FIX (list of features or problems)
12
-
13
- == SYNOPSIS:
14
-
15
- FIX (code sample of usage)
16
-
17
- == REQUIREMENTS:
18
-
19
- * FIX (list of requirements)
20
-
21
- == INSTALL:
22
-
23
- * FIX (sudo gem install, anything else)
24
-
25
- == LICENSE:
26
-
27
- (The MIT License)
28
-
29
- Copyright (c) 2009 FIXME full name
30
-
31
- Permission is hereby granted, free of charge, to any person obtaining
32
- a copy of this software and associated documentation files (the
33
- 'Software'), to deal in the Software without restriction, including
34
- without limitation the rights to use, copy, modify, merge, publish,
35
- distribute, sublicense, and/or sell copies of the Software, and to
36
- permit persons to whom the Software is furnished to do so, subject to
37
- the following conditions:
38
-
39
- The above copyright notice and this permission notice shall be
40
- included in all copies or substantial portions of the Software.
41
-
42
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ If you don't know what this is, you probably don't need it :).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
@@ -9,6 +9,7 @@ rescue LoadError
9
9
  require 'httparty'
10
10
  end
11
11
 
12
+ require 'zelda/base'
12
13
  require 'zelda/request'
13
14
  require 'zelda/zender'
14
15
  require 'zelda/omroep'
@@ -16,7 +17,7 @@ require 'zelda/serie'
16
17
  require 'zelda/aflevering'
17
18
 
18
19
  module Zelda
19
- VERSION = "0.0.2"
20
+ VERSION = "0.0.4"
20
21
 
21
22
  class ZeldaError < StandardError; end
22
23
  class ZeldaCollectionError < ZeldaError; end
@@ -1,15 +1,4 @@
1
1
  module Zelda
2
- class Aflevering
3
- def initialize(vars={})
4
- @attrs = vars
5
- end
6
-
7
- def method_missing(method, *args, &block)
8
- if @attrs[method]
9
- return @attrs[method]
10
- else
11
- super(method, *args, &block)
12
- end
13
- end
2
+ class Aflevering < Zelda::Base
14
3
  end
15
4
  end
@@ -0,0 +1,24 @@
1
+ module Zelda
2
+ # Abstract base class to provide common functionality of Zelda importer classes.
3
+ # including method_missing magic to turn an @attrs hash into getters.
4
+ class Base
5
+ def initialize(attrs={})
6
+ @attrs = attrs
7
+ end
8
+
9
+ def attributes
10
+ @attrs
11
+ end
12
+
13
+ # Try both string keys and symbol keys in that order.
14
+ def method_missing(method, *args, &block)
15
+ if @attrs[method.to_s]
16
+ return @attrs[method.to_s]
17
+ elsif @attrs[method]
18
+ return @attrs[method]
19
+ else
20
+ super(method, *args, &block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,8 +1,9 @@
1
1
  module Zelda
2
- class Omroep
2
+ class Omroep < Zelda::Base
3
3
  class << self
4
- def find(id)
5
- new Request.get("omroepen/#{id}")['omroep']
4
+ def find(slug)
5
+ attrs = Request.get("omroepen/#{slug}")['omroep'] rescue nil
6
+ attrs ? new(attrs) : nil
6
7
  end
7
8
 
8
9
  def all
@@ -10,24 +11,10 @@ module Zelda
10
11
  end
11
12
  end
12
13
 
13
- def initialize(attrs={})
14
- @attrs = attrs
15
- end
16
-
17
- def id
18
- @attrs['id']
19
- end
20
-
21
- def name
22
- @attrs['name']
23
- end
24
-
25
14
  def series
26
- objects = []
27
- Zelda::Request.get("omroepen/#{id}/series")['series'].each do |attrs|
28
- objects << Zelda::Serie.new(attrs)
15
+ Zelda::Request.get("omroepen/#{slug}/series")['omroep']['series']['serie'].map do |attrs|
16
+ Zelda::Serie.new(attrs)
29
17
  end
30
- objects
31
18
  end
32
19
  end
33
20
  end
@@ -1,7 +1,15 @@
1
1
  module Zelda
2
2
  class Request
3
3
  def self.get(url)
4
- HTTParty.get parse_url(url)
4
+ result = HTTParty.get parse_url(url)
5
+
6
+ if result
7
+ %w(created_at updated_at id).each do |key|
8
+ result.delete(key) if result[key]
9
+ end
10
+ end
11
+
12
+ result
5
13
  end
6
14
 
7
15
  def self.parse_url(*args)
@@ -1,12 +1,13 @@
1
1
  module Zelda
2
- class Serie
2
+ class Serie < Zelda::Base
3
3
  class << self
4
4
  def search(query)
5
5
  Request.get("series/search/#{query}")['series']
6
6
  end
7
7
 
8
8
  def find(id)
9
- new Request.get("series/#{id}")
9
+ attrs = Request.get("series/#{id}")['serie'] rescue nil
10
+ attrs ? new(attrs) : nil
10
11
  end
11
12
 
12
13
  def all
@@ -14,39 +15,36 @@ module Zelda
14
15
  end
15
16
  end
16
17
 
17
- def initialize(attrs={})
18
- @attrs = attrs
19
- end
20
-
21
- def name
22
- @attrs["serie"]["name"]
23
- end
24
-
25
- def serieid
26
- @attrs["serie"]["serieid"]
27
- end
28
-
29
- def afleveringen
30
- @afleveringen ||= get_afleveringen()
18
+ def afleveringen(van=nil, tot=nil)
19
+ get_afleveringen(:van => van, :tot => tot)
31
20
  end
32
21
 
33
22
  def upcoming_afleveringen
34
- @upcoming_afleveringen ||= get_afleveringen("upcoming")
23
+ get_afleveringen(:collection => "upcoming")
35
24
  end
36
25
 
37
26
  def past_afleveringen
38
- @past_afleveringen ||= get_afleveringen("past")
27
+ get_afleveringen(:collection => "past")
39
28
  end
40
29
 
41
30
  private
42
- def get_afleveringen(timestamp=nil)
43
- url = [ "series", serieid, "afleveringen", timestamp ].join("/")
31
+ def get_afleveringen(*args)
32
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
44
33
 
45
- objects = []
46
- Request.get(url)["afleveringen"].each do |vars|
47
- objects << Zelda::Aflevering.new(vars)
34
+ Request.get(url)["afleveringen"].map do |vars|
35
+ Zelda::Aflevering.new(vars)
36
+ end
37
+ end
38
+
39
+ def url
40
+ if collection = @options[:collection]
41
+ [ "series", serieid, "afleveringen", collection ].join("/")
42
+ elsif @options[:van] && @options[:tot]
43
+ van, tot = @options[:van], @options[:tot]
44
+ [ "series", serieid, "afleveringen" ].join("/") + "?van=#{van.to_i}&tot=#{tot.to_i}"
45
+ else
46
+ [ "series", serieid, "afleveringen" ].join("/")
48
47
  end
49
- objects
50
48
  end
51
49
  end
52
50
  end
@@ -1,8 +1,9 @@
1
1
  module Zelda
2
- class Zender
2
+ class Zender < Zelda::Base
3
3
  class << self
4
4
  def find(id)
5
- new Request.get("zenders/#{id}")
5
+ attrs = Request.get("zenders/#{id}")
6
+ attrs ? new(attrs) : nil
6
7
  end
7
8
 
8
9
  def all
@@ -10,10 +11,6 @@ module Zelda
10
11
  end
11
12
  end
12
13
 
13
- def initialize(attrs={})
14
- @attrs = attrs
15
- end
16
-
17
14
  def name
18
15
  @attrs["zender"]["name"]
19
16
  end
@@ -8,6 +8,10 @@ end
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + '/../lib')
10
10
 
11
+ # Requires supporting files with custom matchers and macros, etc,
12
+ # in ./support/ and its subdirectories.
13
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
14
+
11
15
  require 'zelda'
12
16
 
13
17
  module Zelda
@@ -0,0 +1,9 @@
1
+ shared_examples_for "a zelda base model" do
2
+ it "should respond to the attributes set in the constructor" do
3
+ @model.foo.should == 'bar'
4
+ end
5
+
6
+ it "should not respond to attributes not set in the constructor" do
7
+ lambda { @model.no_not_here_nope }.should raise_error(NoMethodError)
8
+ end
9
+ end
@@ -1,17 +1,13 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Zelda::Aflevering do
4
- it "should accept attributes when being initialized" do
5
- Zelda::Aflevering.new(:foo => "bar").should be_a(Zelda::Aflevering)
4
+ before do
5
+ @model = Zelda::Aflevering.new(:foo => "bar")
6
6
  end
7
7
 
8
- it "should respond to the attributes set in the constructor" do
9
- afl = Zelda::Aflevering.new(:foo => "bar")
10
- afl.foo.should == 'bar'
11
- end
8
+ it_should_behave_like 'a zelda base model'
12
9
 
13
- it "should not respond to attributes not set in the constructor" do
14
- afl = Zelda::Aflevering.new(:foo => "bar")
15
- lambda { afl.bar }.should raise_error(NoMethodError)
16
- end
10
+ it "should accept attributes when being initialized" do
11
+ Zelda::Aflevering.new(:foo => "bar").should be_a(Zelda::Aflevering)
12
+ end
17
13
  end
@@ -1,6 +1,12 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Zelda::Omroep do
4
+ before do
5
+ @model = Zelda::Omroep.new(:foo => "bar")
6
+ end
7
+
8
+ it_should_behave_like 'a zelda base model'
9
+
4
10
  describe "when retrieving a list of omroepen" do
5
11
  it "should call Zelda with the correct url" do
6
12
  Zelda::Request.should_receive(:get).with("omroepen")
@@ -10,16 +16,16 @@ describe Zelda::Omroep do
10
16
 
11
17
  describe "when retrieving a specific omroep" do
12
18
  before(:each) do
13
- omroep_attrs = { "omroep" => {"id" => "1", "name" => "KRO"} }
14
- Zelda::Request.stub!(:get).with("omroepen/1").and_return omroep_attrs
19
+ omroep_attrs = { "omroep" => {"slug" => "kro", "name" => "KRO"} }
20
+ Zelda::Request.stub!(:get).with("omroepen/kro").and_return omroep_attrs
15
21
  end
16
22
 
17
23
  def find_omroep
18
- Zelda::Omroep.find(1)
24
+ Zelda::Omroep.find('kro')
19
25
  end
20
26
 
21
27
  it "should call Zelda with the correct url" do
22
- Zelda::Request.should_receive(:get).with("omroepen/1")
28
+ Zelda::Request.should_receive(:get).with("omroepen/kro")
23
29
  find_omroep
24
30
  end
25
31
 
@@ -27,8 +33,13 @@ describe Zelda::Omroep do
27
33
  find_omroep.should be_a(Zelda::Omroep)
28
34
  end
29
35
 
30
- it "should return the correct id" do
31
- find_omroep.id.should == "1"
36
+ it "should return nil if the omroep cannot be found" do
37
+ Zelda::Request.stub!(:get).and_return nil
38
+ find_omroep.should be_nil
39
+ end
40
+
41
+ it "should return the correct slug" do
42
+ find_omroep.slug.should == "kro"
32
43
  end
33
44
 
34
45
  it "should return the correct name" do
@@ -38,9 +49,9 @@ describe Zelda::Omroep do
38
49
 
39
50
  describe "when retrieving the series for an omroep" do
40
51
  before(:each) do
41
- @omroep = Zelda::Omroep.new('id' => 1)
52
+ @omroep = Zelda::Omroep.new('slug' => 'kro')
42
53
 
43
- Zelda::Request.stub!(:get).and_return('series' => [{'foo' => 'bar'}])
54
+ Zelda::Request.stub!(:get).and_return('omroep' => {'series' => {'serie' => [{'foo' => 'bar'}]}})
44
55
  end
45
56
 
46
57
  def find_series
@@ -48,7 +59,7 @@ describe Zelda::Omroep do
48
59
  end
49
60
 
50
61
  it "should call Zelda with the correct url" do
51
- Zelda::Request.should_receive(:get).with("omroepen/1/series")
62
+ Zelda::Request.should_receive(:get).with("omroepen/kro/series")
52
63
  find_series
53
64
  end
54
65
 
@@ -2,10 +2,22 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Zelda::Request do
4
4
  it "should call HTTParty" do
5
- HTTParty.should_receive(:get).with("http://zelda.omroep.nl/12345/foo")
5
+ HTTParty.should_receive(:get).with("http://zelda.omroep.nl/12345/foo").and_return nil
6
6
  Zelda::Request.get('foo')
7
7
  end
8
8
 
9
+ it "should remove private attributes" do
10
+ attrs = { 'foo' => "bar", 'created_at' => "foo", 'updated_at' => "bar", 'id' => 10 }
11
+ HTTParty.stub!(:get).and_return(attrs)
12
+
13
+ attrs = Zelda::Request.get('foo')
14
+
15
+ attrs['created_at'].should be_nil
16
+ attrs['updated_at'].should be_nil
17
+ attrs['id'].should be_nil
18
+ attrs['foo'].should == 'bar'
19
+ end
20
+
9
21
  it "should raise an error without an API key" do
10
22
  api_key = Zelda.send(:remove_const, :API_KEY)
11
23
  lambda { Zelda::Request.get('foo') }.should raise_error("No Zelda::API_KEY specified")
@@ -29,10 +29,15 @@ describe Zelda::Serie do
29
29
  it "should return a new Zender" do
30
30
  find_serie.should be_a(Zelda::Serie)
31
31
  end
32
+
33
+ it "should return nil if the zender cannot be found" do
34
+ Zelda::Request.stub!(:get).and_return nil
35
+ find_serie.should be_nil
36
+ end
32
37
 
33
38
  it "should send the correct request when asked for afleveringen" do
34
39
  serie = find_serie
35
- Zelda::Request.should_receive(:get).with("series/POW_00243538/afleveringen/").and_return("afleveringen" => ["foo"])
40
+ Zelda::Request.should_receive(:get).with("series/POW_00243538/afleveringen").and_return("afleveringen" => ["foo"])
36
41
  serie.afleveringen.should == [@aflevering]
37
42
  end
38
43
 
@@ -27,6 +27,11 @@ describe Zelda::Zender do
27
27
  find_zender.should be_a(Zelda::Zender)
28
28
  end
29
29
 
30
+ it "should return nil if the zender cannot be found" do
31
+ Zelda::Request.stub!(:get).and_return nil
32
+ find_zender.should be_nil
33
+ end
34
+
30
35
  it "should return the correct name" do
31
36
  find_zender.name.should == "Nederland 1"
32
37
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{zelda}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bart Zonneveld"]
12
- s.date = %q{2009-08-26}
12
+ s.date = %q{2009-09-01}
13
13
  s.description = %q{ }
14
14
  s.email = %q{loop@superinfinite.com}
15
15
  s.extra_rdoc_files = [
@@ -20,12 +20,12 @@ Gem::Specification.new do |s|
20
20
  ".gitignore",
21
21
  "History.txt",
22
22
  "Manifest.txt",
23
- "PostInstall.txt",
24
23
  "README.rdoc",
25
24
  "Rakefile",
26
25
  "VERSION",
27
26
  "lib/zelda.rb",
28
27
  "lib/zelda/aflevering.rb",
28
+ "lib/zelda/base.rb",
29
29
  "lib/zelda/omroep.rb",
30
30
  "lib/zelda/request.rb",
31
31
  "lib/zelda/serie.rb",
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "script/generate",
36
36
  "spec/spec.opts",
37
37
  "spec/spec_helper.rb",
38
+ "spec/support/shared_examples.rb",
38
39
  "spec/zelda/aflevering_spec.rb",
39
40
  "spec/zelda/omroep_spec.rb",
40
41
  "spec/zelda/request_spec.rb",
@@ -48,10 +49,11 @@ Gem::Specification.new do |s|
48
49
  s.rdoc_options = ["--charset=UTF-8"]
49
50
  s.require_paths = ["lib"]
50
51
  s.rubyforge_project = %q{zelda}
51
- s.rubygems_version = %q{1.3.5}
52
+ s.rubygems_version = %q{1.3.4}
52
53
  s.summary = %q{}
53
54
  s.test_files = [
54
55
  "spec/spec_helper.rb",
56
+ "spec/support/shared_examples.rb",
55
57
  "spec/zelda/aflevering_spec.rb",
56
58
  "spec/zelda/omroep_spec.rb",
57
59
  "spec/zelda/request_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bartzon-zelda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bart Zonneveld
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-26 00:00:00 -07:00
12
+ date: 2009-09-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,12 +35,12 @@ files:
35
35
  - .gitignore
36
36
  - History.txt
37
37
  - Manifest.txt
38
- - PostInstall.txt
39
38
  - README.rdoc
40
39
  - Rakefile
41
40
  - VERSION
42
41
  - lib/zelda.rb
43
42
  - lib/zelda/aflevering.rb
43
+ - lib/zelda/base.rb
44
44
  - lib/zelda/omroep.rb
45
45
  - lib/zelda/request.rb
46
46
  - lib/zelda/serie.rb
@@ -50,6 +50,7 @@ files:
50
50
  - script/generate
51
51
  - spec/spec.opts
52
52
  - spec/spec_helper.rb
53
+ - spec/support/shared_examples.rb
53
54
  - spec/zelda/aflevering_spec.rb
54
55
  - spec/zelda/omroep_spec.rb
55
56
  - spec/zelda/request_spec.rb
@@ -87,6 +88,7 @@ specification_version: 3
87
88
  summary: ""
88
89
  test_files:
89
90
  - spec/spec_helper.rb
91
+ - spec/support/shared_examples.rb
90
92
  - spec/zelda/aflevering_spec.rb
91
93
  - spec/zelda/omroep_spec.rb
92
94
  - spec/zelda/request_spec.rb
@@ -1,7 +0,0 @@
1
-
2
- For more information on zelda, see http://zelda.rubyforge.org
3
-
4
- NOTE: Change this information in PostInstall.txt
5
- You can also delete it if you don't want it.
6
-
7
-