Class: Soca::Pusher
- Inherits:
-
Object
- Object
- Soca::Pusher
- Defined in:
- lib/soca/pusher.rb
Instance Attribute Summary collapse
-
#app_dir ⇒ Object
Returns the value of attribute app_dir.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_path ⇒ Object
Returns the value of attribute config_path.
-
#document ⇒ Object
Returns the value of attribute document.
-
#env ⇒ Object
Returns the value of attribute env.
-
#revision ⇒ Object
Returns the value of attribute revision.
Instance Method Summary collapse
- #app_url ⇒ Object
- #build ⇒ Object
- #compact! ⇒ Object
- #create_db! ⇒ Object
- #db_url ⇒ Object
- #get_current_revision ⇒ Object
-
#initialize(app_dir, env = 'default', config_path = nil) ⇒ Pusher
constructor
A new instance of Pusher.
- #json ⇒ Object
- #load_config ⇒ Object
- #load_couchapprc ⇒ Object
- #logger ⇒ Object
- #plugin(plugin_name, options = {}) ⇒ Object
- #purge! ⇒ Object
- #push! ⇒ Object
- #push_url ⇒ Object
- #run_hook_file!(hook) ⇒ Object
Constructor Details
#initialize(app_dir, env = 'default', config_path = nil) ⇒ Pusher
Returns a new instance of Pusher.
6 7 8 9 10 11 12 |
# File 'lib/soca/pusher.rb', line 6 def initialize(app_dir, env = 'default', config_path = nil) self.env = env self.app_dir = File.(app_dir) + '/' self.config_path = config_path load_config load_couchapprc end |
Instance Attribute Details
#app_dir ⇒ Object
Returns the value of attribute app_dir.
3 4 5 |
# File 'lib/soca/pusher.rb', line 3 def app_dir @app_dir end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/soca/pusher.rb', line 4 def config @config end |
#config_path ⇒ Object
Returns the value of attribute config_path.
3 4 5 |
# File 'lib/soca/pusher.rb', line 3 def config_path @config_path end |
#document ⇒ Object
Returns the value of attribute document.
3 4 5 |
# File 'lib/soca/pusher.rb', line 3 def document @document end |
#env ⇒ Object
Returns the value of attribute env.
3 4 5 |
# File 'lib/soca/pusher.rb', line 3 def env @env end |
#revision ⇒ Object
Returns the value of attribute revision.
3 4 5 |
# File 'lib/soca/pusher.rb', line 3 def revision @revision end |
Instance Method Details
#app_url ⇒ Object
62 63 64 |
# File 'lib/soca/pusher.rb', line 62 def app_url "#{push_url}/index.html" end |
#build ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/soca/pusher.rb', line 31 def build @document = {} run_hook_file!(:before_build) logger.debug "building app JSON" Dir.glob(app_dir + '**/**') do |path| next if File.directory?(path) @document = map_file(path, @document) end run_hook_file!(:after_build) @document end |
#compact! ⇒ Object
101 102 103 104 |
# File 'lib/soca/pusher.rb', line 101 def compact! logger.debug "compacting #{db_url}" post!("#{db_url}/_compact") end |
#create_db! ⇒ Object
66 67 68 69 |
# File 'lib/soca/pusher.rb', line 66 def create_db! logger.debug "creating db: #{db_url}" put!(db_url) end |
#db_url ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/soca/pusher.rb', line 47 def db_url if env =~ /^http\:\/\// # the env is actual a db_url env else env_config = config['couchapprc']['env'][env] raise "No such env: #{env}" unless env_config && env_config['db'] env_config['db'] end end |
#get_current_revision ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/soca/pusher.rb', line 71 def get_current_revision logger.debug "getting current revision" current = get!(push_url) if current current_json = JSON.parse(current) self.revision = current_json['_rev'] logger.debug "current revision: #{revision}" end end |
#json ⇒ Object
43 44 45 |
# File 'lib/soca/pusher.rb', line 43 def json JSON.pretty_generate(build) end |
#load_config ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/soca/pusher.rb', line 18 def load_config if File.readable?(config_path) @config = JSON.parse(File.read(config_path)) else raise "Could not find config at '#{config_path}'. Run `soca init`" end end |
#load_couchapprc ⇒ Object
26 27 28 29 |
# File 'lib/soca/pusher.rb', line 26 def load_couchapprc @config ||= {} @config['couchapprc'] = JSON.parse(File.read(File.join(app_dir, '.couchapprc'))) end |
#plugin(plugin_name, options = {}) ⇒ Object
121 122 123 124 125 |
# File 'lib/soca/pusher.rb', line 121 def plugin(plugin_name, = {}) require "soca/plugins/#{plugin_name}" p = Soca::Plugin.plugins[plugin_name].new(self) p.run() end |
#purge! ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/soca/pusher.rb', line 93 def purge! get_current_revision url = push_url url += "?rev=#{revision}" if revision && revision.length > 0 logger.debug "deleting document at #{url}" delete!(url) end |
#push! ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/soca/pusher.rb', line 81 def push! create_db! build run_hook_file!(:before_push) get_current_revision document['_rev'] = revision if revision post_body = JSON.generate(document) logger.debug "pushing document to #{push_url}" put!(push_url, post_body) run_hook_file!(:after_push) end |
#push_url ⇒ Object
57 58 59 60 |
# File 'lib/soca/pusher.rb', line 57 def push_url raise "no app id specified in config" unless config['id'] "#{db_url}/_design/#{config['id']}" end |
#run_hook_file!(hook) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/soca/pusher.rb', line 110 def run_hook_file!(hook) hook_file_path = File.join(app_dir, 'hooks', "#{hook}.rb") if File.readable? hook_file_path logger.debug "running hook: #{hook} (#{hook_file_path})" Dir.chdir(app_dir) do instance_eval(File.read(hook_file_path)) end logger.debug "finished hook: #{hook} (#{hook_file_path})" end end |