Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'ebay'
- class ListingForm
- include ActiveModel::Model
- attr_accessor :title, :price, :subtitle, :product_id, :duration
- def initialize(form={})
- @title = form[:title]
- @price = form[:price]
- @subtitle = form[:subtitle]
- @product_id = form[:product_id]
- @duration = form[:duration]
- @product = set_product if @product_id
- end
- def post
- yield self if block_given?
- ebay_response = api.add_item(item: item)
- item_id = ebay_response.extract
- if valid_listing?(item_id)
- Listing.new(product_id: @product.id,
- item_number: item_id).save
- else
- raise "invalid item number"
- end
- ebay_response
- end
- def valid_listing?(item_id)
- item_id.to_i != 0
- end
- def item_specifics
- item_specifics = []
- @product.properties.each do |k,v|
- name = k.to_s.for_humans
- value = "#{v}#{PROPERTY_FORMATTER[k.to_sym]}"
- if value == "1"
- value = "included"
- elsif value == "0"
- next
- end
- item_specifics << Ebay::Types::NameValueList.new({name: name, value: value})
- end
- item_specifics
- end
- def remote_images
- remote_images = []
- @product.images.each do |i|
- url = "http://www.howmuchcomputer.com" + i
- remote_url = @api.upload_site_hosted_pictures({:external_picture_url => url})
- remote_images << remote_url.extract(ignore_errors: true)
- end
- remote_images.reverse!
- end
- def template(path)
- ERB.new(File.open(path, "r").read).result(binding).to_s
- end
- def item
- Ebay::Types::Item.new({
- :start_price => price.to_s || @product.price.to_s,
- :description => template("lib/ebay_templates/description.html.erb"),
- :condition_description => template("lib/ebay_templates/condition_description"),
- :listing_duration => "Days_#{duration}",
- :item_specifics => item_specifics,
- :listing_type => "FixedPriceItem",
- :title => title || @product.ebay_title,
- :currency => "USD",
- :country => "US",
- :postal_code => "21030",
- :global_shipping => "true",
- :payment_methods => ["PayPal", "VisaMC", "Discover"],
- :auto_pay => "true",
- :condition_id => @product.condition.to_s,
- :paypal_email_address => "info@EMAIL.com",
- :dispatch_time_max => 1,
- :picture_details => remote_images
- })
- end
- def set_product
- @product = Product.find(@product_id)
- end
- def api
- @api ||= Ebay::Api.new
- end
- end
- def initialize(form={})
- super # magic!
- @product = set_product if @product_id
- end
- def item_specifics
- @product.properties.reject { |k,v| v == "0" }.map do |k,v|
- value = "#{v}#{PROPERTY_FORMATTER[k.to_sym]}"
- value = "included" if value == "1"
- Ebay::Types::NameValueList.new(name: k.to_s.for_humans, value: value)
- end
- end
- def remote_images
- # Side note - where does the variable `url` come from? Document it!
- @product.images.map do |i|
- url = "http://www.howmuchcomputer.com" + i
- remote_url = @api.upload_site_hosted_pictures({:external_picture_url => url})
- remote_images << remote_url.extract(ignore_errors: true)
- end.reverse
- end
- # @todo refactor into controller! Violation of concerns!
- def template(path)
- ERB.new(File.open(path, "r").read).result(binding).to_s
- end
- def set_product
- @product = Product.find(@product_id)
- end
- def api
- @api ||= Ebay::Api.new
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement