almodovar 0.0.1

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.
Files changed (3) hide show
  1. data/README.rdoc +57 -0
  2. data/lib/almodovar.rb +71 -0
  3. metadata +113 -0
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ =Almodovar
2
+
3
+ Almodovar is a client for BeBanjo Movida API written in Ruby
4
+
5
+ ==Getting started
6
+
7
+ Install the gem (make sure you have http://rubygems.org in your gem source list):
8
+
9
+ $ [sudo] gem install almodovar
10
+
11
+ Now, let's play with irb:
12
+
13
+ >> require 'almodovar'
14
+ => true
15
+
16
+ First you need an authentication token:
17
+
18
+ >> auth = Almodovar::DigestAuth.new("realm", "user", "password")
19
+ => #<Almodovar::DigestAuth:0x101f846c8 @password="secret", @username="robot_five", @realm="Staging Movida", @domain=nil>
20
+
21
+ Now you have to instantiate a resource given its URL. Let's try with the root of the api:
22
+
23
+ >> movida = Almodovar::Resource("http://movida.example.com/api", auth)
24
+ => <movida>
25
+ <link href="http://movida.example.com/api/titles" rel="titles"/>
26
+ <link href="http://movida.example.com/api/platforms" rel="platforms"/>
27
+ <link href="http://movida.example.com/api/title_groups" rel="title_groups"/>
28
+ </movida>
29
+
30
+ Ok. Let's see what we have under _platforms_
31
+
32
+ >> movida.platforms
33
+ => [<platform>
34
+ <name>YouTube</name>
35
+ <link href="http://movida.example.com/api/platforms/5/schedule" rel="schedule"/>
36
+ </platform>, <platform>
37
+ <name>Vimeo</name>
38
+ <link href="http://movida.example.com/api/platforms/6/schedule" rel="schedule"/>
39
+ </platform>]
40
+
41
+ Now, show me the _schedule_ of the first _platform_:
42
+
43
+ >> movida.platforms.first.schedule
44
+ => <schedule>
45
+ <link href="http://movida.example.com/api/platforms/1/schedule/schedulings" rel="schedulings"/>
46
+ <link href="http://movida.example.com/api/platforms/1" rel="platform"/>
47
+ </schedule>
48
+
49
+ Next, explore and see the {docs of the API}[http://wiki.github.com/bebanjo/almodovar/] to know about other resources.
50
+
51
+ ==To-do
52
+
53
+ * Ability to use parameters in linked resource calls (e.g. <tt>movida.titles(:external_id => "C51234500001")</tt>)
54
+ * Memoize linked resource calls so that the same request isn't make several times
55
+ * Ability to take advantage of the API _expand_ functionality (e.g. <tt>movida.titles(:expand => [:schedule, :schedulings]).schedule.schedulings</tt>)
56
+
57
+ Copyright (c) 2008 BeBanjo S.L., released under the MIT license
data/lib/almodovar.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'resourceful'
2
+ require 'nokogiri'
3
+
4
+ module Almodovar
5
+
6
+ class DigestAuth < Resourceful::DigestAuthenticator
7
+ end
8
+
9
+ class Resource
10
+ def initialize(auth, xml)
11
+ @auth = auth
12
+ @xml = xml
13
+ end
14
+
15
+ def inspect
16
+ @xml.to_s
17
+ end
18
+
19
+ private
20
+
21
+ def method_missing(meth, *args, &blk)
22
+ attribute = @xml.at_xpath("./*[name()='#{meth}']")
23
+ return node_text(attribute) if attribute
24
+
25
+ link = @xml.at_xpath("./link[@rel='#{meth}']")
26
+ return get_linked_resource(link) if link
27
+
28
+ super
29
+ end
30
+
31
+ def get_linked_resource(link)
32
+ expansion = link.at_xpath("./*")
33
+ expansion ? Almodovar.instantiate(expansion, @auth) : Almodovar::Resource(link['href'], @auth)
34
+ end
35
+
36
+ def node_text(node)
37
+ case node['type']
38
+ when "integer": node.text.to_i
39
+ when "datetime": Time.parse(node.text)
40
+ else
41
+ node.text
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.Resource(url, auth, params = {})
47
+ http = Resourceful::HttpAccessor.new(:authenticator => auth)
48
+ response = http.resource(add_params(url, params)).get
49
+ instantiate Nokogiri.parse(response.body).root, auth
50
+ end
51
+
52
+ def self.instantiate(node, auth)
53
+ if node['type'] == 'array'
54
+ node.xpath("./*").map { |subnode| Resource.new(auth, subnode) }
55
+ else
56
+ Resource.new(auth, node)
57
+ end
58
+ end
59
+
60
+ def self.add_params(url, options)
61
+ options[:expand] = options[:expand].join(",") if options[:expand].is_a?(Array)
62
+ params = options.map { |k, v| "#{k}=#{v}" }.join("&")
63
+ params = "?#{params}" unless params.empty?
64
+ url + params
65
+ end
66
+
67
+ def self.ResourceCollection(*args)
68
+ ResourceCollection.new(*args)
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: almodovar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - BeBanjo S.L.
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: resourceful
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 5
30
+ - 3
31
+ version: 0.5.3
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: nokogiri
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: steak
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: webmock
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id004
70
+ description:
71
+ email: ballsbreaking@bebanjo.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - README.rdoc
78
+ files:
79
+ - lib/almodovar.rb
80
+ - README.rdoc
81
+ has_rdoc: true
82
+ homepage: http://wiki.github.com/bebanjo/almodovar/
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --main
88
+ - README.rdoc
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.6
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: BeBanjo Movida API client
112
+ test_files: []
113
+