rest-builder 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+
2
+ require 'promise_pool/promise'
3
+
4
+ module RestBuilder
5
+ class Promise < PromisePool::Promise
6
+ class Future < PromisePool::Future
7
+ def initialize promise, key
8
+ super(promise)
9
+ @key = key
10
+ end
11
+
12
+ def method_missing msg, *args, &block
13
+ @promise.yield[@key].__send__(msg, *args, &block)
14
+ end
15
+
16
+ def respond_to_missing? msg, *args, &block
17
+ @promise.yield[@key].respond_to?(msg, *args, &block)
18
+ end
19
+ end
20
+
21
+ def future_status ; Future.new(self, RESPONSE_STATUS ); end
22
+ def future_headers ; Future.new(self, RESPONSE_HEADERS); end
23
+ def future_body ; Future.new(self, RESPONSE_BODY ); end
24
+ def future_socket ; Future.new(self, RESPONSE_SOCKET ); end
25
+ def future_failures; Future.new(self, FAIL ); end
26
+ def future_response
27
+ {RESPONSE_STATUS => future_status ,
28
+ RESPONSE_HEADERS => future_headers ,
29
+ RESPONSE_BODY => future_body ,
30
+ RESPONSE_SOCKET => future_socket ,
31
+ FAIL => future_failures,
32
+ PROMISE => self}
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+
2
+ require 'rest-builder'
3
+
4
+ require 'pork/auto'
5
+ require 'muack'
6
+ require 'webmock'
7
+
8
+ WebMock.disable_net_connect!(:allow_localhost => true)
9
+ Pork::Executor.include(Muack::API, WebMock::API)
10
+
11
+ class Pork::Executor
12
+ def with_img
13
+ f = Tempfile.new(['img', '.jpg'])
14
+ n = File.basename(f.path)
15
+ f.write('a'*10)
16
+ f.rewind
17
+ yield(f, n)
18
+ ensure
19
+ f.close!
20
+ end
21
+
22
+ def stub_select_for_stringio
23
+ stub(IO).select(where([is_a(StringIO)]), [], [],
24
+ RestBuilder::EventSource::READ_WAIT){ |rd, *| rd }
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module RestBuilder
3
+ VERSION = '0.9.0'
4
+ end
@@ -0,0 +1,73 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: rest-builder 0.9.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "rest-builder"
6
+ s.version = "0.9.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib"]
10
+ s.authors = ["Lin Jen-Shin (godfat)"]
11
+ s.date = "2016-01-31"
12
+ s.description = "Modular Ruby clients interface for REST APIs.\n\nBuild your own API clients for less dependencies, less codes, less memory,\nless conflicts, and run faster. Checkout [rest-core][] for pre-built\nmiddleware and [rest-more][] for pre-built clients.\n\n[rest-core]: https://github.com/godfat/rest-core\n[rest-more]: https://github.com/godfat/rest-more"
13
+ s.email = ["godfat (XD) godfat.org"]
14
+ s.files = [
15
+ ".gitignore",
16
+ ".gitmodules",
17
+ ".travis.yml",
18
+ "CHANGES.md",
19
+ "Gemfile",
20
+ "README.md",
21
+ "Rakefile",
22
+ "lib/rest-builder.rb",
23
+ "lib/rest-builder/builder.rb",
24
+ "lib/rest-builder/client.rb",
25
+ "lib/rest-builder/engine.rb",
26
+ "lib/rest-builder/engine/dry.rb",
27
+ "lib/rest-builder/engine/http-client.rb",
28
+ "lib/rest-builder/error.rb",
29
+ "lib/rest-builder/event_source.rb",
30
+ "lib/rest-builder/middleware.rb",
31
+ "lib/rest-builder/payload.rb",
32
+ "lib/rest-builder/promise.rb",
33
+ "lib/rest-builder/test.rb",
34
+ "lib/rest-builder/version.rb",
35
+ "rest-builder.gemspec",
36
+ "task/README.md",
37
+ "task/gemgem.rb",
38
+ "test/test_builder.rb",
39
+ "test/test_client.rb",
40
+ "test/test_event_source.rb",
41
+ "test/test_future.rb",
42
+ "test/test_httpclient.rb",
43
+ "test/test_payload.rb"]
44
+ s.homepage = "https://github.com/godfat/rest-builder"
45
+ s.licenses = ["Apache License 2.0"]
46
+ s.rubygems_version = "2.5.1"
47
+ s.summary = "Modular Ruby clients interface for REST APIs."
48
+ s.test_files = [
49
+ "test/test_builder.rb",
50
+ "test/test_client.rb",
51
+ "test/test_event_source.rb",
52
+ "test/test_future.rb",
53
+ "test/test_httpclient.rb",
54
+ "test/test_payload.rb"]
55
+
56
+ if s.respond_to? :specification_version then
57
+ s.specification_version = 4
58
+
59
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
+ s.add_runtime_dependency(%q<promise_pool>, [">= 0"])
61
+ s.add_runtime_dependency(%q<httpclient>, [">= 0"])
62
+ s.add_runtime_dependency(%q<mime-types>, [">= 0"])
63
+ else
64
+ s.add_dependency(%q<promise_pool>, [">= 0"])
65
+ s.add_dependency(%q<httpclient>, [">= 0"])
66
+ s.add_dependency(%q<mime-types>, [">= 0"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<promise_pool>, [">= 0"])
70
+ s.add_dependency(%q<httpclient>, [">= 0"])
71
+ s.add_dependency(%q<mime-types>, [">= 0"])
72
+ end
73
+ end
data/task/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Gemgem
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Provided tasks:
6
+
7
+ rake clean # Remove ignored files
8
+ rake gem:build # Build gem
9
+ rake gem:install # Install gem
10
+ rake gem:release # Release gem
11
+ rake gem:spec # Generate gemspec
12
+ rake test # Run tests in memory
13
+
14
+ ## REQUIREMENTS:
15
+
16
+ * Tested with MRI (official CRuby) 1.9.3, 2.0.0, Rubinius and JRuby.
17
+
18
+ ## INSTALLATION:
19
+
20
+ git submodule add git://github.com/godfat/gemgem.git task
21
+
22
+ And in Rakefile:
23
+
24
+ ``` ruby
25
+ begin
26
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
27
+ rescue LoadError
28
+ sh 'git submodule update --init'
29
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
30
+ end
31
+
32
+ Gemgem.init(dir) do |s|
33
+ s.name = 'your-gem'
34
+ s.version = '0.1.0'
35
+ end
36
+ ```
37
+
38
+ ## LICENSE:
39
+
40
+ Apache License 2.0
41
+
42
+ Copyright (c) 2011-2013, Lin Jen-Shin (godfat)
43
+
44
+ Licensed under the Apache License, Version 2.0 (the "License");
45
+ you may not use this file except in compliance with the License.
46
+ You may obtain a copy of the License at
47
+
48
+ <http://www.apache.org/licenses/LICENSE-2.0>
49
+
50
+ Unless required by applicable law or agreed to in writing, software
51
+ distributed under the License is distributed on an "AS IS" BASIS,
52
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
+ See the License for the specific language governing permissions and
54
+ limitations under the License.
data/task/gemgem.rb ADDED
@@ -0,0 +1,316 @@
1
+
2
+ module Gemgem
3
+ class << self
4
+ attr_accessor :dir, :spec, :spec_create
5
+ end
6
+
7
+ module_function
8
+ def gem_tag ; "#{spec.name}-#{spec.version}" ; end
9
+ def gem_path ; "#{pkg_dir}/#{gem_tag}.gem" ; end
10
+ def spec_path ; "#{dir}/#{spec.name}.gemspec" ; end
11
+ def pkg_dir ; "#{dir}/pkg" ; end
12
+ def escaped_dir; @escaped_dir ||= Regexp.escape(dir); end
13
+
14
+ def init dir, &block
15
+ self.dir = dir
16
+ $LOAD_PATH.unshift("#{dir}/lib")
17
+ ENV['RUBYLIB'] = "#{dir}/lib:#{ENV['RUBYLIB']}"
18
+ ENV['PATH'] = "#{dir}/bin:#{ENV['PATH']}"
19
+ self.spec_create = block
20
+ end
21
+
22
+ def create
23
+ spec = Gem::Specification.new do |s|
24
+ s.authors = ['Lin Jen-Shin (godfat)']
25
+ s.email = ['godfat (XD) godfat.org']
26
+
27
+ s.description = description.join
28
+ s.summary = description.first
29
+ s.license = readme['LICENSE'].sub(/.+\n\n/, '').lines.first.strip
30
+
31
+ s.date = Time.now.strftime('%Y-%m-%d')
32
+ s.files = gem_files
33
+ s.test_files = test_files
34
+ s.executables = bin_files
35
+ end
36
+ spec_create.call(spec)
37
+ spec.homepage ||= "https://github.com/godfat/#{spec.name}"
38
+ self.spec = spec
39
+ end
40
+
41
+ def gem_install
42
+ require 'rubygems/commands/install_command'
43
+ # read ~/.gemrc
44
+ Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
45
+ Gem::Command.extra_args = Gem.configuration[:gem]
46
+
47
+ # setup install options
48
+ cmd = Gem::Commands::InstallCommand.new
49
+ cmd.handle_options([])
50
+
51
+ # install
52
+ install = Gem::Installer.new(gem_path, cmd.options)
53
+ install.install
54
+ puts "\e[35mGem installed: \e[33m#{strip_path(install.gem_dir)}\e[0m"
55
+ end
56
+
57
+ def gem_spec
58
+ create
59
+ write
60
+ end
61
+
62
+ def gem_build
63
+ require 'fileutils'
64
+ require 'rubygems/package'
65
+ gem = nil
66
+ Dir.chdir(dir) do
67
+ gem = Gem::Package.build(Gem::Specification.load(spec_path))
68
+ FileUtils.mkdir_p(pkg_dir)
69
+ FileUtils.mv(gem, pkg_dir) # gem is relative path, but might be ok
70
+ end
71
+ puts "\e[35mGem built: \e[33m#{strip_path("#{pkg_dir}/#{gem}")}\e[0m"
72
+ end
73
+
74
+ def gem_release
75
+ sh_git('tag', gem_tag)
76
+ sh_git('push')
77
+ sh_git('push', '--tags')
78
+ sh_gem('push', gem_path)
79
+ end
80
+
81
+ def gem_check
82
+ unless git('status', '--porcelain').empty?
83
+ puts("\e[35mWorking copy is not clean.\e[0m")
84
+ exit(3)
85
+ end
86
+
87
+ ver = spec.version.to_s
88
+
89
+ if ENV['VERSION'].nil?
90
+ puts("\e[35mExpected " \
91
+ "\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
92
+ exit(1)
93
+
94
+ elsif ENV['VERSION'] != ver
95
+ puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
96
+ "\e[35mbut got\n " \
97
+ "\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
98
+ exit(2)
99
+ end
100
+ end
101
+
102
+ def test
103
+ return if test_files.empty?
104
+
105
+ if ENV['COV'] || ENV['CI']
106
+ require 'simplecov'
107
+ if ENV['CI']
108
+ begin
109
+ require 'coveralls'
110
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
111
+ rescue LoadError => e
112
+ puts "Cannot load coveralls, skip: #{e}"
113
+ end
114
+ end
115
+ SimpleCov.start do
116
+ add_filter('test/')
117
+ add_filter('test.rb')
118
+ end
119
+ end
120
+
121
+ test_files.each{ |file| require "#{dir}/#{file[0..-4]}" }
122
+ end
123
+
124
+ def clean
125
+ return if ignored_files.empty?
126
+
127
+ require 'fileutils'
128
+ trash = File.expand_path("~/.Trash/#{spec.name}")
129
+ puts "Move the following files into: \e[35m#{strip_path(trash)}\e[33m"
130
+
131
+ ignored_files.each do |file|
132
+ from = "#{dir}/#{file}"
133
+ to = "#{trash}/#{File.dirname(file)}"
134
+ puts strip_path(from)
135
+
136
+ FileUtils.mkdir_p(to)
137
+ FileUtils.mv(from, to)
138
+ end
139
+
140
+ print "\e[0m"
141
+ end
142
+
143
+ def write
144
+ File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
145
+ end
146
+
147
+ def split_lines ruby
148
+ ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
149
+ if $2.index(',')
150
+ "#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
151
+ else
152
+ s
153
+ end
154
+ }
155
+ end
156
+
157
+ def strip_path path
158
+ strip_home_path(strip_cwd_path(path))
159
+ end
160
+
161
+ def strip_home_path path
162
+ path.sub(ENV['HOME'], '~')
163
+ end
164
+
165
+ def strip_cwd_path path
166
+ path.sub(Dir.pwd, '.')
167
+ end
168
+
169
+ def git *args
170
+ `git --git-dir=#{dir}/.git #{args.join(' ')}`
171
+ end
172
+
173
+ def sh_git *args
174
+ Rake.sh('git', "--git-dir=#{dir}/.git", *args)
175
+ end
176
+
177
+ def sh_gem *args
178
+ Rake.sh(Gem.ruby, '-S', 'gem', *args)
179
+ end
180
+
181
+ def glob path=dir
182
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
183
+ end
184
+
185
+ def readme
186
+ @readme ||=
187
+ if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
188
+ ps = "##{File.read(path)}".
189
+ scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
190
+ ps.inject('HEADER' => ps.first){ |r, s, i|
191
+ r[s[/\w+/]] = s
192
+ r
193
+ }
194
+ else
195
+ {}
196
+ end
197
+ end
198
+
199
+ def description
200
+ # JRuby String#lines is returning an enumerator
201
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
202
+ end
203
+
204
+ def all_files
205
+ @all_files ||= fold_files(glob).sort
206
+ end
207
+
208
+ def fold_files files
209
+ files.inject([]){ |r, path|
210
+ if File.file?(path) && path !~ %r{/\.git(/|$)} &&
211
+ (rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
212
+ r << rpath
213
+ elsif File.symlink?(path) # walk into symlinks...
214
+ r.concat(fold_files(glob(File.expand_path(path,
215
+ File.readlink(path)))))
216
+ else
217
+ r
218
+ end
219
+ }
220
+ end
221
+
222
+ def gem_files
223
+ @gem_files ||= all_files.reject{ |f|
224
+ f =~ ignored_pattern && !git_files.include?(f)
225
+ }
226
+ end
227
+
228
+ def test_files
229
+ @test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
230
+ end
231
+
232
+ def bin_files
233
+ @bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
234
+ end
235
+
236
+ def git_files
237
+ @git_files ||= if File.exist?("#{dir}/.git")
238
+ git('ls-files').split("\n")
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+ def ignored_files
245
+ @ignored_files ||= all_files.grep(ignored_pattern)
246
+ end
247
+
248
+ def ignored_pattern
249
+ @ignored_pattern ||= if gitignore.empty?
250
+ /^$/
251
+ else
252
+ Regexp.new(expand_patterns(gitignore).join('|'))
253
+ end
254
+ end
255
+
256
+ def expand_patterns pathes
257
+ # http://git-scm.com/docs/gitignore
258
+ pathes.flat_map{ |path|
259
+ # we didn't implement negative pattern for now
260
+ Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
261
+ }
262
+ end
263
+
264
+ def gitignore
265
+ @gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
266
+ File.read(path).lines.
267
+ reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
268
+ else
269
+ []
270
+ end
271
+ end
272
+ end
273
+
274
+ namespace :gem do
275
+
276
+ desc 'Install gem'
277
+ task :install => [:build] do
278
+ Gemgem.gem_install
279
+ end
280
+
281
+ desc 'Build gem'
282
+ task :build => [:spec] do
283
+ Gemgem.gem_build
284
+ end
285
+
286
+ desc 'Generate gemspec'
287
+ task :spec do
288
+ Gemgem.gem_spec
289
+ end
290
+
291
+ desc 'Release gem'
292
+ task :release => [:spec, :check, :build] do
293
+ Gemgem.gem_release
294
+ end
295
+
296
+ task :check do
297
+ Gemgem.gem_check
298
+ end
299
+
300
+ end # of gem namespace
301
+
302
+ desc 'Run tests'
303
+ task :test do
304
+ Gemgem.test
305
+ end
306
+
307
+ desc 'Trash ignored files'
308
+ task :clean => ['gem:spec'] do
309
+ Gemgem.clean
310
+ end
311
+
312
+ task :default do
313
+ # Is there a reliable way to do this in the current process?
314
+ # It failed miserably before between Rake versions...
315
+ exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
316
+ end