Class: Gilt::Sale

Inherits:
Object
  • Object
show all
Defined in:
lib/gilt/sale.rb

Overview

A Gilt Sale object, representing an active or upcoming “flash” sale.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sale_body, client = nil) ⇒ Sale

Returns a new instance of Sale.



36
37
38
39
# File 'lib/gilt/sale.rb', line 36

def initialize(sale_body, client=nil)
  @sale = sale_body
  @client = client
end

Class Method Details

.client(apikey, affid = nil) ⇒ Object



24
25
26
# File 'lib/gilt/sale.rb', line 24

def self.client(apikey, affid=nil)
  Gilt::Client::Sales.new(apikey, affid)
end

.create(store, sale_key, apikey, affid = nil) ⇒ Object



32
33
34
# File 'lib/gilt/sale.rb', line 32

def self.create(store, sale_key, apikey, affid=nil)
  detail(:store => store, :sale_key => sale_key, :apikey => apikey, :affid => affid).first
end

.product_client(apikey, affid = nil) ⇒ Object



28
29
30
# File 'lib/gilt/sale.rb', line 28

def self.product_client(apikey, affid=nil)
  Gilt::Client::Products.new(apikey, affid)
end

Instance Method Details

#beginsObject



69
70
71
# File 'lib/gilt/sale.rb', line 69

def begins
  Time.parse @sale["begins"]
end

#descriptionObject



65
66
67
# File 'lib/gilt/sale.rb', line 65

def description
  @sale["description"]
end

#durationObject



83
84
85
# File 'lib/gilt/sale.rb', line 83

def duration
  (ends - begins).ceil unless ends.nil?
end

#ended?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/gilt/sale.rb', line 78

def ended?
  return false if ends.nil?
  ends < Time.now
end

#endsObject



73
74
75
76
# File 'lib/gilt/sale.rb', line 73

def ends
  end_time = @sale["ends"]
  Time.parse end_time unless end_time.nil?
end

#imagesObject



61
62
63
# File 'lib/gilt/sale.rb', line 61

def images
  @sale["image_urls"]
end

#lengthObject



96
97
98
# File 'lib/gilt/sale.rb', line 96

def length
  return products.length
end

#nameObject



41
42
43
# File 'lib/gilt/sale.rb', line 41

def name
  @sale["name"]
end

#productsObject



87
88
89
90
91
92
93
94
# File 'lib/gilt/sale.rb', line 87

def products
  return @products unless @products.nil?
  resource = Gilt::Client::Products.resources[:detail]
  @products = (@sale["products"] || []).map do |product|
    id = resource.url.extract(product)["product_id"]
    Product.defer @client.detail(:product_id => id).perform
  end
end

#saleObject



53
54
55
# File 'lib/gilt/sale.rb', line 53

def sale
  URI(@sale["sale"])
end

#sale_keyObject



49
50
51
# File 'lib/gilt/sale.rb', line 49

def sale_key
  @sale["sale_key"]
end

#sale_urlObject



57
58
59
# File 'lib/gilt/sale.rb', line 57

def sale_url
  URI(@sale["sale_url"])
end

#storeObject



45
46
47
# File 'lib/gilt/sale.rb', line 45

def store
  @sale["store"].intern
end