ana 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 885136e219bf2a587db435b5a4767b8d33ebf3d1
4
- data.tar.gz: f4e738b169ebc6852532a01a7ba24cb268b766bf
3
+ metadata.gz: bd6dc3594ee445bfef54767794e7bb1eaf5b668f
4
+ data.tar.gz: 2b8b041844620b803938dd359d1f7bc110ce2017
5
5
  SHA512:
6
- metadata.gz: 881212a59ae759617978c7c90238030527719e2f35fec1e41a4969993d116f320b8577363e98dfc391128d37e7ab48ac229bcdb077fde39c0b32579554261293
7
- data.tar.gz: f6094dcf373a19c7a1e9f9531a68781792d3b86264956aabff7d93aae6495af5376bf2c18c0a1cf6a02d9344f87c4b84a5eaac2085eb62d69ea46ba357553695
6
+ metadata.gz: 6dd7b5db308bc2003a246bd33fa3c817178e55cc04ccaa15068c8b5c2e093b3a4d61cb3ea785b89bfe3b95298fd29725f4f9b8338b6ba03b1c8219e7767fcb00
7
+ data.tar.gz: e92dc593ae148b8458b0f523e982173ca654499576ffe25fdf690aba3667c54ee9e7a4ef39533aa1274a110460c35a3feb17570c16851eabe0ffa9a9d44120b2
data/lib/ana/command.rb CHANGED
@@ -41,7 +41,8 @@ module Ana
41
41
  # author(s), and license information.
42
42
  desc 'gem_infos (info)', 'Print Gem Informations'
43
43
  def gem_infos(gem)
44
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'gems')
44
+ return if gem_does_not_exist?(gem)
45
+ gem_hash = get_gem_json!(gem, type: 'gems')
45
46
  say gem_hash['info']
46
47
  say "Has been downloaded for #{colorize(gem_hash['downloads'], RED)} times!"
47
48
  say "The Latest version is #{colorize(gem_hash['version'], BLUE)}."
@@ -58,7 +59,8 @@ module Ana
58
59
  # type: 'runtime', 'development'
59
60
  desc 'gem_dependencies (deps)', 'Print Gem Dependencies (runtime / development).'
60
61
  def gem_dependencies(gem, type='runtime')
61
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'gems')
62
+ return if gem_does_not_exist?(gem)
63
+ gem_hash = get_gem_json!(gem, type: 'gems')
62
64
  gem_hash['dependencies'][type].each do |arr|
63
65
  puts "#{arr['name'].ljust(20)} #{arr['requirements']}"
64
66
  end
@@ -67,15 +69,21 @@ module Ana
67
69
  # Return latest version of given gem.
68
70
  desc 'latest_version (v)', 'latest version of a gem.'
69
71
  def latest_version(gem)
70
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'gems')
71
- say("Latest version is #{gem_hash['version']}.", :blue)
72
+ return if gem_does_not_exist?(gem)
73
+ gem_hash = get_gem_json!(gem, type: 'gems')
74
+ say("Latest version is #{gem_hash['version']}.", :blue) if gem_hash
72
75
  end
73
76
 
74
77
  # List versions of a given Gem, default will only list last 10 versions.
75
78
  # You can pass all or a fairly large number to display all versions.
76
79
  desc 'versions (vs)', 'List versions of a gem.'
77
80
  def versions(gem, count=10)
78
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'versions')
81
+ if number?(count.to_s) == false
82
+ say "#{count} is not a number :("
83
+ return
84
+ end
85
+ return if gem_does_not_exist?(gem)
86
+ gem_hash = get_gem_json!(gem, type: 'versions')
79
87
  if count == 'all' || count.to_i > gem_hash.count
80
88
  count = gem_hash.count
81
89
  end
@@ -85,10 +93,16 @@ module Ana
85
93
  end
86
94
  end
87
95
 
96
+ # FIXME should print info related to that version
88
97
  # Find if a given version of Gem exists.
89
98
  desc 'version exist?', 'Find if a given version exists.'
90
- def find_version(gem, ver)
91
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'versions')
99
+ def find_version(gem, ver='no-input')
100
+ return if gem_does_not_exist?(gem)
101
+ if ver == 'no-input'
102
+ say 'Please specify a version'
103
+ return
104
+ end
105
+ gem_hash = get_gem_json!(gem, type: 'versions')
92
106
  versions = gem_hash.collect { |x| x['number'] }
93
107
  if versions.include? ver
94
108
  gem_infos gem
@@ -100,12 +114,14 @@ module Ana
100
114
  # Search if a given Gem exists? If exists, return the latest version of it.
101
115
  desc 'search (s)', '(Exact) Search for a gem.'
102
116
  def search(gem)
103
- latest_version(gem) if gem_exist?(gem)
117
+ return if gem_does_not_exist?(gem)
118
+ latest_version(gem)
104
119
  end
105
120
 
106
121
  # Invoke system `gem search -r`
107
122
  desc 'fuzzy_search (fs)', 'Fuzzy search for a Gem'
108
123
  def fuzzy_search(gem)
124
+ return if gem_does_not_exist?(gem)
109
125
  system("gem search -r #{Shellwords.escape gem}")
110
126
  end
111
127
 
@@ -113,7 +129,8 @@ module Ana
113
129
  # Available URI types could be found in Ana::Scalars::URI_TYPE.
