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

Support inventory instead of variants for new API #131

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
497d353
Add Inventory api to Esty
buithehien1991 Mar 22, 2021
ce738c1
Add is_digital attribute to listing, inventory only get method
buithehien1991 Mar 23, 2021
09667d2
Update version
buithehien1991 Mar 23, 2021
39f5378
Add new attributes for receipt and transaction
buithehien1991 Mar 23, 2021
06e014e
Add submit tracking code
buithehien1991 Mar 27, 2021
b11b7c6
Add method for get all receipts and transaction. Not only 25 limit
hienbt-tsdv Apr 8, 2021
1be3167
Merge remote-tracking branch 'origin/master'
hienbt-tsdv Apr 8, 2021
ec31a9b
Add method for get all receipts and transaction. Not only 25 limit
hienbt-tsdv Apr 8, 2021
483c88f
Correct post method for submitTracking
hienbt-tsdv Apr 11, 2021
a8eb567
Add Update inventory function
hienbt-tsdv Apr 20, 2021
b1d707f
Add taxonomy model
buithehien1991 Apr 24, 2021
57fa408
Update version
buithehien1991 Apr 24, 2021
1659474
Update version
buithehien1991 Apr 24, 2021
05eb41f
Update taxonomy
buithehien1991 Apr 24, 2021
73f51ee
Update category
buithehien1991 Apr 24, 2021
673a391
Add taxonomy_node_property
hienbt-tsdv Apr 28, 2021
86dcaa5
Support create shop section
hienbt-tsdv May 8, 2021
02c793a
Support ListingFile
buithehien1991 May 9, 2021
7aa164d
Keep old homepage
buithehien1991 May 9, 2021
142e156
Correct static method for ListingFile
buithehien1991 May 9, 2021
d72b042
Dont know why happen error: NameError (uninitialized constant Etsy::L…
buithehien1991 May 9, 2021
516e558
Correct bug NameError (uninitialized constant Etsy::File)
buithehien1991 May 9, 2021
fc23b9a
Add files method to listing
buithehien1991 May 9, 2021
2a7a5ed
Add update inventory post
buithehien1991 May 9, 2021
66cc58b
Update inventory with put body data
buithehien1991 May 9, 2021
0b22193
Fix bug get all sections
buithehien1991 May 15, 2021
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
3 changes: 3 additions & 0 deletions lib/etsy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
require 'etsy/receipt'
require 'etsy/variation/property_set'
require 'etsy/about'
require 'etsy/inventory'
require 'etsy/taxonomy'
require 'etsy/listing_file'

# = Etsy: A friendly Ruby interface to the Etsy API
#
Expand Down
80 changes: 12 additions & 68 deletions lib/etsy/category.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,28 @@
module Etsy

# = Category
#
# A category of listings for sale, formed from 1 to 3 tags.
#
# A category has the following attributes:
#
# [page_description] A short description of the category from the category's landing page
# [page_title] The title of the category's landing page
# [category_name] The category's string ID
# [short_name] A short display name for the category
# [long_name] A longer display name for the category
# [children_count] The number of subcategories below this one
#
class Category

include Etsy::Model

attribute :id, :from => :category_id
attribute :children_count, :from => :num_children
attributes :page_description, :page_title, :category_name, :short_name,
:long_name

# Retrieve one or more top-level categories by name:
#
# Etsy::Category.find('accessories')
#
# You can find multiple top-level categories by passing an array of identifiers:
#
# Etsy::Category.find(['accessories', 'art'])
#
def self.find_top(*identifiers_and_options)
self.find_one_or_more('categories', identifiers_and_options)
end
attribute :id
attributes :level, :name, :parent, :parent_id, :path, :category_id, :children, :children_ids, :full_path_taxonomy_ids

# Retrieve a list of all subcategories of the specified category.
#
# Etsy::Category.find_all_subcategories('accessories')
#
# You can also find the subcategories of a subcategory.
#
# Etsy::Category.find_all_subcategories('accessories/apron')
#
# Etsy categories only go three levels deep, so this will return nil past the third level.
#
# Etsy::Category.find_all_subcategories('accessories/apron/women')
# => nil
#
def self.find_all_subcategories(category, options = {})
valid?(category) ? self.get_all("/taxonomy/categories/#{category}", options) : nil
def self.get_buyer_taxonomy
get_all('/taxonomy/buyer/get')
end

def self.find(tag)
get("/categories/#{tag}")
def self.get_seller_taxonomy
get_all('/taxonomy/seller/get')
end

# Retrieve a list of all top-level categories.
#
# Etsy::Category.all
#
def self.all_top(options = {})
self.get_all("/taxonomy/categories", options)
def self.get_seller_taxonomy_version
get('/taxonomy/seller/version')
end

# The collection of active listings associated with this category.
#
def active_listings
@active_listings ||= Listing.find_all_active_by_category(category_name)
def taxonomy_node_property
get_all("/taxonomy/seller/#{id}/properties")
end

# The collection of subcategories associated with this category.
#
def subcategories
@subcategories ||= Category.find_all_subcategories(category_name)
end

private

def self.valid?(parent_category)
parent_category.count("/") < 2
def self.taxonomy_node_property(taxonomy_id)
get_all("/taxonomy/seller/#{taxonomy_id}/properties")
end
end
end
26 changes: 26 additions & 0 deletions lib/etsy/inventory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Etsy
class Inventory
include Etsy::Model

attributes :products, :price_on_property, :quantity_on_property, :sku_on_property
def self.find_by_listing_id(listing_id, options={})
get("/listings/#{listing_id}/inventory", options)
end

def self.update_inventory(listing_id, options={})
options.merge!(:require_secure => true)
put_body("/listings/#{listing_id}/inventory", options)
end

def self.update_inventory_post(listing_id, options={})
options.merge!(:require_secure => true)
post("/listings/#{listing_id}/inventory", options)
end

private

def oauth
oauth = (token && secret) ? {:access_token => token, :access_secret => secret} : {}
end
end
end
30 changes: 24 additions & 6 deletions lib/etsy/listing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,30 @@ class Listing
:tags, :materials, :hue, :saturation, :brightness, :is_black_and_white,
:featured_rank, :occasion, :num_favorers, :user_id,
:shipping_template_id, :who_made, :when_made,
:style, :category_path, :taxonomy_id, :taxonomy_attributes
:style, :category_path, :taxonomy_id, :taxonomy_attributes,
:sku, :category_id, :taxonomy_path,
:has_variations, :should_auto_renew, :is_digital

association :image, :from => 'Images'

def transactions
@transactions ||= Transaction.find_all_by_listing_id(id, oauth)
end

def all_transactions
@all_transactions ||= Transaction.find_all_by_listing_id(id, oauth.merge({ limit: :all }))
end

def inventory
Inventory.find_by_listing_id(id, oauth)
end

def receipts
transactions.map{|t|t.receipt}
transactions.map(&:receipt)
end

def all_receipts
all_transactions.map(&:receipt)
end

def self.create(options = {})
Expand Down Expand Up @@ -132,6 +146,10 @@ def self.find_all_active_by_category(category, options = {})
get_all("/listings/active", options)
end

def files
@files ||= ListingFile.find_files_by_listing(id, oauth)
end

# The collection of images associated with this listing.
#
def images
Expand Down Expand Up @@ -244,7 +262,7 @@ def ending_at
def admirers(options = {})
options = options.merge(:access_token => token, :access_secret => secret) if (token && secret)
favorite_listings = FavoriteListing.find_all_listings_favored_by(id, options)
user_ids = favorite_listings.map {|f| f.user_id }.uniq
user_ids = favorite_listings.map(&:user_id).uniq
(user_ids.size > 0) ? Array(Etsy::User.find(user_ids, options)) : []
end

Expand All @@ -266,7 +284,7 @@ def self.sold_listings(shop_id, options = {})
includes = options.delete(:includes)

transactions = Transaction.find_all_by_shop_id(shop_id, options)
listing_ids = transactions.map {|t| t.listing_id }.uniq
listing_ids = transactions.map(&:listing_id).uniq

options = options.merge(:includes => includes) if includes
(listing_ids.size > 0) ? Array(find(listing_ids, options)) : []
Expand All @@ -276,7 +294,7 @@ def self.sold_listings(shop_id, options = {})
#
def self.find_all_user_favorite_listings(user_id, options = {})
favorite_listings = FavoriteListing.find_all_user_favorite_listings(user_id, options)
listing_ids = favorite_listings.map {|f| f.listing_id }.uniq
listing_ids = favorite_listings.map(&:listing_id).uniq
(listing_ids.size > 0) ? Array(find(listing_ids, options)) : []
end

Expand All @@ -286,7 +304,7 @@ def self.bought_listings(user_id, options = {})
includes = options.delete(:includes)

transactions = Transaction.find_all_by_buyer_id(user_id, options)
listing_ids = transactions.map {|t| t.listing_id }.uniq
listing_ids = transactions.map(&:listing_id).uniq

options = options.merge(:includes => includes) if includes
(listing_ids.size > 0) ? Array(find(listing_ids, options)) : []
Expand Down
26 changes: 26 additions & 0 deletions lib/etsy/listing_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Etsy
class ListingFile
include Etsy::Model

attribute :id, :from => :listing_file_id
attribute :listing_id

attributes :rank, :filename, :filesize, :size_bytes, :filetype, :create_date

def self.find_files_by_listing(listing_id, options)
get_all("/listings/#{listing_id}/files", options)
end

def self.upload_listing_file(listing_id, options)
post("/listings/#{listing_id}/files", options)
end

def self.find_listing_file(listing_id, listing_file_id, options)
get("/listings/#{listing_id}/files/#{listing_file_id}", options)
end

def self.delete_listing_file(listing_id, listing_file_id, options)
delete("/listings/#{listing_id}/files/#{listing_file_id}", options)
end
end
end
4 changes: 4 additions & 0 deletions lib/etsy/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def put(endpoint, options={})
Request.put(endpoint, options)
end

def put_body(endpoint, options={})
Request.put_body(endpoint, options)
end

def delete(endpoint, options={})
Request.delete(endpoint, options)
end
Expand Down
27 changes: 21 additions & 6 deletions lib/etsy/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ module Etsy
class Receipt
include Model

attribute :id, :from => :receipt_id
attribute :buyer_id, :from => :buyer_user_id
attribute :id, from: :receipt_id
attribute :buyer_id, from: :buyer_user_id

attribute :created, :from => :creation_tsz
attribute :last_modified, :from => :last_modified_tsz
attribute :created, from: :creation_tsz
attribute :last_modified, from: :last_modified_tsz

attributes :quantity, :listing_id, :name, :first_line, :second_line, :city, :state, :zip, :country_id,
:payment_email, :buyer_email
:formatted_address, :payment_method, :payment_email, :buyer_email,
:message_from_seller, :message_from_buyer, :was_paid, :was_shipped,
:grandtotal, :adjusted_grandtotal, :buyer_adjusted_grandtotal, :shipments

def self.find(*identifiers_and_options)
find_one_or_more('receipts', identifiers_and_options)
Expand All @@ -23,6 +25,11 @@ def self.find_all_by_shop_id_and_status(shop_id, status, options = {})
get_all("/shops/#{shop_id}/receipts/#{status}", options)
end

def self.submit_tracking_by_shop_id(shop_id, receipt_id, options = {})
options.merge!(:require_secure => true)
post("/shops/#{shop_id}/receipts/#{receipt_id}/tracking", options)
end

def created_at
Time.at(created)
end
Expand All @@ -34,11 +41,19 @@ def buyer
def transactions
unless @transactions
options = {}
options = options.merge(:access_token => token, :access_secret => secret) if (token && secret)
options = options.merge(access_token: token, access_secret: secret) if token && secret
@transactions = Transaction.find_all_by_receipt_id(id, options)
end
@transactions
end

def all_transactions
unless @all_transactions
options = {}
options = options.merge(access_token: token, access_secret: secret) if token && secret
@all_transactions = Transaction.find_all_by_receipt_id(id, options.merge({ limit: :all }))
end
@all_transactions
end
end
end
9 changes: 9 additions & 0 deletions lib/etsy/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def self.put(resource_path, parameters = {})
request = Request.new(resource_path, parameters)
Response.new(request.put)
end

def self.put_body(resource_path, parameters = {})
request = Request.new(resource_path, parameters)
Response.new(request.put_body)
end

def self.delete(resource_path, parameters = {})
request = Request.new(resource_path, parameters)
Expand Down Expand Up @@ -75,6 +80,10 @@ def post
def put
client.put(endpoint_url)
end

def put_body
client.put_body(endpoint_url(:include_query => false), @parameters)
end

def delete
client.delete(endpoint_url)
Expand Down
6 changes: 5 additions & 1 deletion lib/etsy/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ class Section
attribute :id, :from => :shop_section_id

def self.find_by_shop(shop)
get("/shops/#{shop.id}/sections")
get_all("/shops/#{shop.id}/sections")
end

def self.find(shop, id)
get("/shops/#{shop.id}/sections/#{id}")
end

def self.create(shop, options)
post("/shops/#{shop.id}/sections", options)
end
end
end
14 changes: 14 additions & 0 deletions lib/etsy/secure_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def post(endpoint)
def put(endpoint)
client.put(endpoint)
end

def put_body(endpoint, body)
# https = Net::HTTP.new(Etsy.host, Etsy.protocol == "http" ? 80 : 443)
# https.use_ssl = true if Etsy.protocol == "https"
#
# client.start do |http|
# req = Net::HTTP::Put.new(endpoint)
# add_oauth(req)
# req["Content-Type"] = "application/x-www-form-urlencoded"
# req.form_data = body
# res = http.request(req)
# end
client.put(endpoint, body, { 'Content-Type' => 'application/x-www-form-urlencoded' })
end

def delete(endpoint)
client.delete(endpoint)
Expand Down
20 changes: 20 additions & 0 deletions lib/etsy/taxonomy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Etsy
class Taxonomy
include Etsy::Model

attribute :id
attributes :level, :name, :parent, :parent_id, :path, :category_id, :children, :children_ids, :full_path_taxonomy_ids

def self.get_buyer_taxonomy
get_all('/taxonomy/buyer/get')
end

def self.get_seller_taxonomy
get_all('/taxonomy/seller/get')
end

def self.get_seller_taxonomy_version
get('/taxonomy/seller/version')
end
end
end
Loading