rest-more 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +6 -6
  2. data/.gitignore +1 -2
  3. data/.gitmodules +1 -1
  4. data/.travis.yml +5 -4
  5. data/CHANGES.md +1 -1
  6. data/Gemfile +16 -10
  7. data/README.md +209 -44
  8. data/Rakefile +11 -22
  9. data/doc/{tutorial/facebook.md → facebook.md} +1 -1
  10. data/example/multi.rb +3 -3
  11. data/example/rails3/Gemfile +18 -8
  12. data/example/rails3/app/controllers/application_controller.rb +2 -5
  13. data/example/rails3/config/application.rb +1 -1
  14. data/example/rails3/test/functional/application_controller_test.rb +0 -12
  15. data/example/rails3/test/test_helper.rb +12 -5
  16. data/example/rails3/test/unit/rails_util_test.rb +1 -6
  17. data/example/simple.rb +2 -2
  18. data/lib/rest-core/client/firebase.rb +50 -0
  19. data/lib/rest-core/client/instagram.rb +59 -0
  20. data/lib/rest-core/client/linkedin.rb +0 -4
  21. data/lib/rest-more.rb +8 -6
  22. data/lib/rest-more/test.rb +0 -11
  23. data/lib/rest-more/version.rb +1 -1
  24. data/rest-more.gemspec +20 -17
  25. data/task/README.md +54 -0
  26. data/task/gemgem.rb +151 -156
  27. data/test/dropbox/test_api.rb +1 -1
  28. data/test/facebook/test_api.rb +6 -7
  29. data/test/facebook/test_error.rb +1 -1
  30. data/test/facebook/test_handler.rb +2 -2
  31. data/test/facebook/test_load_config.rb +1 -1
  32. data/test/facebook/test_misc.rb +4 -14
  33. data/test/facebook/test_oauth.rb +1 -1
  34. data/test/facebook/test_old.rb +1 -1
  35. data/test/facebook/test_page.rb +4 -5
  36. data/test/facebook/test_parse.rb +0 -7
  37. data/test/facebook/test_serialize.rb +1 -1
  38. data/test/facebook/test_timeout.rb +4 -4
  39. data/test/instagram/test_api.rb +54 -0
  40. data/test/twitter/test_api.rb +1 -1
  41. metadata +26 -23
  42. data/example/rainbows.rb +0 -67
  43. data/task/.gitignore +0 -1
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 CHANGED
@@ -1,177 +1,171 @@
1
1
 
2
- require 'pathname'
3
-
4
2
  module Gemgem
5
3
  class << self
6
- attr_accessor :dir, :spec
4
+ attr_accessor :dir, :spec, :spec_create
7
5
  end
8
6
 
9
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
+
10
22
  def create
11
- yield(spec = Gem::Specification.new{ |s|
23
+ spec = Gem::Specification.new do |s|
12
24
  s.authors = ['Lin Jen-Shin (godfat)']
13
25
  s.email = ['godfat (XD) godfat.org']
14
26
 
15
27
  s.description = description.join
16
28
  s.summary = description.first
29
+ s.license = readme['LICENSE'].sub(/.+\n\n/, '').lines.first.strip
17
30
 
18
- s.rubygems_version = Gem::VERSION
19
- s.date = Time.now.strftime('%Y-%m-%d')
20
- s.files = gem_files
21
- s.test_files = gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
22
- s.executables = Dir['bin/*'].map{ |f| File.basename(f) }
23
- s.require_paths = %w[lib]
24
- })
25
- spec.homepage ||= "https://github.com/godfat/#{spec.name}"
26
- spec
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
27
39
  end
28
40
 