114
130
  desc 'open (o)', 'Open gem doc directly via open command.'
115
131
  def open(gem, open_type='doc')
116
- gem_hash = check_if_gem_exist_and_get_json!(gem, type: 'gems')
132
+ return if gem_does_not_exist?(gem)
133
+ gem_hash = get_gem_json!(gem, type: 'gems')
117
134
  url = if URI_TYPE.keys.include? open_type
118
135
  skip = false
119
136
  gem_hash[URI_TYPE[open_type]]
@@ -129,6 +146,7 @@ module Ana
129
146
  # Download a given Gem.
130
147
  desc 'download (dl)', 'Download a Gem'
131
148
  def download(gem)
149
+ return if gem_does_not_exist?(gem)
132
150
  open gem, 'lib'
133
151
  end
134
152
 
@@ -151,28 +169,55 @@ module Ana
151
169
  print "\n"
152
170
  end
153
171
 
154
- # Check if a Gem exists, if exists load the json and return.
155
- # The real load will execute every TTL (900) seconds.
156
- # type: 'versions', 'gems'
157
- # @return [Hash]
158
- def check_if_gem_exist_and_get_json!(gem, type: 'versions')
159
- if gem_exist?(gem)
160
- gem_uri = URI("https://rubygems.org/api/v1/#{type}/#{gem}.json")
161
- gem_json_file_path = full_path("~/.gemjsons/#{gem}/#{type}.json")
162
- unless File.exist?(gem_json_file_path) && fresh?(gem_json_file_path)
172
+ # Download json if it hasn't been downloaded
173
+ # or older than 900s.
174
+ # return Hash parsed from gem's (gems / version) json
175
+ def get_gem_json!(gem, type: 'versions')
176
+ gem_json_file_path = full_path("~/.gemjsons/#{gem}/#{type}.json")
177
+ gem_uri = URI.parse(URI.encode("https://rubygems.org/api/v1/#{type}/#{gem}.json", "[]"))
178
+ if !File.exist?(gem_json_file_path)
179
+ create_file(gem_json_file_path, Net::HTTP.get(gem_uri), verbose: false)
180
+ else
181
+ if not_fresh?(gem_json_file_path)
163
182
  remove_and_create_file!(gem_json_file_path, Net::HTTP.get(gem_uri))
164
183
  end
165
- else
166
- print_gem_not_found!
167
184
  end
168
- return JSON.parse(IO.read(gem_json_file_path))
185
+ return JSON.parse(IO.read(gem_json_file_path)) || nil
169
186
  end
170
187
 
171
- # Check if a given gem exists?
188
+ # Returns true if given gem exists.
172
189
  def gem_exist?(gem)
173
- uri = URI "https://rubygems.org/api/v1/gems/#{gem}.json"
174
- return false if GEM_NOT_FOUND == Net::HTTP.get(uri)
175
- return true
190
+ uri = URI.parse(URI.encode("https://rubygems.org/api/v1/gems/#{gem}.json", "[]"))
191
+ response = Net::HTTP.get(uri)
192
+ if response.include? 'page not found'
193
+ say "#{gem}, typo?"
194
+ return false
195
+ end
196
+ case response
197
+ when GEM_NOT_FOUND
198
+ print_gem_not_found!
199
+ return false
200
+ when GEM_DOES_NOT_EXIST
201
+ print_gem_does_not_exist!
202
+ return false
203
+ else
204
+ return true
205
+ end
206
+ end
207
+
208
+ # Returns true if given gem does not exist
209
+ def gem_does_not_exist?(gem)
210
+ !gem_exist?(gem)
211
+ end
212
+
213
+ def number?(str)
214
+ return false if(str =~ /\A[-+]?[0-9]*\.?[0-9]+\z/).nil?
215
+ true
216
+ end
217
+
218
+ # Print Gem does not exist message.
219
+ def print_gem_does_not_exist!
220
+ say(GEM_DOES_NOT_EXIST, :red)
176
221
  end
177
222
 
178
223
  # Print Gem version not found message.
@@ -201,8 +246,8 @@ module Ana
201
246
  end
202
247
 
203
248
  # Check if a file is has been changed within 900s?
204
- def fresh?(file_path)
205
- access_time(file_path) < TTL
249
+ def not_fresh?(file_path)
250
+ access_time(file_path) > TTL
206
251
  end
207
252
 
208
253
  # Return a file's change time with respect to current time.
data/lib/ana/constants.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Ana
2
2
  module Scalars
3
3
  TTL = 900 # Every 900s updates the json.
4
- GEM_NOT_FOUND = 'This rubygem could not be found.'
5
- GEM_VER_NOT_FOUND = 'This version could not be found.'
4
+ GEM_NOT_FOUND = 'This rubygem could not be found.'
5
+ GEM_DOES_NOT_EXIST = 'This gem does not exist.'
6
+ GEM_VER_NOT_FOUND = 'This version could not be found.'
6
7
 
7
8
  # Terminal Colour Codes
8
9
  # http://en.wikipedia.org/wiki/ANSI_escape_code
data/lib/ana/version.rb CHANGED
@@ -2,7 +2,7 @@ module Ana
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 9
5
- TINY = 3
5
+ TINY = 4
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ana
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito Fatas