Class: Quickdraw::ShopifyConnector

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/quickdraw/shopify_connector.rb

Constant Summary collapse

NOOPParser =
Proc.new {|data, format| {} }
BINARY_EXTENSIONS =
%w(png gif jpg jpeg eot svg ttf woff otf swf ico)
IGNORE =
%w(config.yml)
DEFAULT_WHITELIST =
%w(layout/ assets/ config/ snippets/ templates/)
TIMEFORMAT =
"%H:%M:%S"

Instance Method Summary collapse

Constructor Details

#initializeShopifyConnector

Returns a new instance of ShopifyConnector.



17
18
19
20
# File 'lib/quickdraw/shopify_connector.rb', line 17

def initialize
	@config = Quickdraw.config
	@auth = {:username => @config[:api_key], :password => @config[:password]}
end

Instance Method Details

#compile_asset(asset) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/quickdraw/shopify_connector.rb', line 92

def compile_asset(asset)
	if File.exists?(asset.to_s)
		target_asset = "theme/#{asset.relative_to(Quickdraw.src_dir).to_s.gsub('.erb', '')}"
		template = ERB.new(File.read(asset))
		File.write("#{target_asset}", template.result)
	end

	return [asset, nil]
end

#download_asset(asset, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/quickdraw/shopify_connector.rb', line 52

def download_asset(asset, options={})
	options.merge!({:query => {:asset => {:key => asset.relative_to(Quickdraw.theme_dir).to_s}}, :parser => NOOPParser})

	response = Celluloid::Actor[:shopify_connector_pool].request(:get, "https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", options)

	# HTTParty json parsing is broken?
	data = response.code == 200 ? JSON.parse(response.body)["asset"] : {}
	data['response'] = response

	if data['value']
		# For CRLF line endings
		content = data['value'].gsub("\r", "")
		format = "w"
	elsif data['attachment']
		content = Base64.decode64(data['attachment'])
		format = "w+b"
	end

	FileUtils.mkdir_p(File.dirname(asset))
	File.open(asset, format) {|f| f.write content} if content

	return [asset, response]
end

#get_asset_list(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/quickdraw/shopify_connector.rb', line 22

def get_asset_list(options={})
	options.merge!({:parser => NOOPParser})
	response = Celluloid::Actor[:shopify_connector_pool].request(:get, "https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", options)

	if JSON.parse(response.body)["assets"]
		return JSON.parse(response.body)["assets"].collect {|a| a['key'] }
	end

	return nil
end

#is_binary_data?(string) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/quickdraw/shopify_connector.rb', line 84

def is_binary_data?(string)
	if string.respond_to?(:encoding)
		string.encoding == "US-ASCII"
	else
		( string.count( "^ -~", "^\r\n" ).fdiv(string.size) > 0.3 || string.index( "\x00" ) ) unless string.empty?
	end
end

#remove_asset(asset, options = {}) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/quickdraw/shopify_connector.rb', line 76

def remove_asset(asset, options={})
	options.merge!({:body => {:asset => {:key => asset.relative_to(Quickdraw.theme_dir).to_s}}})

	response = Celluloid::Actor[:shopify_connector_pool].request(:delete, "https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", options)

	return [asset, response]
end

#upload_asset(asset) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/quickdraw/shopify_connector.rb', line 33

def upload_asset(asset)
	time = Time.now
	data = {:key => asset.relative_to(Quickdraw.theme_dir).to_s}

	content = File.read(asset)
	if BINARY_EXTENSIONS.include?(File.extname(asset).gsub('.','')) || is_binary_data?(content)
		content = File.open(asset, "rb") { |io| io.read }
		data.merge!(:attachment => Base64.encode64(content))
	else
		data.merge!(:value => content)
	end

	data = {:body => {:asset => data}}

	response = Celluloid::Actor[:shopify_connector_pool].request(:put, "https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", data)

	return [asset, response]
end