Class: Soca::Pusher

Inherits:
Object
  • Object
show all
Defined in:
lib/soca/pusher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(app_dir) + '/'
  self.config_path = config_path
  load_config
  load_couchapprc
end

Instance Attribute Details

#app_dirObject

Returns the value of attribute app_dir.



3
4
5
# File 'lib/soca/pusher.rb', line 3

def app_dir
  @app_dir
end

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/soca/pusher.rb', line 4

def config
  @config
end

#config_pathObject

Returns the value of attribute config_path.



3
4
5
# File 'lib/soca/pusher.rb', line 3

def config_path
  @config_path
end

#documentObject

Returns the value of attribute document.



3
4
5
# File 'lib/soca/pusher.rb', line 3

def document
  @document
end

#envObject

Returns the value of attribute env.



3
4
5
# File 'lib/soca/pusher.rb', line 3

def env
  @env
end

#revisionObject

Returns the value of attribute revision.



3
4
5
# File 'lib/soca/pusher.rb', line 3

def revision
  @revision
end

Instance Method Details

#app_urlObject



63
64
65
# File 'lib/soca/pusher.rb', line 63

def app_url
  "#{push_url}/index.html"
end

#buildObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/soca/pusher.rb', line 32

def build
  @document = {}
  run_hooks!(: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_hooks!(:after_build)
  @document
end

#compact!Object



102
103
104
105
# File 'lib/soca/pusher.rb', line 102

def compact!
  logger.debug "compacting #{db_url}"
  post!("#{db_url}/_compact")
end

#create_db!Object



67
68
69
70
# File 'lib/soca/pusher.rb', line 67

def create_db!
  logger.debug "creating db: #{db_url}"
  put!(db_url)
end

#db_urlObject



48
49
50
51
52
53
54
55
56
# File 'lib/soca/pusher.rb', line 48

def db_url
  if env =~ /^https?\:\/\// # 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_revisionObject



72
73
74
75
76
77
78
79
80
# File 'lib/soca/pusher.rb', line 72

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

#jsonObject



44
45
46
# File 'lib/soca/pusher.rb', line 44

def json
  JSON.pretty_generate(build)
end

#load_configObject



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_couchapprcObject



26
27
28
29
30
# File 'lib/soca/pusher.rb', line 26

def load_couchapprc
  @config ||= {}
  @config['couchapprc'] = JSON.parse(File.read(File.join(app_dir, '.couchapprc')))
  run_hooks!(:after_load_couchapprc)
end

#loggerObject



107
108
109
# File 'lib/soca/pusher.rb', line 107

def logger
  Soca.logger
end

#pluginsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/soca/pusher.rb', line 135

def plugins
  return @plugins if @plugins
  if config['plugins']
    @plugins = config['plugins'].map do |plugin_config|
      plugin_name, plugin_options = [plugin_config].flatten
      require "soca/plugins/#{plugin_name}"
      plugin_options ||= {}
      plugin_options.each {|k, v| plugin_options[k.to_sym] = v }
      [plugin_name, Soca::Plugin.plugins[plugin_name].new(self, plugin_options)]
    end
  else
    @plugins = []
  end
  @plugins
end

#purge!Object



94
95
96
97
98
99
100
# File 'lib/soca/pusher.rb', line 94

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



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/soca/pusher.rb', line 82

def push!
  create_db!
  build
  run_hooks!(: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_hooks!(:after_push)
end

#push_urlObject



58
59
60
61
# File 'lib/soca/pusher.rb', line 58

def push_url
  raise "no app id specified in config" unless config['id']
  config['not_design'] ? "#{db_url}/#{config['id']}" : "#{db_url}/_design/#{config['id']}"
end

#run_hook_file!(hook) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/soca/pusher.rb', line 116

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

#run_hooks!(hook) ⇒ Object



111
112
113
114
# File 'lib/soca/pusher.rb', line 111

def run_hooks!(hook)
  run_hook_file!(hook)
  run_plugin_hooks!(hook)
end

#run_plugin_hooks!(hook) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/soca/pusher.rb', line 127

def run_plugin_hooks!(hook)
  plugins.each do |plugin_name, plugin|
    if plugin.respond_to?(hook)
      plugin.send(hook)
    end
  end
end