Class: Store

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/store.rb

Overview

Copyright 2010-2011 Benjamin Lee Smith [email protected]

This file is part of CafePress Wrapper. CafePress Wrapper is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CafePress Wrapper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CafePress Wrapper. If not, see http://www.gnu.org/licenses/.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_all_stores_cafepress_data ⇒ Object



86
87
88
89
90
# File 'app/models/store.rb', line 86

def self.load_all_stores_cafepress_data
  Store.all.each do |store|
    store.load_cafepress_data
  end
end

.load_cafepress_store(cafepress_store_id) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/store.rb', line 21

def self.load_cafepress_store(cafepress_store_id)
  store = Store.find_or_create_by_cafepress_store_id(cafepress_store_id)
  cafepress_store_attributes = CafePressAPI.get_store(cafepress_store_id)
  store.description = cafepress_store_attributes[:description]
  store.title = cafepress_store_attributes[:title]
  store.save!
  store
end

.load_cafepress_store_and_products(cafepress_store_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/store.rb', line 30

def self.load_cafepress_store_and_products(cafepress_store_id)
  store = Store.load_cafepress_store(cafepress_store_id)

  store.products.destroy_all

  cafepress_store_products = CafePressAPI.get_store_products(cafepress_store_id)

  cafepress_store_products.each do |cp_product_attributes|
    # pull thumbnails out of hash
    image_urls = cp_product_attributes.delete(:image_urls)

    # pull sizes out of hash
    sizes = cp_product_attributes.delete(:sizes)

    # create product
    product = store.products.build(cp_product_attributes)

    # add thumbnails to product
    image_urls.each do |image_url_attributes|
      product.image_urls.build(image_url_attributes)
    end

    # add sizes to product
    sizes.each do |size_attributes|
      product.sizes.build(size_attributes)
    end
  end

  store.save!
  store

  design_url = CafePressAPI.get_design_url(store.products.first.cafepress_design_id)
  store.update_attributes(:cafepress_design_url => design_url)

  if store.products.first.cafepress_back_design_id
    design_url = CafePressAPI.get_design_url(store.products.first.cafepress_back_design_id)
    store.update_attributes(:cafepress_back_design_url => design_url)
  end
end

Instance Method Details

#load_cafepress_data ⇒ Object



82
83
84
# File 'app/models/store.rb', line 82

def load_cafepress_data
  Store.load_cafepress_store_and_products(self.cafepress_store_id)
end

#mens_products ⇒ Object



70
71
72
# File 'app/models/store.rb', line 70

def mens_products
  products.find_all_by_gender(CafePressAPI::MALE)
end

#unisex_products ⇒ Object



78
79
80
# File 'app/models/store.rb', line 78

def unisex_products
  products.find_all_by_gender(CafePressAPI::UNISEX)
end

#womens_products ⇒ Object



74
75
76
# File 'app/models/store.rb', line 74

def womens_products
  products.find_all_by_gender(CafePressAPI::FEMALE)
end