Class: EbayProducts

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ebay_products.rb,
lib/ebay_products/data.rb

Defined Under Namespace

Classes: Data, ProductInformation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, appid) ⇒ EbayProducts

Returns a new instance of EbayProducts.



15
16
17
18
19
# File 'lib/ebay_products.rb', line 15

def initialize(args, appid)
  @query = args[:keywords]
  @product_id = args[:product_id]
  @appid = appid
end

Instance Attribute Details

#appidObject (readonly)

Returns the value of attribute appid.



13
14
15
# File 'lib/ebay_products.rb', line 13

def appid
  @appid
end

#product_idObject (readonly)

Returns the value of attribute product_id.



13
14
15
# File 'lib/ebay_products.rb', line 13

def product_id
  @product_id
end

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/ebay_products.rb', line 13

def query
  @query
end

Instance Method Details

#optionsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/ebay_products.rb', line 44

def options
  hash = {:appid => @appid}
  if @product_id
    hash['ProductID.value'.to_sym] = @product_id
    hash['ProductID.type'.to_sym] = 'Reference' # assumes product id is of type reference
  else
    hash[:QueryKeywords] = @query
  end
  hash
end

#productObject



40
41
42
# File 'lib/ebay_products.rb', line 40

def product
  products.first
end

#productsObject



36
37
38
# File 'lib/ebay_products.rb', line 36

def products
  @products ||= search.collect {|product| ProductInformation.new(product) }
end

#searchObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ebay_products.rb', line 21

def search
  if @search.blank?
    response = self.class.get("/shopping", :query => options, :format => :xml)["FindProductsResponse"]
    errors = response['Errors']
    if errors
      raise SearchFailure, errors['LongMessage']
    end
    @search = response["Product"]
    if @search.is_a? Hash
      @search = [@search]
    end
  end
  @search
end