quickdraw 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a8bd5091dd77c46a587472cf5655c812767927b
4
+ data.tar.gz: 85f9157dc564dd8144bcde74136b4745556baa85
5
+ SHA512:
6
+ metadata.gz: 8e558f6021374c82ed89b8531a5ffa8d4dc05e28f9867570435939040df3b5bd1e83848c3ec06f66fb9f72087097ebc5b232073a2f9ea2573efd2d5b2657b581
7
+ data.tar.gz: 609f01b571d75d0c81aaad16536ba9c0079c4be175dddf21f081a249fbb7f391c265595b61550dae3928fb4e44002c19903063096c9a0e66979520c873f7103b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quickdraw.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bryan Morris
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Quickdraw
2
+
3
+ The idea for Quickdraw comes from the 'shopify_theme' gem. I use a lot of code from them.
4
+
5
+ ### Features
6
+
7
+ - MUCH faster downloads and uploads. Quickdraw can download your files as much as 10x faster or more!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'quickdraw'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install quickdraw
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/quickdraw ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ def fallback_load_path(path)
4
+ retried = false
5
+ begin
6
+ yield
7
+ rescue LoadError
8
+ unless retried
9
+ $: << path
10
+ retried = true
11
+ retry
12
+ end
13
+ raise
14
+ end
15
+ end
16
+
17
+ fallback_load_path(File.join(File.dirname(__FILE__), '..', 'lib')) do
18
+ require 'quickdraw'
19
+ require 'quickdraw/shopify_connector'
20
+ require 'quickdraw/shopify_connector_pool'
21
+ require 'quickdraw/cli'
22
+ require 'celluloid'
23
+ end
24
+
25
+ Quickdraw::Cli.start(ARGV)
@@ -0,0 +1,53 @@
1
+ require 'erb'
2
+ require 'listen'
3
+ require 'pathname'
4
+
5
+
6
+ class Namespace
7
+ def initialize(hash={})
8
+ hash.each do |key, value|
9
+ singleton_class.send(:define_method, key) { value }
10
+ end
11
+ end
12
+
13
+ def get_binding
14
+ binding
15
+ end
16
+ end
17
+
18
+ @ns = Namespace.new()
19
+ @path = Dir.pwd
20
+ @watched_files = Pathname.glob("#{@path}/**/*.erb")
21
+
22
+ def compile_file(filepath)
23
+ if File.exists?(filepath)
24
+ pathname = Pathname.new(filepath)
25
+ relapath = pathname.relative_path_from(Pathname.pwd)
26
+ targetpath = relapath.to_s.gsub(/^src/, 'theme').gsub('.erb', '')
27
+ template = ERB.new(File.read(filepath))
28
+ File.write("#{targetpath}", template.result(@ns.get_binding))
29
+ end
30
+ end
31
+
32
+ #Compile all files on startup
33
+ puts "Compiling Files..."
34
+ @watched_files.each do |filepath|
35
+ compile_file(filepath)
36
+ end
37
+
38
+ puts "Watching for changes..."
39
+ listener = Listen.to(@path + '/src', :force_polling => true) do |modified, added, removed|
40
+ modified.each do |filepath|
41
+ puts "MODIFIED: #{filepath}"
42
+ compile_file(filepath)
43
+ end
44
+ added.each do |filepath|
45
+ puts "ADDED: #{filepath}"
46
+ compile_file(filepath)
47
+ end
48
+ removed.each do |filepath|
49
+ puts "REMOVED: #{filepath}"
50
+ end
51
+ end
52
+ listener.start
53
+ sleep
@@ -0,0 +1,92 @@
1
+ require 'thor'
2
+ #require 'yaml'
3
+ #YAML::ENGINE.yamler = 'syck' if defined? Syck
4
+ #require 'abbrev'
5
+ #require 'base64'
6
+ #require 'fileutils'
7
+ #require 'json'
8
+ require 'listen'
9
+ #require 'launchy'
10
+ require 'benchmark'
11
+
12
+ module Quickdraw
13
+ class Cli < Thor
14
+ include Thor::Actions
15
+
16
+ BINARY_EXTENSIONS = %w(png gif jpg jpeg eot svg ttf woff otf swf ico)
17
+ IGNORE = %w(config.yml)
18
+ DEFAULT_WHITELIST = %w(layout/ assets/ config/ snippets/ templates/)
19
+ TIMEFORMAT = "%H:%M:%S"
20
+
21
+ desc "download FILE", "download the shops current theme assets"
22
+ #method_option :quiet, :type => :boolean, :default => false
23
+ def download(filter)
24
+
25
+ asset_list = Celluloid::Actor[:shopify_connector].get_asset_list
26
+
27
+ if filter.empty?
28
+ assets = asset_list
29
+ else
30
+ assets = asset_list.select{ |i| i[/^#{filter}/] }
31
+ end
32
+
33
+ futures = []
34
+
35
+ assets.each do |asset|
36
+ futures << Celluloid::Actor[:shopify_connector].future.download_asset(asset)
37
+ end
38
+
39
+ futures.each do |future|
40
+ say("Downloaded: #{future.value}", :green)
41
+ end
42
+
43
+ say("Done.", :green) unless options['quiet']
44
+ end
45
+
46
+ desc "watch", "compile then upload and delete individual theme assets as they change, use the --keep_files flag to disable remote file deletion"
47
+ #method_option :quiet, :type => :boolean, :default => false
48
+ #method_option :keep_files, :type => :boolean, :default => false
49
+ def watch
50
+ puts "Watching current folder: #{Dir.pwd}"
51
+ listener = Listen.to(@path + '/src', :force_polling => true) do |modified, added, removed|
52
+ modified.each do |filePath|
53
+ filePath.slice!(Dir.pwd + "/")
54
+ send_asset(filePath, options['quiet']) if local_assets_list.include?(filePath)
55
+ end
56
+ added.each do |filePath|
57
+ filePath.slice!(Dir.pwd + "/")
58
+ send_asset(filePath, options['quiet']) if local_assets_list.include?(filePath)
59
+ end
60
+ unless options['keep_files']
61
+ removed.each do |filePath|
62
+ filePath.slice!(Dir.pwd + "/")
63
+ delete_asset(filePath, options['quiet']) if !local_assets_list.include?(filePath)
64
+ end
65
+ end
66
+ end
67
+ listener.start
68
+ sleep
69
+ rescue
70
+ puts "exiting...."
71
+ end
72
+
73
+ private
74
+
75
+ def download_asset(key)
76
+ #notify_and_sleep("Approaching limit of API permits. Naptime until more permits become available!") if ShopifyTheme.needs_sleep?
77
+ asset = ShopifyTheme.get_asset(key)
78
+ if asset['value']
79
+ # For CRLF line endings
80
+ content = asset['value'].gsub("\r", "")
81
+ format = "w"
82
+ elsif asset['attachment']
83
+ content = Base64.decode64(asset['attachment'])
84
+ format = "w+b"
85
+ end
86
+
87
+ FileUtils.mkdir_p(File.dirname(key))
88
+ File.open(key, format) {|f| f.write content} if content
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,64 @@
1
+
2
+ require 'httparty'
3
+ require 'celluloid'
4
+ require 'pathname'
5
+
6
+ module Quickdraw
7
+ class ShopifyConnector
8
+ include Celluloid
9
+
10
+ NOOPParser = Proc.new {|data, format| {} }
11
+
12
+ def initialize
13
+ @config = Quickdraw.config
14
+ @auth = {:username => @config[:username], :password => @config[:password]}
15
+ end
16
+
17
+ def get(path, options={})
18
+ options.merge!({:basic_auth => @auth})
19
+
20
+ response = Celluloid::Actor[:shopify_connector_pool].get(path, options)
21
+
22
+ return response
23
+ end
24
+
25
+ def get_asset_list(options={})
26
+ options.merge!({:parser => NOOPParser})
27
+ response = get("https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", options)
28
+
29
+ if JSON.parse(response.body)["assets"]
30
+ return JSON.parse(response.body)["assets"].collect {|a| a['key'] }
31
+ end
32
+
33
+ return nil
34
+ end
35
+
36
+ def download_asset(assetpath, options={})
37
+ options.merge!({:query => {:asset => {:key => assetpath}}, :parser => NOOPParser})
38
+
39
+ response = get("https://#{@config[:store]}/admin/themes/#{@config[:theme_id]}/assets.json", options)
40
+
41
+ # HTTParty json parsing is broken?
42
+ asset = response.code == 200 ? JSON.parse(response.body)["asset"] : {}
43
+ asset['response'] = response
44
+
45
+ if asset['value']
46
+ # For CRLF line endings
47
+ content = asset['value'].gsub("\r", "")
48
+ format = "w"
49
+ elsif asset['attachment']
50
+ content = Base64.decode64(asset['attachment'])
51
+ format = "w+b"
52
+ end
53
+
54
+ save_path = "theme/"+assetpath
55
+
56
+ FileUtils.mkdir_p(File.dirname(save_path))
57
+ File.open(save_path, format) {|f| f.write content} if content
58
+
59
+ return assetpath
60
+ end
61
+ end
62
+
63
+ ShopifyConnector.supervise_as(:shopify_connector)
64
+ end
@@ -0,0 +1,39 @@
1
+
2
+ require 'httparty'
3
+ require 'celluloid'
4
+
5
+ module Quickdraw
6
+ class ShopifyConnectorPool
7
+ include HTTParty
8
+ include Celluloid
9
+
10
+ def get(path, options = {})
11
+
12
+ begin
13
+ tries ||= 3
14
+
15
+ response = HTTParty.get(path, options)
16
+
17
+ if response.code == 429
18
+ tries += 1
19
+ puts "Too fast for Shopify! Retrying..."
20
+ raise "Slow down!"
21
+ end
22
+
23
+ if response.code != 200
24
+ puts response.inspect
25
+ raise "Request Failed"
26
+ end
27
+
28
+ rescue => e
29
+ sleep 1
30
+ retry unless (tries -= 1).zero?
31
+ end
32
+
33
+ return response
34
+ end
35
+
36
+ end
37
+
38
+ Celluloid::Actor[:shopify_connector_pool] = ShopifyConnectorPool.pool(:size => 16)
39
+ end
@@ -0,0 +1,3 @@
1
+ module Quickdraw
2
+ VERSION = "0.0.2"
3
+ end
data/lib/quickdraw.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "quickdraw/version"
2
+
3
+ module Quickdraw
4
+
5
+ NOOPParser = Proc.new {|data, format| {} }
6
+ TIMER_RESET = 5 * 60 + 5
7
+ PERMIT_LOWER_LIMIT = 10
8
+
9
+ def self.asset_list
10
+ # HTTParty parser chokes on assest listing, have it noop
11
+ # and then use a rel JSON parser.
12
+ response = Celluloid::Actor[:shopify_connector]
13
+ response = shopify.get(path, :parser => NOOPParser)
14
+ #manage_timer(response)
15
+
16
+ assets = JSON.parse(response.body)["assets"].collect {|a| a['key'] }
17
+ # Remove any .css files if a .css.liquid file exists
18
+ assets.reject{|a| assets.include?("#{a}.liquid") }
19
+ end
20
+
21
+ def self.config
22
+ @config ||= if File.exist? 'config.yml'
23
+ config = YAML.load(File.read('config.yml'))
24
+ config
25
+ else
26
+ puts "config.yml does not exist!"
27
+ {}
28
+ end
29
+ end
30
+
31
+ def self.get()
32
+ puts "Waiting: #{@sleeptime}"
33
+ sleep @sleeptime
34
+
35
+ options.merge!({:basic_auth => @auth})
36
+ HTTParty.get(path, options)
37
+
38
+ begin
39
+ tries ||= 3
40
+ response = HTTParty.get(path, options)
41
+
42
+ puts response.inspect
43
+
44
+ if response.code != 200
45
+ raise "Request Failed"
46
+ end
47
+
48
+ @api_call_count, @total_api_calls = response.headers['x-shopify-shop-api-call-limit'].split('/')
49
+ @sleeptime = (5 / (@total_api_calls.to_f / @api_call_count.to_f))
50
+
51
+ puts "STATUS: #{@sleeptime} - #{@api_call_count} - #{@total_api_calls}"
52
+
53
+ rescue => e
54
+ puts e.inspect
55
+ puts "Failed: will retry #{tries} time(s)"
56
+ sleep 1
57
+ retry unless (tries -= 1).zero?
58
+ end
59
+
60
+ return response
61
+
62
+ end
63
+
64
+
65
+ end
data/quickdraw.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quickdraw/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quickdraw"
8
+ spec.version = Quickdraw::VERSION
9
+ spec.authors = ["Bryan Morris"]
10
+ spec.email = ["bryan@internalfx.com"]
11
+ spec.description = %q{Quickly develop Shopify themes}
12
+ spec.summary = %q{Allows the use of ERB templates to develop and "compile" a theme and then automatically deploy to Shopify}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "thor"
25
+ spec.add_runtime_dependency "listen"
26
+ spec.add_runtime_dependency "celluloid"
27
+ spec.add_runtime_dependency "httparty"
28
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickdraw
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Morris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: listen
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: celluloid
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Quickly develop Shopify themes
98
+ email:
99
+ - bryan@internalfx.com
100
+ executables:
101
+ - quickdraw
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .ruby-version
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/quickdraw
112
+ - lib/auto_compile.rb
113
+ - lib/quickdraw.rb
114
+ - lib/quickdraw/cli.rb
115
+ - lib/quickdraw/shopify_connector.rb
116
+ - lib/quickdraw/shopify_connector_pool.rb
117
+ - lib/quickdraw/version.rb
118
+ - quickdraw.gemspec
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.1.11
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Allows the use of ERB templates to develop and "compile" a theme and then
143
+ automatically deploy to Shopify
144
+ test_files: []