Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for APIs that don't use file extensions

  • Loading branch information...
commit e9a07e6b2778d1be34eae2a67a6c3163ab3ab386 1 parent afe22d0
@justinweiss justinweiss authored
View
1  Gemfile
@@ -2,3 +2,4 @@ source "http://rubygems.org"
# Specify your gem's dependencies in reactive_resource.gemspec
gemspec
+gem 'rake'
View
2  Gemfile.lock
@@ -12,6 +12,7 @@ GEM
activesupport (2.3.10)
addressable (2.2.2)
crack (0.1.8)
+ rake (0.8.7)
shoulda (2.11.3)
webmock (1.6.2)
addressable (>= 2.2.2)
@@ -22,6 +23,7 @@ PLATFORMS
DEPENDENCIES
activeresource (~> 2.3.10)
+ rake
reactive_resource!
shoulda (~> 2.11.3)
webmock (~> 1.6.1)
View
15 lib/reactive_resource/base.rb
@@ -46,18 +46,22 @@ def self.collection_name
end
end
+ def self.extension
+ format.extension.blank? ? "" : ".#{format.extension}"
+ end
+
# This method differs from its parent by adding association_prefix
# into the generated url. This is needed to support belongs_to
# associations.
def self.collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
- "#{prefix(prefix_options)}#{association_prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
+ "#{prefix(prefix_options)}#{association_prefix(prefix_options)}#{collection_name}#{extension}#{query_string(query_options)}"
end
# Same as collection_path, except with an extra +method_name+
def self.custom_method_collection_url(method_name, options = {})
prefix_options, query_options = split_options(options)
- "#{prefix(prefix_options)}#{association_prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}"
+ "#{prefix(prefix_options)}#{association_prefix(prefix_options)}#{collection_name}/#{method_name}#{extension}#{query_string(query_options)}"
end
# This is overridden to support nested urls for belongs_to
@@ -70,7 +74,7 @@ def self.element_path(id, prefix_options = {}, query_options = nil)
if id || !singleton?
element_path += "/#{id}"
end
- element_path += ".#{format.extension}#{query_string(query_options)}"
+ element_path += "#{extension}#{query_string(query_options)}"
element_path
end
@@ -118,9 +122,8 @@ def self.association_prefix(options)
end
belongs_to_associations.each do |association|
- param = association.attribute
- if association_prefix.blank? && param_value = options.delete("#{param}_id".intern) # only take the first one
- association_prefix = "#{param.to_s.pluralize}/#{param_value}/"
+ if association_prefix.blank? && param_value = options.delete("#{association.attribute}_id".intern) # only take the first one
+ association_prefix = "#{association.associated_class.collection_name}/#{param_value}/"
end
end
end
View
16 test/test_objects.rb
@@ -42,3 +42,19 @@ class Phone < ReactiveResource::Address
class ReactiveResource::Phone < ReactiveResource::Base
belongs_to :address
end
+
+module ActiveResource
+ module Formats
+ module NoFormatFormat
+ extend ActiveResource::Formats::JsonFormat
+ extend self
+ def extension
+ ''
+ end
+ end
+ end
+end
+
+class ReactiveResource::NoExtension < ReactiveResource::Base
+ self.format = :no_format
+end
View
16 test/unit/base_test.rb
@@ -150,4 +150,20 @@ class ReactiveResource::BaseTest < Test::Unit::TestCase
end
end
+
+ context "A resource without an extension" do
+ should "hit the correct urls" do
+ stub_request(:get, "https://api.avvo.com/api/1/no_extensions")
+ @object = ReactiveResource::NoExtension.find(:all)
+ assert_requested(:get, "https://api.avvo.com/api/1/no_extensions")
+
+ stub_request(:put, "https://api.avvo.com/api/1/no_extensions/1")
+ @object = ReactiveResource::NoExtension.new(:id => 1).save
+ assert_requested(:put, "https://api.avvo.com/api/1/no_extensions/1")
+
+ stub_request(:get, "https://api.avvo.com/api/1/no_extensions/test")
+ @object = ReactiveResource::NoExtension.get(:test)
+ assert_requested(:get, "https://api.avvo.com/api/1/no_extensions/test")
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.