Class: Shopifydev::Asset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shop, path = ENV['TM_FILEPATH'], project_root) ⇒ Asset

Returns a new instance of Asset.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/shopifydev/asset.rb', line 8

def initialize(shop, path=ENV['TM_FILEPATH'], project_root)
  puts "in asset::initialize"
  @shop = shop

  # Asset name should be relative to TM_PROJECT_DIRECTORY
  # but if it isn't, then gsub just doesn't replace anything
  # so if you know the key, you can just supply the key?
  unless project_root.nil?
    @remote_key = path.gsub(project_root + '/', '') # fix absolute path
    @local_path = Pathname.new(project_root + '/' + @remote_key) # prepend project root
  else
    raise Exception, "could no determine project_root. In .shopifydev.yaml include\n  project_root: relative/path"
  end
end

Instance Attribute Details

#remote_keyObject

Returns the value of attribute remote_key.



6
7
8
# File 'lib/shopifydev/asset.rb', line 6

def remote_key
  @remote_key
end

#shopObject

Returns the value of attribute shop.



6
7
8
# File 'lib/shopifydev/asset.rb', line 6

def shop
  @shop
end

Instance Method Details

#upload(root = nil) ⇒ Object



23
24
25
26
27
28
29
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
# File 'lib/shopifydev/asset.rb', line 23

def upload(root=nil)
  # check that it's not binary 
  puts "in upload"

  puts "creating asset for " + @remote_key
  puts @remote_key
  asset = ShopifyAPI::Asset.new(:key => @remote_key)

  puts "filling with 1's and 0's from " + @local_path.to_path

  contents = File.read(@local_path.to_path)

  fm = FileMagic.new

  if (fm.file(@local_path.realpath.to_path).include?('image'))
    asset.attach contents
  else
    asset.value = contents
  end

  # TODO this doesn't fail spectacularly enough... I don't 
  # feel comfortable with that
  puts "saving..."
  if asset.valid?
    if asset.save
      puts "Success!"
    else
      puts "failed to save"
    end
  else
    puts "failed to validate"
  end
  puts "---"
end