flappy-cli 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/CODE_OF_CONDUCT.md +74 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +41 -0
  7. data/Rakefile +2 -0
  8. data/bin/console +14 -0
  9. data/bin/flappy +14 -0
  10. data/bin/setup +8 -0
  11. data/flappy-cli.gemspec +50 -0
  12. data/lib/flappy/api.yml +7 -0
  13. data/lib/flappy/cli.rb +145 -0
  14. data/lib/flappy/patches/blank.rb +131 -0
  15. data/lib/flappy/patches/concern.rb +146 -0
  16. data/lib/flappy/patches/default_headers.rb +9 -0
  17. data/lib/flappy/patches/hash.rb +79 -0
  18. data/lib/flappy/patches/instance_variables.rb +30 -0
  19. data/lib/flappy/patches/native_patch.rb +28 -0
  20. data/lib/flappy/patches/os_patch.rb +28 -0
  21. data/lib/flappy/patches/try.rb +102 -0
  22. data/lib/flappy/patches.rb +12 -0
  23. data/lib/flappy/util/archive_ipa.rb +55 -0
  24. data/lib/flappy/util/build_apk.rb +104 -0
  25. data/lib/flappy/util/build_common.rb +90 -0
  26. data/lib/flappy/util/build_ipa.rb +331 -0
  27. data/lib/flappy/util/config.rb +41 -0
  28. data/lib/flappy/util/http.rb +30 -0
  29. data/lib/flappy/util/iOS_check.rb +29 -0
  30. data/lib/flappy/util/iOS_env_config.rb +54 -0
  31. data/lib/flappy/util/iOS_logger.rb +113 -0
  32. data/lib/flappy/util/info.rb +39 -0
  33. data/lib/flappy/util/ipa_info.rb +198 -0
  34. data/lib/flappy/util/mapping.rb +84 -0
  35. data/lib/flappy/util/multi_io.rb +27 -0
  36. data/lib/flappy/util/parser/apk.rb +42 -0
  37. data/lib/flappy/util/parser/bin/pngcrush +0 -0
  38. data/lib/flappy/util/parser/common.rb +24 -0
  39. data/lib/flappy/util/parser/pngcrush.rb +23 -0
  40. data/lib/flappy/util/publish.rb +185 -0
  41. data/lib/flappy/util/update_pod.rb +264 -0
  42. data/lib/flappy/util.rb +105 -0
  43. data/lib/flappy/version.rb +3 -0
  44. data/lib/flappy-cli.rb +3 -0
  45. data/lib/flappy.rb +28 -0
  46. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000308/20170103000308_WorkspaceForPackageTest.json +1 -0
  47. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000308/20170103000308_WorkspaceForPackageTest_Podfile +16 -0
  48. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000308/20170103000308_WorkspaceForPackageTest_Podfile_lock +40 -0
  49. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000314/20170103000314_WorkspaceForPackageTest.json +1 -0
  50. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000314/20170103000314_WorkspaceForPackageTest_Podfile +16 -0
  51. data/output/Flappy-Archives/iOS/WorkspaceForPackageTest/20170103000314/20170103000314_WorkspaceForPackageTest_Podfile_lock +40 -0
  52. metadata +223 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef22db1617f751383041aa44ba0e6ed4e0dcd25851d110d4ed3f28a2e267e8d8
4
+ data.tar.gz: 06e916a505338769d307f501bc8740d54fe93ebd8e85d88d73d151485445a812
5
+ SHA512:
6
+ metadata.gz: b72a085ded596b1de1d11337975b51a437b8da99d5eeb7cc6a1790accbb5471f6d1e3f7d0febd2f184aa98987e6ba27b6cb0d29dfbc541643b54ac1dabff6a3b
7
+ data.tar.gz: f9445566613bf94346c1b6cf4859aea03b6e1a926ff82c7426415aca72e3024e6a20511c590a4ca0423876dba3808553ac1d05d3161dac3d20b48fddbbb1284d
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 545025590@qq.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+ if ENV['USE_OFFICIAL_GEM_SOURCE']
3
+ source 'https://rubygems.org'
4
+ else
5
+ source 'https://ruby.taobao.org'
6
+ end
7
+
8
+ # Specify your gem's dependencies in flappy-cli.gemspec
9
+ gemspec
10
+
11
+ gem 'codeclimate-test-reporter', group: :test, require: nil
12
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 warmwei818
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Flappy::Cli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/flappy/cli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'flappy-cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install flappy-cli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flappy-cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flappy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/flappy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require_relative '../lib/flappy'
5
+
6
+ begin
7
+ OS.set_locale
8
+ Flappy::CLI.start ARGV
9
+ rescue => e
10
+ raise e if $DEBUG
11
+ STDERR.puts e.message
12
+ STDERR.puts e.backtrace.join("\n")
13
+ exit 1
14
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flappy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flappy-cli"
8
+ spec.version = Flappy::VERSION
9
+ spec.authors = ["warmwei818, yangyao"]
10
+ spec.email = ["545025590@qq.com, yangyao@souche.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "http://git.souche.com/flappy/flappy-cli"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+
30
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
32
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
33
+ spec.require_paths = ["lib"]
34
+ spec.post_install_message = %q(-----> Flappy <-----)
35
+
36
+
37
+ spec.add_development_dependency 'bundler', '~> 1.13'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'minitest', '~> 5.7'
40
+ # spec.add_development_dependency 'pry', '~> 0.10'
41
+ # spec.add_development_dependency 'pry-nav', '~> 0.2.4'
42
+
43
+ spec.add_dependency 'thor', '~> 0.19'
44
+ spec.add_dependency 'ruby_android', '~> 0.7'
45
+ spec.add_dependency 'CFPropertyList', '~> 2.3'
46
+ spec.add_dependency 'rest-client', '~> 1.7'
47
+ spec.add_dependency 'rqrcode', '~> 0.7'
48
+ spec.add_dependency 'cocoapods'
49
+
50
+ end
@@ -0,0 +1,7 @@
1
+ fir:
2
+ domain: 'http://fir.im'
3
+ base_url: 'http://api.fir.im'
4
+ user_url: 'http://api.fir.im/user'
5
+ app_url: 'http://api.fir.im/apps'
6
+ udids_url: 'http://api.fir.im/devices/multi_udid'
7
+
data/lib/flappy/cli.rb ADDED
@@ -0,0 +1,145 @@
1
+ require 'flappy/version'
2
+ require 'thor'
3
+
4
+ module Flappy
5
+ class CLI < Thor
6
+ class_option :firToken, type: :string, aliases: '-T', desc: "User's API Token at fir.im"
7
+ class_option :logfile, type: :string, aliases: '-L', desc: 'Path to writable logfile'
8
+ class_option :verbose, type: :boolean, aliases: '-V', desc: 'Show verbose', default: true
9
+ class_option :quiet, type: :boolean, aliases: '-q', desc: '安静模式,只显示必要的信息'
10
+ class_option :help, type: :boolean, aliases: '-h', desc: 'Show this help message and quit'
11
+
12
+ desc 'build_apk BUILD_DIR', 'Build Android app (alias: `ba`).'
13
+ long_desc <<-LONGDESC
14
+ `build_apk` command will auto build your project to an apk package
15
+ and it also can auto publish your built apk to fir.im if use `-p` option.
16
+ Internally, it use `gradle` to accomplish these things, use `gradle --help` to get more information.
17
+
18
+ Example:
19
+
20
+ $ flappy ba <project dir> [-o <apk output dir> -c <changelog> -p -Q -T <your api token>]
21
+
22
+ $ flappy ba <project dir> [-f <flavor> -o <apk output dir> -c <changelog> -p -Q -T <your api token>]
23
+
24
+ $ flappy ba <git ssh url> [-B develop -o <apk output dir> -c <changelog> -p -Q -T <your api token>]
25
+ LONGDESC
26
+ map ['ba'] => :build_apk
27
+ method_option :branch, type: :string, aliases: '-B', desc: 'Set branch if project is a git repo, the default is `master`'
28
+ method_option :output, type: :string, aliases: '-o', desc: 'APK output path, the default is: BUILD_DIR/build/outputs/apk'
29
+ method_option :publish, type: :boolean, aliases: '-p', desc: 'true/false if publish to fir.im'
30
+ method_option :flavor, type: :string, aliases: '-f', desc: 'Set flavor if have productFlavors'
31
+ method_option :debug, type: :boolean, aliases: '-d', desc: 'Set buildType if debug or release', default: false
32
+ method_option :short, type: :string, aliases: '-s', desc: 'Set custom short link if publish to fir.im'
33
+ method_option :name, type: :string, aliases: '-n', desc: 'Set custom apk name when builded'
34
+ method_option :changelog, type: :string, aliases: '-c', desc: 'Set changelog if publish to fir.im, support string/file'
35
+ method_option :qrcode, type: :boolean, aliases: '-Q', desc: 'Generate qrcode'
36
+ method_option :open, type: :boolean, desc: 'true/false if open for everyone, the default is: true', default: true
37
+ method_option :password, type: :string, desc: 'Set password for app'
38
+ def build_apk(*args)
39
+ prepare :build_apk
40
+
41
+ Flappy.build_apk(*args, options)
42
+ end
43
+
44
+ desc 'build_ipa BUILD_DIR [options] [settings]', 'Build iOS app (alias: `bi`).'
45
+ long_desc <<-LONGDESC
46
+ `build_ipa` command will auto build your project/workspace to an ipa package
47
+
48
+ Example:
49
+
50
+ $ flappy bi <workspace dir> -S <scheme name> [-C <configuration>] [-t <target name>] [-o <ipa output dir>] [settings] [-c <changelog>] [-p -Q -T <your api token>]
51
+ $ flappy bi <project dir> [-C <configuration>] [-t <target name>] [-o <ipa output dir>] [settings] [-c <changelog>] [-p -Q -T <your api token>]
52
+ $ flappy bi <git ssh url> [-B <branch>] [-C <configuration>] [-t <target name>] [-o <ipa output dir>]
53
+
54
+ LONGDESC
55
+
56
+ map ['bi'] => :build_ipa
57
+ ######### pod
58
+ method_option :'update-pod', type: :boolean, desc: '是否更新pod`'
59
+ method_option :'no-repo-update', type: :boolean, desc: '安装pod时不更新cocoapod库'
60
+ method_option :'only-update-souche-pod', type: :boolean, desc: '是否只更新车牛pod'
61
+ method_option :'pod-env-test', type: :boolean, desc: '是否依赖测试环境pod'
62
+
63
+ ######### other
64
+ method_option :open, type: :boolean, desc: '打包完成后是否打开文件夹'
65
+ method_option :'no-savelog', type: :boolean, desc: '是否保存打包日志'
66
+
67
+ ######## 环境
68
+ method_option :'https', type: :string, desc: "是否开启htts, 'Y' or 'N'"
69
+ method_option :'env', type: :string, desc: '主环境'
70
+ method_option :'port', type: :string, desc: '主环境端口'
71
+ method_option :'pay-env', type: :string, desc: '支付环境'
72
+
73
+ ######## package
74
+ method_option :branch, type: :string, aliases: '-B', desc: '打包分支,默认为master'
75
+ method_option :scheme, type: :string, aliases: '-S', desc: 'build the scheme NAME'
76
+ method_option :configuration, type: :string, aliases: '-C', desc: 'use the build configuration NAME for building each target'
77
+ method_option :target, type: :string, aliases: '-t', desc: 'build the target NAME'
78
+ method_option :output, type: :string, aliases: '-o', desc: 'IPA等打包信息的存放路径'
79
+
80
+ def build_ipa(*args)
81
+ prepare :build_ipa
82
+ Flappy.build_ipa(*args, options)
83
+ end
84
+
85
+ desc 'archive_ipa', '归档build文件为ipa'
86
+ def archive_ipa(*args)
87
+ prepare :archive_ipa
88
+ Flappy.archive_ipa(*args, options)
89
+ end
90
+
91
+
92
+ desc 'publish APP_FILE_PATH', 'Publish iOS/Android app to fir.im, support ipa/apk file (aliases: `p`).'
93
+ long_desc <<-LONGDESC
94
+ `publish` command will publish your app file to fir.im, also the command support to publish app's short & changelog.
95
+
96
+ Example:
97
+
98
+ $ flappy p <app file path> [-c <changelog> -s <custom short link> -Q -T <your api token>]
99
+
100
+ $ flappy p <app file path> [-c <changelog> -s <custom short link> --password=123456 --open=false -Q -T <your api token>]
101
+
102
+ $ flappy p <app file path> [-c <changelog> -s <custom short link> -m <mapping file path> -P <bughd project id> -Q -T <your api token>]
103
+ LONGDESC
104
+ map 'p' => :publish
105
+ method_option :short, type: :string, aliases: '-s', desc: 'Set custom short link'
106
+ method_option :changelog, type: :string, aliases: '-c', desc: 'Set changelog'
107
+ method_option :qrcode, type: :boolean, aliases: '-Q', desc: 'Generate qrcode'
108
+ method_option :mappingfile, type: :string, aliases: '-m', desc: 'App mapping file'
109
+ method_option :proj, type: :string, aliases: '-P', desc: 'Project id in BugHD.com if upload app mapping file'
110
+ method_option :open, type: :boolean, desc: 'true/false if open for everyone'
111
+ method_option :password, type: :string, desc: 'Set password for app'
112
+ def publish(*args)
113
+ prepare :publish
114
+
115
+ Flappy.publish(*args, options)
116
+ end
117
+
118
+ desc 'help', 'Describe available commands or one specific command (aliases: `h`).'
119
+ map Thor::HELP_MAPPINGS => :help
120
+ def help(command = nil, subcommand = false)
121
+ super
122
+ end
123
+
124
+ no_commands do
125
+ def invoke_command(command, *args)
126
+ logfile = options[:logfile].blank? ? STDOUT : options[:logfile]
127
+ logfile = '/dev/null' if options[:quiet]
128
+
129
+ Flappy.logger = Logger.new(logfile)
130
+ Flappy.logger.level = options[:verbose] ? Logger::INFO : Logger::ERROR
131
+ super
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def prepare(task)
138
+ if options.help?
139
+ help(task.to_s)
140
+ fail SystemExit
141
+ end
142
+ $DEBUG = true if ENV['DEBUG']
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+
3
+ class Object
4
+ # An object is blank if it's false, empty, or a whitespace string.
5
+ # For example, '', ' ', +nil+, [], and {} are all blank.
6
+ #
7
+ # This simplifies
8
+ #
9
+ # address.nil? || address.empty?
10
+ #
11
+ # to
12
+ #
13
+ # address.blank?
14
+ #
15
+ # @return [true, false]
16
+ def blank?
17
+ respond_to?(:empty?) ? !!empty? : !self
18
+ end
19
+
20
+ # An object is present if it's not blank.
21
+ #
22
+ # @return [true, false]
23
+ def present?
24
+ !blank?
25
+ end
26
+
27
+ # Returns the receiver if it's present otherwise returns +nil+.
28
+ # <tt>object.presence</tt> is equivalent to
29
+ #
30
+ # object.present? ? object : nil
31
+ #
32
+ # For example, something like
33
+ #
34
+ # state = params[:state] if params[:state].present?
35
+ # country = params[:country] if params[:country].present?
36
+ # region = state || country || 'US'
37
+ #
38
+ # becomes
39
+ #
40
+ # region = params[:state].presence || params[:country].presence || 'US'
41
+ #
42
+ # @return [Object]
43
+ def presence
44
+ self if present?
45
+ end
46
+ end
47
+
48
+ class NilClass
49
+ # +nil+ is blank:
50
+ #
51
+ # nil.blank? # => true
52
+ #
53
+ # @return [true]
54
+ def blank?
55
+ true
56
+ end
57
+ end
58
+
59
+ class FalseClass
60
+ # +false+ is blank:
61
+ #
62
+ # false.blank? # => true
63
+ #
64
+ # @return [true]
65
+ def blank?
66
+ true
67
+ end
68
+ end
69
+
70
+ class TrueClass
71
+ # +true+ is not blank:
72
+ #
73
+ # true.blank? # => false
74
+ #
75
+ # @return [false]
76
+ def blank?
77
+ false
78
+ end
79
+ end
80
+
81
+ class Array
82
+ # An array is blank if it's empty:
83
+ #
84
+ # [].blank? # => true
85
+ # [1,2,3].blank? # => false
86
+ #
87
+ # @return [true, false]
88
+ alias_method :blank?, :empty?
89
+ end
90
+
91
+ class Hash
92
+ # A hash is blank if it's empty:
93
+ #
94
+ # {}.blank? # => true
95
+ # { key: 'value' }.blank? # => false
96
+ #
97
+ # @return [true, false]
98
+ alias_method :blank?, :empty?
99
+ end
100
+
101
+ class String
102
+ BLANK_RE = /\A[[:space:]]*\z/
103
+
104
+ # A string is blank if it's empty or contains whitespaces only:
105
+ #
106
+ # ''.blank? # => true
107
+ # ' '.blank? # => true
108
+ # "\t\n\r".blank? # => true
109
+ # ' blah '.blank? # => false
110
+ #
111
+ # Unicode whitespace is supported:
112
+ #
113
+ # "\u00a0".blank? # => true
114
+ #
115
+ # @return [true, false]
116
+ def blank?
117
+ BLANK_RE === self
118
+ end
119
+ end
120
+
121
+ class Numeric #:nodoc:
122
+ # No number is blank:
123
+ #
124
+ # 1.blank? # => false
125
+ # 0.blank? # => false
126
+ #
127
+ # @return [false]
128
+ def blank?
129
+ false
130
+ end
131
+ end