simple_worker 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple_worker/service.rb +136 -7
  2. metadata +2 -2
@@ -2,6 +2,9 @@ require 'base64'
2
2
  require 'logger'
3
3
  require 'appoxy_api'
4
4
  require 'zip'
5
+ require 'rest_client'
6
+ require 'json'
7
+
5
8
  module SimpleWorker
6
9
 
7
10
  @@logger = Logger.new(STDOUT)
@@ -15,6 +18,134 @@ module SimpleWorker
15
18
  3
16
19
  end
17
20
 
21
+ module Api
22
+
23
+ # Subclass must define:
24
+ # host: endpoint url for service
25
+ class Client
26
+
27
+ @@logger = Logger.new(STDOUT)
28
+ @@logger.level = Logger::INFO
29
+
30
+ def self.logger
31
+ @@logger
32
+ end
33
+
34
+ attr_accessor :host, :access_key, :secret_key, :version
35
+
36
+ def initialize(host, access_key, secret_key, options={})
37
+ @host = host
38
+ @access_key = access_key
39
+ @secret_key = secret_key
40
+ end
41
+
42
+ def process_ex(ex)
43
+ body = ex.http_body
44
+ puts 'EX BODY=' + body.to_s
45
+ decoded_ex = JSON.parse(ex.http_body)
46
+ exception = Exception.new(ex.message+":"+decoded_ex["msg"])
47
+ exception.set_backtrace(decoded_ex["backtrace"].split(",")) if decoded_ex["backtrace"]
48
+ raise exception
49
+ end
50
+
51
+ def get(method, params={}, options={})
52
+ begin
53
+ # ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
54
+ parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers), options
55
+ rescue RestClient::BadRequest, RestClient::InternalServerError => ex
56
+ process_ex(ex)
57
+ end
58
+ end
59
+
60
+ def post_file(method, file, params={}, options={})
61
+ begin
62
+ parse_response RestClient.post(url(method), add_params(method, params).merge!({:file=>file}), :multipart => true), options
63
+ rescue RestClient::BadRequest, RestClient::InternalServerError => ex
64
+ process_ex(ex)
65
+ end
66
+ end
67
+
68
+ def post(method, params={}, options={})
69
+ begin
70
+ parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), options
71
+ #ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
72
+ rescue RestClient::BadRequest, RestClient::InternalServerError => ex
73
+ process_ex(ex)
74
+ end
75
+ end
76
+
77
+
78
+ def put(method, body, options={})
79
+ begin
80
+ parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), options
81
+ #ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
82
+ rescue RestClient::BadRequest, RestClient::InternalServerError => ex
83
+ process_ex(ex)
84
+ end
85
+ end
86
+
87
+ def delete(method, params={}, options={})
88
+ begin
89
+ parse_response RestClient.delete(append_params(url(method), add_params(method, params))), options
90
+ rescue RestClient::BadRequest, RestClient::InternalServerError => ex
91
+ process_ex(ex)
92
+ end
93
+ end
94
+
95
+ def url(command_path)
96
+ url = host + command_path
97
+ url
98
+ end
99
+
100
+ def add_params(command_path, hash)
101
+ v = version||"0.1"
102
+ ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
103
+ # puts 'timestamp = ' + ts
104
+ sig = case v
105
+ when "0.2"
106
+ Appoxy::Api::Signatures.generate_signature(command_path + Appoxy::Api::Signatures.hash_to_s(hash), ts, secret_key)
107
+ when "0.1"
108
+ Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
109
+ end
110
+
111
+ extra_params = {'sigv'=>v, 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
112
+ hash.merge!(extra_params)
113
+ end
114
+
115
+ def append_params(host, params)
116
+ host += "?"
117
+ i = 0
118
+ params.each_pair do |k, v|
119
+ host += "&" if i > 0
120
+ host += k + "=" + CGI.escape(v)
121
+ i +=1
122
+ end
123
+ return host
124
+ end
125
+
126
+ def headers
127
+ user_agent = "Appoxy API Ruby Client"
128
+ headers = {'User-Agent' => user_agent}
129
+ end
130
+
131
+ def parse_response(response, options={})
132
+ puts 'PARSE RESPONSE!'
133
+ unless options[:parse] == false
134
+ begin
135
+ return JSON.parse(response.to_s)
136
+ rescue => ex
137
+ puts 'response that caused error = ' + response.to_s
138
+ raise ex
139
+ end
140
+ else
141
+ response
142
+ end
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
18
149
  class Service < Appoxy::Api::Client
19
150
 
20
151
  attr_accessor :config
@@ -157,13 +288,13 @@ module SimpleWorker
157
288
  #File.open(fname2, "w") do |f|
158
289
  File.delete(fname2) if File.exist?(fname2)
159
290
  Zip::ZipFile.open(fname2, 'w') do |f|
160
- if merged_gems
291
+ if merged_gems && merged_gems.size > 0
161
292
  merged_gems.each do |gem|
162
293
  next unless gem[:merge]
163
294
  # puts 'gem=' + gem.inspect
164
295
  path = get_gem_path(gem)
165
296
  if path
166
- puts "Collecting gem #{path}"
297
+ SimpleWorker.logger.debug "Collecting gem #{path}"
167
298
  Dir["#{path}/**/**"].each do |file|
168
299
  # puts 'gem2=' + gem.inspect
169
300
  zdest = "gems/#{gem[:name]}/#{file.sub(path+'/', '')}"
@@ -180,11 +311,10 @@ module SimpleWorker
180
311
  # puts "merging #{m} into #{filename}"
181
312
  f.add(File.basename(m), m)
182
313
  end
183
- puts "merge models - done"
184
- if merged_mailers
185
- puts " MERGED MAILERS" + merged_mailers.inspect
314
+ if merged_mailers && merged_mailers.size > 0
315
+ # puts " MERGED MAILERS" + merged_mailers.inspect
186
316
  merged_mailers.each do |mailer|
187
- puts "Collecting mailer #{mailer[:name]}"
317
+ SimpleWorker.logger.debug "Collecting mailer #{mailer[:name]}"
188
318
  f.add(File.basename(mailer[:filename]), mailer[:filename])
189
319
  path = mailer[:path_to_templates]
190
320
  Dir["#{path}/**/**"].each do |file|
@@ -193,7 +323,6 @@ module SimpleWorker
193
323
  end
194
324
  end
195
325
  end
196
- puts "merging templates - done"
197
326
  end
198
327
  fname2
199
328
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.0
5
+ version: 0.6.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Travis Reeder
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-26 00:00:00 Z
13
+ date: 2011-05-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appoxy_api