Skip to content

Commit

Permalink
Added SellerListing client module
Browse files Browse the repository at this point in the history
- Added methods for getting seller listings
  • Loading branch information
akilburge committed Jun 29, 2015
1 parent 2bb391c commit a7f0134
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/gogokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'gogokit/client/oauth'
require 'gogokit/client/root'
require 'gogokit/client/search'
require 'gogokit/client/seller_listing'
require 'gogokit/client/venue'
require 'gogokit/version'

Expand All @@ -33,6 +34,7 @@ class Client
include GogoKit::Client::OAuth
include GogoKit::Client::Root
include GogoKit::Client::Search
include GogoKit::Client::SellerListing
include GogoKit::Client::Venue

attr_accessor :client_id,
Expand Down
40 changes: 40 additions & 0 deletions lib/gogokit/client/seller_listing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'gogokit/utils'
require 'gogokit/resource/seller_listing'

module GogoKit
class Client
# {GogoKit::Client} methods for getting seller_listings
module SellerListing
include GogoKit::Utils

# Retrieves a listing by ID
#
# @param [Integer] listing_id The ID of the listing to be retrieved
# @param [Hash] options Optional options
# @return [GogoKit::SellerListing] The requested listing
def get_seller_listing(listing_id, options = {})
root = get_root
object_from_response(GogoKit::SellerListing,
GogoKit::SellerListingRepresenter,
:get,
"#{root.links['self'].href}/sellerlistings/" \
"#{listing_id}",
options)
end

# Retrieves all listings for the authenticated user
#
# @see http://developer.viagogo.net/#usersellerlistings
# @param [Hash] options Optional options
# @return [GogoKit::PagedResource] All listings for the authenticated user
def get_seller_listings(options = {})
root = get_root
object_from_response(GogoKit::PagedResource,
GogoKit::SellerListingsRepresenter,
:get,
"#{root.links['self'].href}/sellerlistings",
options)
end
end
end
end
14 changes: 12 additions & 2 deletions lib/gogokit/resource/seller_listing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SellerListing < Resource
:listing_notes
end

module ListingRepresenter
module SellerListingRepresenter
include Representable::JSON
include GogoKit::ResourceRepresenter

Expand Down Expand Up @@ -78,4 +78,14 @@ module ListingRepresenter
skip_parse: ->(fragment, _) { fragment.nil? },
embedded: true
end
end

module SellerListingsRepresenter
include Representable::JSON
include GogoKit::PagedResourceRepresenter

collection :items,
class: SellerListing,
extend: SellerListingRepresenter,
embedded: true
end
end
2 changes: 1 addition & 1 deletion spec/gogokit/client/listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

it 'returns {GogoKit::Listing} created from the response' do
allow(client).to receive(:get_root).and_return(root)
stub_request(:any, /.*/).to_return(body: fixture('genres.json'))
stub_request(:any, /.*/).to_return(body: fixture('listing.json'))
genres = client.get_listing(-1)
expect(genres).to be_an_instance_of(GogoKit::Listing)
end
Expand Down
102 changes: 102 additions & 0 deletions spec/gogokit/client/seller_listing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'rspec'
require 'spec_helper'

describe GogoKit::Client::SellerListing do
let(:client) do
GogoKit::Client.new(client_id: 'CK', client_secret: 'CS')
end

let(:root) do
self_link = Roar::Hypermedia::Hyperlink.new(href: 'https://api.com/')
root = GogoKit::Root.new
root.links = {
'self' => self_link
}
root
end

describe '#get_seller_listing' do
it 'performs a get request' do
allow(client).to receive(:get_root).and_return(root)
stub_request(:any, /.*/).to_return(body: '{}')
client.get_seller_listing(-1)
expect(a_request(:get, /.*/)).to have_been_made
end

it 'performs a request built from the self link of the root resource' do
self_link = Roar::Hypermedia::Hyperlink.new(href: 'https://apiroot.com')
root = GogoKit::Root.new
root.links = {'self' => self_link}
expected_url = self_link.href + '/sellerlistings/55'
allow(client).to receive(:get_root).and_return(root)
allow(client).to receive(:request).and_return(body: '{}', status: 200)
client.get_seller_listing 55

expect(client).to have_received(:request).with(anything,
expected_url,
anything)
end

it 'passes the given options in the request' do
expected_options = {params: {foo: 5}, headers: {bar: '50'}}
allow(client).to receive(:get_root).and_return(root)
allow(client).to receive(:request).and_return(body: '{}', status: 200)
client.get_seller_listing(-1, expected_options)

expect(client).to have_received(:request) do |_, _, options|
expect(options).to eq(expected_options)
end
end

it 'returns {GogoKit::SellerListing} created from the response' do
allow(client).to receive(:get_root).and_return(root)
stub_request(:any, /.*/).to_return(body: fixture('seller_listing.json'))
genres = client.get_seller_listing(-1)
expect(genres).to be_an_instance_of(GogoKit::SellerListing)
end
end

describe '#get_seller_listings' do
it 'performs a get request' do
allow(client).to receive(:get_root).and_return(root)
stub_request(:any, /.*/).to_return(body: '{}')
client.get_seller_listings
expect(a_request(:get, /.*/)).to have_been_made
end

it 'performs a request built from the self link of the root resource' do
self_link = Roar::Hypermedia::Hyperlink.new(href: 'https://apiroot.com')
root = GogoKit::Root.new
root.links = {'self' => self_link}
expected_url = self_link.href + '/sellerlistings'
allow(client).to receive(:get_root).and_return(root)
allow(client).to receive(:request).and_return(body: '{}', status: 200)
client.get_seller_listings

expect(client).to have_received(:request).with(anything,
expected_url,
anything)
end

it 'passes the given options in the request' do
expected_options = {params: {foo: 5}, headers: {bar: '50'}}
allow(client).to receive(:get_root).and_return(root)
allow(client).to receive(:request).and_return(body: '{}', status: 200)
client.get_seller_listings expected_options

expect(client).to have_received(:request) do |_, _, options|
expect(options).to eq(expected_options)
end
end

it 'returns {GogoKit::PagedResource} with {GogoKit::SellerListing}' \
':items created from the response' do
allow(client).to receive(:get_root).and_return(root)
stub_request(:any, /.*/).to_return(body: fixture('seller_listings.json'))
seller_listings = client.get_seller_listings
expect(seller_listings).to be_an_instance_of(GogoKit::PagedResource)
expect(seller_listings.items[0])
.to be_an_instance_of(GogoKit::SellerListing)
end
end
end

0 comments on commit a7f0134

Please sign in to comment.