Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow array params #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/rebay/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Api

class << self
attr_accessor :app_id, :default_site_id, :sandbox

def base_url
[base_url_prefix,
sandbox ? "sandbox" : nil,
Expand All @@ -28,18 +28,18 @@ def base_url_suffix
def sandbox
@sandbox ||= false
end

def default_site_id
@default_site_id || EBAY_US
end

def configure
yield self if block_given?
end
end

protected

def get_json_response(url)
Rebay::Response.new(JSON.parse(Net::HTTP.get_response(URI.parse(url)).body))
end
Expand All @@ -48,7 +48,14 @@ def build_rest_payload(params)
payload = ''
unless params.nil?
params.keys.each do |key|
payload += URI.escape "&#{key}=#{params[key]}"
if params[key].is_a?(Array)
params[key].each_with_index do |object, index|
payload += URI.escape "&#{key}(#{index}).name=#{object[:name]}"
payload += URI.escape "&#{key}(#{index}).value=#{object[:value]}"
end
else
payload += URI.escape "&#{key}=#{params[key]}"
end
end
end
return payload
Expand Down
20 changes: 15 additions & 5 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Rebay
it "should respond to configure" do
Rebay::Api.should respond_to(:configure)
end

describe "#base_url_prefix" do
it "shouldn't be nil" do
Rebay::Api.base_url_prefix.should_not be_nil
Expand All @@ -19,14 +19,14 @@ module Rebay
Rebay::Api.base_url_suffix.should_not be_nil
end
end

describe "#base_url" do
context "api calls should hit the sandbox" do
it "should return a sandboxed url" do
Rebay::Api.configure do |c|
c.sandbox = true
end

Rebay::Api.base_url.should include "sandbox"
end
end
Expand All @@ -45,7 +45,7 @@ module Rebay
describe "#sandbox" do
it_behaves_like "a configuration option", :sandbox, true
end

describe "#app_id" do
it_behaves_like "a configuration option", :app_id, 'super_id-11'
end
Expand Down Expand Up @@ -77,7 +77,17 @@ module Rebay
payload.should include("&test2=blah")
payload.should include("&test3=blah%20blah")
end

it 'should correctly handle arrays in the hash' do
hash = {:test=>'blah',
:test2=>[{:name=>'filter1', :value=>1},
{:name=>'filter2', :value=>'true'}]}
payload = @api.send(:build_rest_payload, hash)
payload.should include("&test=blah")
payload.should include("&test2(0).name=filter1&test2(0).value=1")
payload.should include("&test2(1).name=filter2&test2(1).value=true")
end
end
end
end