29
- def readme
30
- path = %w[README.md README].find{ |name|
31
- File.exist?("#{Gemgem.dir}/#{name}")
32
- }
33
- @readme ||=
34
- if path
35
- ps = "##{File.read(path)}".
36
- scan(/((#+)[^\n]+\n\n.+?(?=\n\n\2[^#\n]+\n))/m).map(&:first)
37
- ps.inject({'HEADER' => ps.first}){ |r, s, i|
38
- r[s[/\w+/]] = s
39
- r
40
- }
41
+ def write
42
+ File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
43
+ end
44
+
45
+ def split_lines ruby
46
+ ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
47
+ if $2.index(',')
48
+ "#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
41
49
  else
42
- {}
50
+ s
43
51
  end
52
+ }
44
53
  end
45
54
 
46
- def description
47
- @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
55
+ def strip_path path
56
+ strip_home_path(strip_cwd_path(path))
48
57
  end
49
58
 
50
- def changes
51
- path = %w[CHANGES.md CHANGES].find{ |name|
52
- File.exist?("#{Gemgem.dir}/#{name}")
53
- }
54
- @changes ||=
55
- if path
56
- date = '\d+{4}\-\d+{2}\-\d{2}'
57
- File.read(path).match(
58
- /([^\n]+#{date}\n\n(.+?))(?=\n\n[^\n]+#{date}\n|\Z)/m)[1]
59
- else
60
- ''
61
- end
59
+ def strip_home_path path
60
+ path.sub(ENV['HOME'], '~')
62
61
  end
63
62
 
64
- def ann_md
65
- "#{readme['HEADER'].sub(/([\w\-]+)/, "[\\1](#{spec.homepage})")}\n\n" \
66
- "##{readme['DESCRIPTION'][/[^\n]+\n\n[^\n]+/]}\n\n" \
67
- "### CHANGES:\n\n" \
68
- "###{changes}\n\n" \
69
- "##{readme['INSTALLATION']}\n\n" +
70
- if readme['SYNOPSIS'] then "##{readme['SYNOPSIS'][/[^\n]+\n\n[^\n]+/]}"
71
- else '' end
63
+ def strip_cwd_path path
64
+ path.sub(Dir.pwd, '.')
72
65
  end
73
66
 
74
- def ann_html
75
- gem 'nokogiri'
76
- gem 'kramdown'
77
-
78
- IO.popen('kramdown', 'r+') do |md|
79
- md.puts Gemgem.ann_md
80
- md.close_write
81
- require 'nokogiri'
82
- html = Nokogiri::XML.parse("<gemgem>#{md.read}</gemgem>")
83
- html.css('*').each{ |n| n.delete('id') }
84
- html.root.children.to_html
85
- end
67
+ def git *args
68
+ `git --git-dir=#{dir}/.git #{args.join(' ')}`
86
69
  end
87
70
 
88
- def ann_email
89
- "#{readme['HEADER'].sub(/([\w\-]+)/, "\\1 <#{spec.homepage}>")}\n\n" \
90
- "#{readme['DESCRIPTION']}\n\n" \
91
- "#{readme['INSTALLATION']}\n\n" +
92
- if readme['SYNOPSIS'] then "##{readme['SYNOPSIS']}\n\n" else '' end +
93
- "## CHANGES:\n\n" \
94
- "##{changes}\n\n"
71
+ def sh_git *args
72
+ Rake.sh('git', "--git-dir=#{dir}/.git", *args)
95
73
  end
96
74
 
97
- def gem_tag
98
- "#{spec.name}-#{spec.version}"
75
+ def sh_gem *args
76
+ Rake.sh(Gem.ruby, '-S', 'gem', *args)
99
77
  end
100
78
 
101
- def write
102
- File.open("#{dir}/#{spec.name}.gemspec", 'w'){ |f|
103
- f << split_lines(spec.to_ruby) }
79
+ def glob path=dir
80
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
104
81
  end
105
82
 
106
- def split_lines ruby
107
- ruby.gsub(/(.+?)\[(.+?)\]/){ |s|
108
- if $2.index(',')
109
- "#{$1}[\n #{$2.split(',').map(&:strip).join(",\n ")}]"
83
+ def readme
84
+ @readme ||=
85
+ if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
86
+ ps = "##{File.read(path)}".
87
+ scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
88
+ ps.inject('HEADER' => ps.first){ |r, s, i|
89
+ r[s[/\w+/]] = s
90
+ r
91
+ }
110
92
  else
111
- s
93
+ {}
112
94
  end
113
- }
95
+ end
96
+
97
+ def description
98
+ # JRuby String#lines is returning an enumerator
99
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
114
100
  end
115
101
 
116
102
  def all_files
117
- @all_files ||= find_files(Pathname.new(dir)).map{ |file|
118
- if file.to_s =~ %r{\.git/|\.git$}
119
- nil
103
+ @all_files ||= fold_files(glob).sort
104
+ end
105
+
106
+ def fold_files files
107
+ files.inject([]){ |r, path|
108
+ if File.file?(path) && path !~ %r{/\.git(/|$)} &&
109
+ (rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
110
+ r << rpath
111
+ elsif File.symlink?(path) # walk into symlinks...
112
+ r.concat(fold_files(glob(File.expand_path(path,
113
+ File.readlink(path)))))
120
114
  else
121
- file.to_s
115
+ r
122
116
  end
123
- }.compact.sort
117
+ }
124
118
  end
125
119
 
126
120
  def gem_files
127
- @gem_files ||= all_files - ignored_files
121
+ @gem_files ||= all_files.reject{ |f|
122
+ f =~ ignored_pattern && !git_files.include?(f)
123
+ }
128
124
  end
129
125
 
130
- def ignored_files
131
- @ignored_file ||= all_files.select{ |path| ignore_patterns.find{ |ignore|
132
- path =~ ignore && !git_files.include?(path)}}
126
+ def test_files
127
+ @test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
128
+ end
129
+
130
+ def bin_files
131
+ @bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
133
132
  end
134
133
 
135
134
  def git_files
136
135
  @git_files ||= if File.exist?("#{dir}/.git")
137
- `git ls-files`.split("\n")
136
+ git('ls-files').split("\n")
138
137
  else
139
138
  []
140
139
  end
141
140
  end
142
141
 
143
- # protected
144
- def find_files path
145
- path.children.select(&:file?).map{|file| file.to_s[(dir.size+1)..-1]} +
146
- path.children.select(&:directory?).map{|dir| find_files(dir)}.flatten
142
+ def ignored_files
143
+ @ignored_files ||= all_files.grep(ignored_pattern)
147
144
  end
148
145
 
149
- def ignore_patterns
150
- @ignore_files ||= expand_patterns(
151
- gitignore.split("\n").reject{ |pattern|
152
- pattern.strip == ''
153
- }).map{ |pattern| %r{^([^/]+/)*?#{Regexp.escape(pattern)}(/[^/]+)*?$} }
146
+ def ignored_pattern
147
+ @ignored_pattern ||= if gitignore.empty?
148
+ /^$/
149
+ else
150
+ Regexp.new(expand_patterns(gitignore).join('|'))
151
+ end
154
152
  end
155
153
 
156
154
  def expand_patterns pathes
157
- pathes.map{ |path|
158
- if path !~ /\*/
159
- path
160
- else
161
- expand_patterns(
162
- Dir[path] +
163
- Pathname.new(File.dirname(path)).children.select(&:directory?).
164
- map{ |prefix| "#{prefix}/#{File.basename(path)}" })
165
- end
166
- }.flatten
155
+ # http://git-scm.com/docs/gitignore
156
+ pathes.flat_map{ |path|
157
+ # we didn't implement negative pattern for now
158
+ Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
159
+ }
167
160
  end
168
161
 
169
162
  def gitignore
170
- if File.exist?(path = "#{dir}/.gitignore")
171
- File.read(path)
172
- else
173
- ''
174
- end
163
+ @gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
164
+ File.read(path).lines.
165
+ reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
166
+ else
167
+ []
168
+ end
175
169
  end
176
170
  end
177
171
 
@@ -179,22 +173,37 @@ namespace :gem do
179
173
 
180
174
  desc 'Install gem'
181
175
  task :install => [:build] do
182
- sh("#{Gem.ruby} -S gem install pkg/#{Gemgem.gem_tag}")
176
+ Gemgem.sh_gem('install', Gemgem.gem_path)
183
177
  end
184
178
 
185
179
  desc 'Build gem'
186
180
  task :build => [:spec] do
187
- sh("#{Gem.ruby} -S gem build #{Gemgem.spec.name}.gemspec")
188
- sh("mkdir -p pkg")
189
- sh("mv #{Gemgem.gem_tag}.gem pkg/")
181
+ require 'fileutils'
182
+ require 'rubygems/package'
183
+ gem = nil
184
+ Dir.chdir(Gemgem.dir) do
185
+ gem = Gem::Package.build(Gem::Specification.load(Gemgem.spec_path))
186
+ FileUtils.mkdir_p(Gemgem.pkg_dir)
187
+ FileUtils.mv(gem, Gemgem.pkg_dir) # gem is relative path, but might be ok
188
+ end
189
+ puts "\e[35mGem built: \e[33m" \
190
+ "#{Gemgem.strip_path("#{Gemgem.pkg_dir}/#{gem}")}\e[0m"
191
+ end
192
+
193
+ desc 'Generate gemspec'
194
+ task :spec do
195
+ Gemgem.create
196
+ Gemgem.write
190
197
  end
191
198
 
192
199
  desc 'Release gem'
193
200
  task :release => [:spec, :check, :build] do
194
- sh("git tag #{Gemgem.gem_tag}")
195
- sh("git push")
196
- sh("git push --tags")
197
- sh("#{Gem.ruby} -S gem push pkg/#{Gemgem.gem_tag}.gem")
201
+ Gemgem.module_eval do
202
+ sh_git('tag', Gemgem.gem_tag)
203
+ sh_git('push')
204
+ sh_git('push', '--tags')
205
+ sh_gem('push', Gemgem.gem_path)
206
+ end
198
207
  end
199
208
 
200
209
  task :check do
@@ -215,53 +224,39 @@ end
215
224
 
216
225
  end # of gem namespace
217
226
 
218
- desc 'Run tests in memory'
227
+ desc 'Run tests'
219
228
  task :test do
229
+ next if Gemgem.test_files.empty?
230
+
220
231
  require 'bacon'
221
232
  Bacon.extend(Bacon::TestUnitOutput)
222
233
  Bacon.summary_on_exit
223
- $LOAD_PATH.unshift('lib')
224
- Dir['./test/**/test_*.rb'].each{ |file| require file[0..-4] }
225
- end
226
-
227
- desc 'Run tests with shell'
228
- task 'test:shell', :RUBY_OPTS do |t, args|
229
- files = Dir['test/**/test_*.rb'].join(' ')
230
-
231
- cmd = [Gem.ruby, args[:RUBY_OPTS],
232
- '-I', 'lib', '-S', 'bacon', '--quiet', files]
233
-
234
- sh(cmd.compact.join(' '))
234
+ Gemgem.test_files.each{ |file| require "#{Gemgem.dir}/#{file[0..-4]}" }
235
235
  end
236
236
 
237
- desc 'Generate ann markdown'
238
- task 'ann:md' => ['gem:spec'] do
239
- puts Gemgem.ann_md
240
- end
237
+ desc 'Trash ignored files'
238
+ task :clean => ['gem:spec'] do
239
+ next if Gemgem.ignored_files.empty?
241
240
 
242
- desc 'Generate ann html'
243
- task 'ann:html' => ['gem:spec'] do
244
- puts Gemgem.ann_html
245
- end
241
+ require 'fileutils'
242
+ trash = File.expand_path("~/.Trash/#{Gemgem.spec.name}")
243
+ puts "Move the following files into:" \
244
+ " \e[35m#{Gemgem.strip_path(trash)}\e[33m"
246
245
 
247
- desc 'Generate ann email'
248
- task 'ann:email' => ['gem:spec'] do
249
- puts Gemgem.ann_email
250
- end
246
+ Gemgem.ignored_files.each do |file|
247
+ from = "#{Gemgem.dir}/#{file}"
248
+ to = "#{trash}/#{File.dirname(file)}"
249
+ puts Gemgem.strip_path(from)
251
250
 
252
- desc 'Generate rdoc'
253
- task :doc => ['gem:spec'] do
254
- sh("yardoc -o rdoc --main README.md" \
255
- " --files #{Gemgem.spec.extra_rdoc_files.join(',')}")
256
- end
251
+ FileUtils.mkdir_p(to)
252
+ FileUtils.mv(from, to)
253
+ end
257
254
 
258
- desc 'Remove ignored files'
259
- task :clean => ['gem:spec'] do
260
- trash = "~/.Trash/#{Gemgem.spec.name}/"
261
- sh "mkdir -p #{trash}" unless File.exist?(File.expand_path(trash))
262
- Gemgem.ignored_files.each{ |file| sh "mv #{file} #{trash}" }
255
+ print "\e[0m"
263
256
  end
264
257
 
265
258
  task :default do
266
- puts `#{Gem.ruby} -S #{$PROGRAM_NAME} -T`
259
+ # Is there a reliable way to do this in the current process?
260
+ # It failed miserably before between Rake versions...
261
+ exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
267
262
  end