Class: Shopifydev::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/shopifydev/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shop, project_root) ⇒ Template

Returns a new instance of Template.



9
10
11
12
# File 'lib/shopifydev/template.rb', line 9

def initialize(shop, project_root)
  @shop = shop
  @project_root = project_root
end

Instance Attribute Details

#project_rootObject

Returns the value of attribute project_root.



7
8
9
# File 'lib/shopifydev/template.rb', line 7

def project_root
  @project_root
end

#shopObject

Returns the value of attribute shop.



7
8
9
# File 'lib/shopifydev/template.rb', line 7

def shop
  @shop
end

Instance Method Details

#download(root = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shopifydev/template.rb', line 14

def download(root=nil)
  root ||= FileUtils.pwd
  shop.logger.info("Downloading all pages from #{shop.credentials['url']}")

  begin
    response = ShopifyAPI::Asset.find(:all) # get all them assets
    get_list_of_assets(response).each {| key, file | write_local_copy(file) }
  rescue SocketError => e
    puts e.message
    puts "Maybe check your internet connection?"
  rescue NoMethodError => e
    puts e.message
  rescue ActiveResource::UnauthorizedAccess => e
    puts e.message
    puts "Make sure you have the right password set in .shopifydev.yaml"
  rescue Exception => e
    puts e.message
  end
end

#get_asset(key) ⇒ Object



68
69
70
# File 'lib/shopifydev/template.rb', line 68

def get_asset(key)
  ShopifyAPI::Asset.find(key)
end

#get_list_of_assets(response) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/shopifydev/template.rb', line 58

def get_list_of_assets(response)
  response.reverse.inject({}) do | list, asset_info | 
    pathname = Pathname.new(asset_info.attributes["key"])
  path = pathname.to_path

  list[path] = pathname unless list.keys.include?(path + '.liquid')
  list
  end
end

#write_local_copy(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shopifydev/template.rb', line 34

def write_local_copy(file)

  directory_path = Pathname.new(@project_root +
                             File::Separator +
                             file.dirname.to_path)

  directory_path.mkpath unless (directory_path.exist?)

  begin
    puts "downloading #{file.basename}"
    asset = get_asset(file.to_path)
  rescue SocketError => e
    puts e.message
    puts "The connection was interupted while downloading #{file.to_path}."
  end

  # TODO maybe this should compare timestamps?
  File.open((directory_path.realdirpath + file.basename).to_path, 'w') do |f|
    puts "writing #{directory_path.to_path + File::Separator +  file.basename.to_path}"
    f.write(asset.value)
    puts "---"
  end
end