zelda 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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pleghm.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = zelda
2
+
3
+ * https://github.com/tilsammans/zelda
4
+
5
+ == DESCRIPTION:
6
+
7
+ If you don't know what this is, you probably don't need it :).
8
+
9
+
10
+ == INSTALLATION
11
+
12
+ In environment.rb:
13
+
14
+ config.gem 'zelda'
15
+
16
+ In Gemfile:
17
+
18
+ gem 'zelda'
19
+
20
+
21
+ == CONFIGURATION
22
+
23
+ Create <tt>config/initializers/zelda.rb</tt>:
24
+
25
+ Zelda::API_KEY = 'deadbeef'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,10 @@
1
+ module Zelda
2
+ class Aflevering < Zelda::Base
3
+ class << self
4
+ def find(id)
5
+ attrs = Request.get("afleveringen/#{id}")['aflevering'] rescue nil
6
+ attrs ? new(attrs) : nil
7
+ end
8
+ end
9
+ end
10
+ end
data/lib/zelda/base.rb ADDED
@@ -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
@@ -0,0 +1,20 @@
1
+ module Zelda
2
+ class Omroep < Zelda::Base
3
+ class << self
4
+ def find(slug)
5
+ attrs = Request.get("omroepen/#{slug}")['omroep'] rescue nil
6
+ attrs ? new(attrs) : nil
7
+ end
8
+
9
+ def all
10
+ Request.get("omroepen")['omroepen']
11
+ end
12
+ end
13
+
14
+ def series
15
+ Zelda::Request.get("omroepen/#{slug}/series")['omroep']['series']['serie'].map do |attrs|
16
+ Zelda::Serie.new(attrs)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Zelda
2
+ class Request
3
+ def self.get(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
13
+ end
14
+
15
+ def self.parse_url(*args)
16
+ raise "No Zelda::API_KEY specified" unless defined?(Zelda::API_KEY)
17
+ "http://zelda.omroep.nl/#{ Zelda::API_KEY }/#{ args.join('/') }"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,58 @@
1
+ module Zelda
2
+ class Serie < Zelda::Base
3
+ class << self
4
+ def search(query)
5
+ series = []
6
+ Request.get("series/search/#{query}")['series'].each do |attrs|
7
+ series << Serie.new(attrs)
8
+ end
9
+ series
10
+ end
11
+
12
+ def find(id)
13
+ attrs = Request.get("series/#{serie_id_nebo}")['serie'] rescue nil
14
+ attrs ? new(attrs) : nil
15
+ end
16
+
17
+ def all
18
+ series = []
19
+ Request.get("series")['series'].each do |attrs|
20
+ series << Serie.new(attrs)
21
+ end
22
+ series
23
+ end
24
+ end
25
+
26
+ def afleveringen(van=nil, tot=nil)
27
+ get_afleveringen(:van => van, :tot => tot)
28
+ end
29
+
30
+ def upcoming_afleveringen
31
+ get_afleveringen(:collection => "upcoming")
32
+ end
33
+
34
+ def past_afleveringen
35
+ get_afleveringen(:collection => "past")
36
+ end
37
+
38
+ private
39
+ def get_afleveringen(*args)
40
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
41
+
42
+ Request.get(url)["afleveringen"].map do |vars|
43
+ Zelda::Aflevering.new(vars)
44
+ end
45
+ end
46
+
47
+ def url
48
+ if collection = @options[:collection]
49
+ [ "series", serie_id_nebo, "afleveringen", collection ].join("/")
50
+ elsif @options[:van] && @options[:tot]
51
+ van, tot = @options[:van], @options[:tot]
52
+ [ "series", serie_id_nebo, "afleveringen" ].join("/") + "?van=#{van.to_i}&tot=#{tot.to_i}"
53
+ else
54
+ [ "series", serie_id_nebo, "afleveringen" ].join("/")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Zelda
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ module Zelda
2
+ class Zender < Zelda::Base
3
+ class << self
4
+ def find(id)
5
+ attrs = Request.get("zenders/#{id}")
6
+ attrs ? new(attrs) : nil
7
+ end
8
+
9
+ def all
10
+ Request.get("zenders")
11
+ end
12
+ end
13
+
14
+ def name
15
+ @attrs["zender"]["name"]
16
+ end
17
+
18
+ def code
19
+ @attrs["zender"]["code"]
20
+ end
21
+ end
22
+ end
data/lib/zelda.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'zelda/version'
2
+
3
+ require 'httparty'
4
+
5
+ require 'zelda/base'
6
+ require 'zelda/request'
7
+ require 'zelda/zender'
8
+ require 'zelda/omroep'
9
+ require 'zelda/serie'
10
+ require 'zelda/aflevering'
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
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
+
15
+ require 'zelda'
16
+
17
+ module Zelda
18
+ API_KEY = '12345' unless defined?(API_KEY)
19
+ end
@@ -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
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Zelda::Aflevering do
4
+ before do
5
+ @model = Zelda::Aflevering.new(:foo => "bar")
6
+ end
7
+
8
+ it_should_behave_like 'a zelda base model'
9
+
10
+ it "should accept attributes when being initialized" do
11
+ Zelda::Aflevering.new(:foo => "bar").should be_a(Zelda::Aflevering)
12
+ end
13
+
14
+ it "should find an aflevering in Zelda correctly" do
15
+ afl_attrs = { "aflevering" => {"afleveringid" => "POW_00243538", "name" => "3 op reis"} }
16
+ Zelda::Request.stub!(:get).with("afleveringen/1").and_return afl_attrs
17
+ afl = Zelda::Aflevering.find(1)
18
+ afl.should be_a(Zelda::Aflevering)
19
+ afl.afleveringid.should == 'POW_00243538'
20
+ end
21
+ end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
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
+
10
+ describe "when retrieving a list of omroepen" do
11
+ it "should call Zelda with the correct url" do
12
+ Zelda::Request.should_receive(:get).with("omroepen").and_return('omroepen' => ['omroep'])
13
+ Zelda::Omroep.all.should == ['omroep']
14
+ end
15
+ end
16
+
17
+ describe "when retrieving a specific omroep" do
18
+ before(:each) do
19
+ omroep_attrs = { "omroep" => {"slug" => "kro", "name" => "KRO"} }
20
+ Zelda::Request.stub!(:get).with("omroepen/kro").and_return omroep_attrs
21
+ end
22
+
23
+ def find_omroep
24
+ Zelda::Omroep.find('kro')
25
+ end
26
+
27
+ it "should call Zelda with the correct url" do
28
+ Zelda::Request.should_receive(:get).with("omroepen/kro")
29
+ find_omroep
30
+ end
31
+
32
+ it "should return a new Omroep" do
33
+ find_omroep.should be_a(Zelda::Omroep)
34
+ end
35
+
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"
43
+ end
44
+
45
+ it "should return the correct name" do
46
+ find_omroep.name.should == "KRO"
47
+ end
48
+ end
49
+
50
+ describe "when retrieving the series for an omroep" do
51
+ before(:each) do
52
+ @omroep = Zelda::Omroep.new('slug' => 'kro')
53
+
54
+ Zelda::Request.stub!(:get).and_return('omroep' => {'series' => {'serie' => [{'foo' => 'bar'}]}})
55
+ end
56
+
57
+ def find_series
58
+ @omroep.series
59
+ end
60
+
61
+ it "should call Zelda with the correct url" do
62
+ Zelda::Request.should_receive(:get).with("omroepen/kro/series")
63
+ find_series
64
+ end
65
+
66
+ it "should return an array of series" do
67
+ find_series.first.should be_a(Zelda::Serie)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Zelda::Request do
4
+ it "should call HTTParty" do
5
+ HTTParty.should_receive(:get).with("http://zelda.omroep.nl/12345/foo").and_return nil
6
+ Zelda::Request.get('foo')
7
+ end
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
+
21
+ it "should raise an error without an API key" do
22
+ api_key = Zelda.send(:remove_const, :API_KEY)
23
+ lambda { Zelda::Request.get('foo') }.should raise_error("No Zelda::API_KEY specified")
24
+ Zelda::API_KEY = api_key
25
+ end
26
+
27
+ describe "when returning an url" do
28
+ it "should be correct with 1 args" do
29
+ Zelda::Request.parse_url('foo').should == "http://zelda.omroep.nl/12345/foo"
30
+ end
31
+
32
+ it "should be correct with 2 args" do
33
+ Zelda::Request.parse_url('foo', 'bar').should == "http://zelda.omroep.nl/12345/foo/bar"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Zelda::Serie do
4
+ describe "when retrieving a list of series" do
5
+ it "should call Zelda with the correct url" do
6
+ Zelda::Request.should_receive(:get).with("series").and_return({ 'series' => ['serie'] })
7
+ Zelda::Serie.stub!(:new).and_return 'serie'
8
+ Zelda::Serie.all.should == ['serie']
9
+ end
10
+
11
+ it "should return an array of series" do
12
+ Zelda::Request.should_receive(:get).with("series").and_return({ 'series' => ['serie'] })
13
+ Zelda::Serie.all.first.should be_a(Zelda::Serie)
14
+ end
15
+ end
16
+
17
+ describe "when retrieving a specific serie" do
18
+ before(:each) do
19
+ serie_attrs = { "serie" => {"serie_id_nebo" => 302, "name" => "3 op reis"} }
20
+ Zelda::Request.stub!(:get).with("series/1").and_return serie_attrs
21
+
22
+ @aflevering = mock("Aflevering")
23
+ Zelda::Aflevering.stub!(:new).and_return @aflevering
24
+ end
25
+
26
+ def find_serie
27
+ Zelda::Serie.find(1)
28
+ end
29
+
30
+ it "should call Zelda with the correct url" do
31
+ Zelda::Request.should_receive(:get).with("series/1")
32
+ find_serie
33
+ end
34
+
35
+ it "should return a new Zender" do
36
+ find_serie.should be_a(Zelda::Serie)
37
+ end
38
+
39
+ it "should return nil if the zender cannot be found" do
40
+ Zelda::Request.stub!(:get).and_return nil
41
+ find_serie.should be_nil
42
+ end
43
+
44
+ it "should send the correct request when asked for afleveringen" do
45
+ serie = find_serie
46
+ Zelda::Request.should_receive(:get).with("series/302/afleveringen").and_return("afleveringen" => ["foo"])
47
+ serie.afleveringen.should == [@aflevering]
48
+ end
49
+
50
+ it "should send the correct request when asked for upcoming afleveringen" do
51
+ serie = find_serie
52
+ Zelda::Request.should_receive(:get).with("series/302/afleveringen/upcoming").and_return("afleveringen" => ["foo"])
53
+ serie.upcoming_afleveringen.should == [@aflevering]
54
+ end
55
+
56
+ it "should send the correct request when asked for past afleveringen" do
57
+ serie = find_serie
58
+ Zelda::Request.should_receive(:get).with("series/302/afleveringen/past").and_return("afleveringen" => ["foo"])
59
+ serie.past_afleveringen.should == [@aflevering]
60
+ end
61
+ end
62
+
63
+ it "should send the correct request when searching for series" do
64
+ Zelda::Request.should_receive(:get).with("series/search/foo").and_return("series" => ["foo"])
65
+ Zelda::Serie.should_receive(:new).with("foo").and_return "foo"
66
+ Zelda::Serie.search("foo").should == ["foo"]
67
+ end
68
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Zelda::Zender do
4
+ describe "when retrieving a list of zenders" do
5
+ it "should call Zelda with the correct url" do
6
+ Zelda::Request.should_receive(:get).with("zenders")
7
+ Zelda::Zender.all
8
+ end
9
+ end
10
+
11
+ describe "when retrieving a specific zender" do
12
+ before(:each) do
13
+ zender_attrs = { "zender" => {"code" => "NL1", "name" => "Nederland 1"} }
14
+ Zelda::Request.stub!(:get).with("zenders/1").and_return zender_attrs
15
+ end
16
+
17
+ def find_zender
18
+ Zelda::Zender.find(1)
19
+ end
20
+
21
+ it "should call Zelda with the correct url" do
22
+ Zelda::Request.should_receive(:get).with("zenders/1")
23
+ find_zender
24
+ end
25
+
26
+ it "should return a new Zender" do
27
+ find_zender.should be_a(Zelda::Zender)
28
+ end
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
+
35
+ it "should return the correct name" do
36
+ find_zender.name.should == "Nederland 1"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Zelda do
4
+ end
data/zelda.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "zelda/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "zelda"
7
+ s.version = Zelda::VERSION
8
+ s.authors = ["Joost Baaij"]
9
+ s.email = ["joost@spacebabies.nl"]
10
+ s.homepage = "https://github.com/tilsammans/zelda"
11
+ s.summary = %q{If you don't know what this is, you probably don't need it}
12
+ s.description = %q{If you don't know what this is, you probably don't need it}
13
+
14
+ s.rubyforge_project = "zelda"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zelda
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Joost Baaij
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-11 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: If you don't know what this is, you probably don't need it
18
+ email:
19
+ - joost@spacebabies.nl
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - README.rdoc
30
+ - Rakefile
31
+ - lib/zelda.rb
32
+ - lib/zelda/aflevering.rb
33
+ - lib/zelda/base.rb
34
+ - lib/zelda/omroep.rb
35
+ - lib/zelda/request.rb
36
+ - lib/zelda/serie.rb
37
+ - lib/zelda/version.rb
38
+ - lib/zelda/zender.rb
39
+ - spec/spec.opts
40
+ - spec/spec_helper.rb
41
+ - spec/support/shared_examples.rb
42
+ - spec/zelda/aflevering_spec.rb
43
+ - spec/zelda/omroep_spec.rb
44
+ - spec/zelda/request_spec.rb
45
+ - spec/zelda/serie_spec.rb
46
+ - spec/zelda/zender_spec.rb
47
+ - spec/zelda_spec.rb
48
+ - zelda.gemspec
49
+ has_rdoc: true
50
+ homepage: https://github.com/tilsammans/zelda
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: zelda
73
+ rubygems_version: 1.6.2
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: If you don't know what this is, you probably don't need it
77
+ test_files:
78
+ - spec/spec.opts
79
+ - spec/spec_helper.rb
80
+ - spec/support/shared_examples.rb
81
+ - spec/zelda/aflevering_spec.rb
82
+ - spec/zelda/omroep_spec.rb
83
+ - spec/zelda/request_spec.rb
84
+ - spec/zelda/serie_spec.rb
85
+ - spec/zelda/zender_spec.rb
86
+ - spec/zelda_spec.rb