Permalink
Browse files

Merge remote-tracking branch 'origin/pagination' into 0.5.x

Conflicts:
	Gemfile.lock
	lib/almodovar.rb
  • Loading branch information...
2 parents 9392e3f + 93021f4 commit 472cabb37a94e4cd400a9b7a1c5e55505367cf99 @cavalle cavalle committed Apr 30, 2012
View
@@ -1,3 +1,9 @@
source "http://rubygems.org"
gemspec
+
+gem 'rake'
+gem 'rspec', '~> 1.3.0'
+gem 'steak'
+gem 'webmock', '~> 1.3.0'
+gem 'lorax'
View
@@ -5,14 +5,17 @@ PATH
activesupport (~> 2.3.0)
nokogiri
resourceful (= 0.5.3)
+ yajl-ruby
GEM
remote: http://rubygems.org/
specs:
- activesupport (2.3.14)
+ activesupport (2.3.12)
addressable (2.2.2)
crack (0.1.8)
httpauth (0.1)
+ lorax (0.2.0)
+ nokogiri (>= 1.4.0)
nokogiri (1.5.2)
rake (0.8.7)
resourceful (0.5.3)
@@ -24,12 +27,14 @@ GEM
webmock (1.3.4)
addressable (>= 2.1.1)
crack (>= 0.1.7)
+ yajl-ruby (0.8.2)
PLATFORMS
ruby
DEPENDENCIES
almodovar!
+ lorax
rake
rspec (~> 1.3.0)
steak
View
@@ -85,7 +85,7 @@ Resource collections have the _create_ method. Just call it with the attributes
>> jobs = Almodovar::Resource("http://sequence.example.com/api/work_areas/52/jobs", auth)
=> [<job> ... </job>, <job> ... </job>]
- >> job = jobs.create(:name => "Wadus")
+ >> job = jobs.create(:job => {:name => "Wadus"})
=> <job> ... </job>
>> job.name
=> "Wadus"
@@ -96,7 +96,7 @@ You can use the _update_ method:
>> job = Almodovar::Resource("http://sequence.example.com/api/work_areas/52/jobs", auth).first
=> <job> ... </job>
- >> job.update(:name => "Wadus wadus")
+ >> job.update(:job => {:name => "Wadus wadus"})
=> ...
>> job.name
=> "Wadus wadus"
View
@@ -6,7 +6,6 @@ Gem::Specification.new do |s|
s.email = "ballsbreaking@bebanjo.com"
s.homepage = "http://wiki.github.com/bebanjo/almodovar/"
- s.has_rdoc = true
s.extra_rdoc_files = %w(README.rdoc)
s.rdoc_options = %w(--main README.rdoc)
@@ -16,9 +15,5 @@ Gem::Specification.new do |s|
s.add_dependency("resourceful", "= 0.5.3")
s.add_dependency("nokogiri")
s.add_dependency("activesupport", "~> 2.3.0")
-
- s.add_development_dependency("rake")
- s.add_development_dependency("rspec", "~> 1.3.0")
- s.add_development_dependency("steak")
- s.add_development_dependency("webmock", "~> 1.3.0")
+ s.add_dependency("yajl-ruby")
end
View
@@ -1,183 +1,17 @@
require File.dirname(__FILE__) + '/../vendor/resourceful-0.5.3-patched/lib/resourceful'
require 'nokogiri'
-require 'active_support'
-require 'to_xml'
-require 'uri'
+begin
+ require 'active_support/all'
+rescue LoadError
+ require 'active_support'
+end
-module Almodovar
-
- class DigestAuth < Resourceful::DigestAuthenticator
- end
-
- module HttpAccessor
-
- def xml
- @xml ||= begin
- response = http.resource(url_with_params).get
- Nokogiri.parse(response.body).root
- end
- end
-
- private
-
- def url_with_params
- @options[:expand] = @options[:expand].join(",") if @options[:expand].is_a?(Array)
- params = @options.map { |k, v| "#{k}=#{v}" }.join("&")
- params = "?#{params}" unless params.empty?
- @url + params
- end
-
- def http
- Resourceful::HttpAccessor.new(:authenticator => @auth)
- end
-
- end
-
- class SingleResource
-
- include HttpAccessor
-
- undef id, type
-
- def initialize(url, auth, xml = nil, options = {})
- @url = url
- @auth = auth
- @xml = xml
- @options = options
- end
-
- def update(attrs = {})
- response = http.resource(@url).put(attrs.to_xml(:root => object_type), :content_type => "application/xml")
- @xml = Nokogiri.parse(response.body).root
- end
-
- def delete
- http.resource(@url).delete
- end
-
- def url
- @url ||= xml.at_xpath("./link[@rel='self']").try(:[], "href")
- end
-
- delegate :to_xml, :to => :xml
- alias_method :inspect, :to_xml
-
- private
-
- def object_type
- uri_parts = URI.parse(@url).path.split("/")
- @object_type ||= (uri_parts.last =~ /^\d+$/ ? uri_parts[-2].singularize : uri_parts.last.singularize)
- end
-
- def [](key) # for resources with type "document"
- return super unless xml.at_xpath("/*[@type='document']")
- Hash.from_xml(xml.to_xml).values.first[key]
- end
-
- def method_missing(meth, *args, &blk)
- document = xml.at_xpath("./*[(name()='#{meth}' or name()='#{attribute_name(meth)}') and @type='document']")
- return Resource.from_xml(document.to_xml) if document
-
- attribute = xml.at_xpath("./*[name()='#{meth}' or name()='#{attribute_name(meth)}']")
- return node_text(attribute) if attribute
-
- link = xml.at_xpath("./link[@rel='#{meth}' or @rel='#{attribute_name(meth)}']")
- return Resource.new(link["href"], @auth, link.at_xpath("./*"), *args) if link
-
- super
- end
-
- def node_text(node)
- case node['type']
- when "integer": node.text.to_i
- when "datetime": Time.parse(node.text)
- else
- node.text
- end
- end
-
- def attribute_name(attribute)
- attribute.to_s.gsub('_', '-')
- end
-
- end
-
- class ResourceCollection
-
- include HttpAccessor
-
- delegate :inspect, :to => :resources
-
- def initialize(url, auth, xml = nil, options = {})
- @url = url
- @auth = auth
- @xml = xml if options.empty?
- @options = options
- end
-
- def create(attrs = {})
- response = http.resource(url_with_params).post(attrs.to_xml(:root => object_type, :convert_links => true), :content_type => "application/xml")
- Resource.new(nil, @auth, Nokogiri.parse(response.body).root)
- end
-
- private
-
- def object_type
- @object_type ||= URI.parse(@url).path.split("/").last.singularize
- end
-
- def resources
- @resources ||= xml.xpath("./*").map { |subnode| Resource.new(subnode.at_xpath("./link[@rel='self']").try(:[], "href"), @auth, subnode, @options) }
- end
-
- def method_missing(meth, *args, &blk)
- resources.send(meth, *args, &blk)
- end
- end
-
- class Resource
-
- include HttpAccessor
-
- undef id, type
-
- delegate :inspect, :to => :get!
-
- def self.from_xml(xml, auth = nil)
- new(nil, auth, Nokogiri.parse(xml).root)
- end
-
- def initialize(url, auth, xml = nil, options = {})
- @url = url
- @auth = auth
- @xml = xml
- @options = options
- end
-
- def method_missing(meth, *args, &blk)
- @resource_object ||= resource_class(meth).new(@url, @auth, @xml, @options)
- @resource_object.send(meth, *args, &blk)
- end
-
- def resource_class(meth, *args)
- @resource_class ||= collection_call?(meth, *args) ? ResourceCollection : SingleResource
- end
-
- def get!
- klass = xml['type'] == 'array' ? ResourceCollection : SingleResource
- @resource_object = klass.new(@url, @auth, @xml, @options)
- end
-
- private
-
- def collection_call?(meth, *args)
- (Array.instance_methods + ["create"] - ["delete", "id", "[]"]).include?(meth.to_s) ||
- (meth.to_s == "[]" && args.size == 1 && args.first.is_a?(Fixnum))
- end
- end
-
- def self.Resource(url, auth, params = {})
- Resource.new(url, auth, nil, params)
- end
-
-end
+require 'almodovar/digest_auth'
+require 'almodovar/http_accessor'
+require 'almodovar/resource'
+require 'almodovar/resource_collection'
+require 'almodovar/single_resource'
+require 'almodovar/to_xml'
+require 'almodovar/resource_presenter'
+require 'almodovar/resource_presenter/collection'
+require 'almodovar/resource_presenter/link'
@@ -0,0 +1,4 @@
+module Almodovar
+ class DigestAuth < Resourceful::DigestAuthenticator
+ end
+end
@@ -0,0 +1,23 @@
+module Almodovar
+ module HttpAccessor
+ def xml
+ @xml ||= begin
+ response = http.resource(url_with_params).get
+ Nokogiri::XML.parse(response.body).root
+ end
+ end
+
+ private
+
+ def url_with_params
+ @options[:expand] = @options[:expand].join(",") if @options[:expand].is_a?(Array)
+ params = @options.map { |k, v| "#{k}=#{v}" }.join("&")
+ params = "?#{params}" unless params.empty?
+ @url + params
+ end
+
+ def http
+ Resourceful::HttpAccessor.new(:authenticator => @auth)
+ end
+ end
+end
View
@@ -0,0 +1,54 @@
+module Almodovar
+ class Resource
+ include HttpAccessor
+
+ undef id, type
+
+ delegate :inspect, :to => :get!
+
+ def self.from_xml(xml, auth = nil)
+ new(nil, auth, Nokogiri::XML.parse(xml).root)
+ end
+
+ def initialize(url, auth, xml = nil, options = {})
+ @url = url
+ @auth = auth
+ @xml = xml
+ @options = options
+ end
+
+ def method_missing(meth, *args, &blk)
+ resource_object(meth).send(meth, *args, &blk)
+ end
+
+ def get!
+ klass = xml['type'] == 'array' ? ResourceCollection : SingleResource
+ @resource_object = klass.new(@url, @auth, @xml, @options)
+ end
+
+ def respond_to?(meth)
+ super || resource_object(meth).respond_to?(meth)
+ end
+
+ private
+
+ def collection_call?(meth, *args)
+ (Array.instance_methods + ["create"] - ["delete", "id", "[]"]).include?(meth.to_s) ||
+ (meth.to_s == "[]" && args.size == 1 && args.first.is_a?(Fixnum))
+ end
+
+ def resource_object(meth)
+ @resource_object ||= resource_class(meth).new(@url, @auth, @xml, @options)
+ end
+
+ def resource_class(meth, *args)
+ @resource_class ||= collection_call?(meth, *args) ? ResourceCollection : SingleResource
+ end
+
+ end
+
+ def self.Resource(url, auth = nil, params = {})
+ Resource.new(url, auth, nil, params)
+ end
+
+end
Oops, something went wrong.

0 comments on commit 472cabb

Please sign in to comment.