Skip to content

Commit

Permalink
Call super on method missing when an API call can't be found
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Fauser committed Dec 30, 2006
1 parent b0f6f3d commit e01af61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# * Actually use the auth_token, call super on method_missing when the API call isn't found
# * Fix overriding the default site_id and auth_token when constructing the Api
# * Make Gem work with edge Rails
# * Update to schema version 491
Expand Down
46 changes: 21 additions & 25 deletions lib/ebay/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Api

cattr_accessor :use_sandbox, :sandbox_url, :production_url, :site_id
cattr_accessor :dev_id, :app_id, :cert, :auth_token
attr_reader :auth_token, :site_id

self.sandbox_url = 'https://api.sandbox.ebay.com/ws/api.dll'
self.production_url = 'https://api.ebay.com/ws/api.dll'
Expand Down Expand Up @@ -89,14 +90,6 @@ def cert
self.class.cert
end

def auth_token
@auth_token || self.class.auth_token
end

def site_id
@site_id || self.class.site_id
end

# With no options, the default is to use the default site_id and the default
# auth_token configured on the Api class.
# ebay = Ebay::Api.new
Expand All @@ -106,32 +99,35 @@ def site_id
# ebay = Ebay::Api.new(:site_id => 2, :auth_token => 'TEST')
def initialize(options = {})
@format = options[:format] || :object
@auth_token = options[:auth_token]
@site_id = options[:site_id]
@auth_token = options[:auth_token] || self.class.auth_token
@site_id = options[:site_id] || self.class.site_id
end

private
def method_missing(method_id, *args, &block)
args = args.first || {}

method_args = { :format => @format }.update(args)
method_args[:auth_token] = auth_token

invoke_request(method_id.to_s, method_args, &block)
format = args.delete(:format) || @format

args[:auth_token] = auth_token

begin
request = build_request(method_id.to_s.ebay_camelize, args)
rescue NameError
super
end

yield request if block_given?
invoke(request, format)
end

def invoke_request(name, args)
format = args.delete(:format)
request = build_request(name.ebay_camelize, args)

yield request if block_given?

raw_response = connection.post( service_uri.path,
build_body(request),
build_headers(request.call_name)
)
def invoke(request, format)
response = connection.post( service_uri.path,
build_body(request),
build_headers(request.call_name)
)

parse decompress(raw_response), format
parse decompress(response), format
end

def build_request(name, args)
Expand Down
6 changes: 6 additions & 0 deletions test/unit/ebay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ def test_raise_on_error_with_errors
assert_equal ErrorClassificationCode::RequestError, error.error_classification
end
end

def test_unknown_request_raises_no_method_error
assert_raise(NoMethodError) do
@ebay.get_sushi
end
end
end

0 comments on commit e01af61

Please sign in to comment.