Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. require 'ebay'
  2.  
  3. class ListingForm
  4. include ActiveModel::Model
  5.  
  6. attr_accessor :title, :price, :subtitle, :product_id, :duration
  7.  
  8. def initialize(form={})
  9. @title = form[:title]
  10. @price = form[:price]
  11. @subtitle = form[:subtitle]
  12. @product_id = form[:product_id]
  13. @duration = form[:duration]
  14. @product = set_product if @product_id
  15. end
  16.  
  17. def post
  18. yield self if block_given?
  19. ebay_response = api.add_item(item: item)
  20. item_id = ebay_response.extract
  21. if valid_listing?(item_id)
  22. Listing.new(product_id: @product.id,
  23. item_number: item_id).save
  24. else
  25. raise "invalid item number"
  26. end
  27. ebay_response
  28. end
  29.  
  30. def valid_listing?(item_id)
  31. item_id.to_i != 0
  32. end
  33.  
  34. def item_specifics
  35. item_specifics = []
  36. @product.properties.each do |k,v|
  37. name = k.to_s.for_humans
  38. value = "#{v}#{PROPERTY_FORMATTER[k.to_sym]}"
  39. if value == "1"
  40. value = "included"
  41. elsif value == "0"
  42. next
  43. end
  44. item_specifics << Ebay::Types::NameValueList.new({name: name, value: value})
  45. end
  46. item_specifics
  47. end
  48.  
  49. def remote_images
  50. remote_images = []
  51. @product.images.each do |i|
  52. url = "http://www.howmuchcomputer.com" + i
  53. remote_url = @api.upload_site_hosted_pictures({:external_picture_url => url})
  54. remote_images << remote_url.extract(ignore_errors: true)
  55. end
  56. remote_images.reverse!
  57. end
  58.  
  59. def template(path)
  60. ERB.new(File.open(path, "r").read).result(binding).to_s
  61. end
  62.  
  63. def item
  64. Ebay::Types::Item.new({
  65. :start_price => price.to_s || @product.price.to_s,
  66. :description => template("lib/ebay_templates/description.html.erb"),
  67. :condition_description => template("lib/ebay_templates/condition_description"),
  68. :listing_duration => "Days_#{duration}",
  69. :item_specifics => item_specifics,
  70. :listing_type => "FixedPriceItem",
  71. :title => title || @product.ebay_title,
  72. :currency => "USD",
  73. :country => "US",
  74. :postal_code => "21030",
  75. :global_shipping => "true",
  76. :payment_methods => ["PayPal", "VisaMC", "Discover"],
  77. :auto_pay => "true",
  78. :condition_id => @product.condition.to_s,
  79. :paypal_email_address => "info@EMAIL.com",
  80. :dispatch_time_max => 1,
  81. :picture_details => remote_images
  82. })
  83. end
  84.  
  85. def set_product
  86. @product = Product.find(@product_id)
  87. end
  88.  
  89. def api
  90. @api ||= Ebay::Api.new
  91. end
  92. end
  93.  
  94. def initialize(form={})
  95. super # magic!
  96. @product = set_product if @product_id
  97. end
  98.  
  99. def item_specifics
  100. @product.properties.reject { |k,v| v == "0" }.map do |k,v|
  101. value = "#{v}#{PROPERTY_FORMATTER[k.to_sym]}"
  102. value = "included" if value == "1"
  103. Ebay::Types::NameValueList.new(name: k.to_s.for_humans, value: value)
  104. end
  105. end
  106.  
  107. def remote_images
  108. # Side note - where does the variable `url` come from? Document it!
  109. @product.images.map do |i|
  110. url = "http://www.howmuchcomputer.com" + i
  111. remote_url = @api.upload_site_hosted_pictures({:external_picture_url => url})
  112. remote_images << remote_url.extract(ignore_errors: true)
  113. end.reverse
  114. end
  115.  
  116. # @todo refactor into controller! Violation of concerns!
  117. def template(path)
  118. ERB.new(File.open(path, "r").read).result(binding).to_s
  119. end
  120.  
  121. def set_product
  122. @product = Product.find(@product_id)
  123. end
  124.  
  125. def api
  126. @api ||= Ebay::Api.new
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement