utility 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c405ee382d7806115552f2126152760ac81caf31
4
- data.tar.gz: 108299419e98711c7cc8e833bf31d3b69f2f8ecf
3
+ metadata.gz: efae9fe3c8901e514220f238a0afe301774f2fd5
4
+ data.tar.gz: c165887c69908712d15d18b31d4bb5cc8813e868
5
5
  SHA512:
6
- metadata.gz: 7c8254c38159d0d53a71ca236249018c88bd31b36cd9bdb85076c37708edc696020d5830398a239f25130026f0b95dfb3f36ca2844ad1668fc2d0ac1ab401183
7
- data.tar.gz: 3b9fa1484b6b3f3c257be312313e0e440690931ecf087f376efa933527216c259f0cdc35a2212c9cf50b860a0dfbd4bdd84f2deff8e30939a66309ac75249775
6
+ metadata.gz: f006ac97803b86d97b066e31e604e20808cf77a7a4bd5939ef4e5a1dbc0050f9e2a4b12cc5cb4cdb171db9a26d3f9d2d2ff2b9b5acdb8da7d5aa30738cf24600
7
+ data.tar.gz: b7d97c94abc25c22c9dd852b6c3cbb156ecace921b6e85a06b858517dc14d16df344ada9da75ece171ce4e71ffb9746020dc49df49c97f8c5f326439d8a873fd
data/README.md CHANGED
@@ -20,14 +20,14 @@ require "utility"
20
20
  #### base64 ####
21
21
  ```ruby
22
22
  # base64 encode
23
- Utility.base64encode "ruby" #=> cnVieQ==
24
- Utility.base64encode "utility" #=> dXRpbGl0eQ==
25
- Utility.base64encode "中文" #=> 5Lit5paH
23
+ Utility.base64encode "ruby" #=> "cnVieQ=="
24
+ Utility.base64encode "utility" #=> "dXRpbGl0eQ=="
25
+ Utility.base64encode "中文" #=> "5Lit5paH"
26
26
 
27
27
  # base64 decode
28
- Utility.base64decode "cnVieQ==" #=> ruby
29
- Utility.base64decode "dXRpbGl0eQ==" #=> utility
30
- Utility.base64decode "5Lit5paH" #=> 中文
28
+ Utility.base64decode "cnVieQ==" #=> "ruby"
29
+ Utility.base64decode "dXRpbGl0eQ==" #=> "utility"
30
+ Utility.base64decode "5Lit5paH" #=> "中文"
31
31
  ```
32
32
 
33
33
  #### md5/rmd/sha ####
@@ -59,16 +59,16 @@ Utility.url_escape "http://example.com/search?q=#ruby&from=深圳"
59
59
  #=> "http://example.com/search?q=%23ruby&from=%E6%B7%B1%E5%9C%B3"
60
60
  Utility.url_escape "http://example.com/search?q=#ruby&from=深圳", :all
61
61
  #=> "http%3A%2F%2Fexample.com%2Fsearch%3Fq%3D%23ruby%26from%3D%E6%B7%B1%E5%9C%B3"
62
- Utility.url_escape "深圳香港一水之隔"
63
- #=> "%E6%B7%B1%E5%9C%B3%E9%A6%99%E6%B8%AF%E4%B8%80%E6%B0%B4%E4%B9%8B%E9%9A%94"
62
+ Utility.url_escape "深圳"
63
+ #=> "%E6%B7%B1%E5%9C%B3"
64
64
 
65
65
  # url unescape
66
66
  Utility.url_unescape "http://example.com/search?q=%23ruby&from=%E6%B7%B1%E5%9C%B3"
67
67
  #=> "http://example.com/search?q=#ruby&from=深圳"
68
68
  Utility.url_unescape "http%3A%2F%2Fexample.com%2Fsearch%3Fq%3D%23ruby%26from%3D%E6%B7%B1%E5%9C%B3"
69
69
  #=> "http://example.com/search?q=#ruby&from=深圳"
70
- Utility.url_unescape "%E6%B7%B1%E5%9C%B3%E9%A6%99%E6%B8%AF%E4%B8%80%E6%B0%B4%E4%B9%8B%E9%9A%94"
71
- #=> "深圳香港一水之隔"
70
+ Utility.url_unescape "%E6%B7%B1%E5%9C%B3"
71
+ #=> "深圳"
72
72
 
73
73
  # html escape/unescape
74
74
  Utility.html_escape "<p>some text</p>" #=> "&lt;p&gt;some&nbsp;text&lt;/p&gt;"
@@ -82,21 +82,22 @@ Utility.html_text("<p>This <span>is</span> <p>plain</p> text</p>")
82
82
 
83
83
  #### computer info ####
84
84
  ```ruby
85
- # hostname
85
+ # hostname/ip/mac/os/arch
86
86
  Utility.get_hostname #=> "Charles"
87
-
88
- # ip address
89
87
  Utility.get_ip #=> "192.168.12.26"
90
-
91
- # mac address
92
88
  Utility.get_mac #=> "c8:60:00:10:ec:ee"
93
-
94
- # os
95
89
  Utility.get_os #=> "linux"
96
-
97
- # os arch
98
90
  Utility.get_arch #=> 64
99
91
  ```
100
92
 
93
+ #### try ####
94
+ ```ruby
95
+ # use 'try' as same as in rails
96
+ self.try :class #=> Object
97
+ self.try(:conf).try :ap_name #=> "irb"
98
+ Utility.try{ Date.current.year.size } #=> nil
99
+ #=> nil
100
+ ```
101
+
101
102
  ### License ###
102
103
  Released under the [MIT](http://opensource.org/licenses/MIT) license. See LICENSE file for details.
data/lib/utility.rb CHANGED
@@ -8,4 +8,5 @@ module Utility
8
8
  extend HMAC
9
9
  extend URL
10
10
  extend Computer
11
+ extend Try
11
12
  end
@@ -0,0 +1,38 @@
1
+ module Utility::Try
2
+ def try
3
+ yield rescue nil
4
+ end
5
+
6
+ unless Object.respond_to?(:try)
7
+ # refer: https://apidock.com/rails/v4.2.7/Object/try
8
+ Object.class_eval do
9
+ def try(*a, &b)
10
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
11
+ end
12
+
13
+ def try!(*a, &b)
14
+ if a.empty? && block_given?
15
+ if b.arity == 0
16
+ instance_eval(&b)
17
+ else
18
+ yield self
19
+ end
20
+ else
21
+ public_send(*a, &b)
22
+ end
23
+ end
24
+ end
25
+
26
+ NilClass.class_eval do
27
+ def try(*args)
28
+ nil
29
+ end
30
+
31
+ def try!(*args)
32
+ nil
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+
@@ -1,3 +1,3 @@
1
1
  module Utility
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cenxky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-03 00:00:00.000000000 Z
11
+ date: 2017-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: macaddr
@@ -101,6 +101,7 @@ files:
101
101
  - lib/utility/computer.rb
102
102
  - lib/utility/digest.rb
103
103
  - lib/utility/hmac.rb
104
+ - lib/utility/try.rb
104
105
  - lib/utility/url.rb
105
106
  - lib/utility/version.rb
106
107
  - utility.gemspec