nice_http 1.7.6 → 1.7.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -8
  3. data/lib/nice_http.rb +37 -5
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67837a2fd29de2c94b9d15fd1c006bc187a7670ad3374ec586e361a842416131
4
- data.tar.gz: a88c42fa24de6ba8d29ca0ac942e5881d37a7bbe05fc3a091b86140e5decc8fe
3
+ metadata.gz: fd198745893c0373b41fd3f1a07ec5869459c3afac0b6312e6c30b39978ab358
4
+ data.tar.gz: c55402d3c84057c198d0a9a21f185abc3af336ef97286d55f45f452475398835
5
5
  SHA512:
6
- metadata.gz: ab3750b660b2261585d87a0e9272480656c08e16d3f8a70ede08092e1bc5640a1768c53011ba0e628484375b4323bbb3ce6ffc2750e1bcd8302753982b5ca443
7
- data.tar.gz: 94e73fdf83b857432acd0a2ae7b63aa34485eb3527c8bbe6f43a115238082cacd6efdf94e0d6bb5f3e280e1a9b1e98bb1f4d8124956520289d0ff2e38cd8af28
6
+ metadata.gz: 6e7edb12e4ba71ff70e8756611fb4d8ead683d4b2d343bd397c49bea2b47afba73fbd5fa742309c265c3f8de492a466c0d625d6e1ab58a3e1b8f1890ff27c627
7
+ data.tar.gz: e847b5e94d825ade9f232944680f70d2753bdb3e69e1014e9321424ce65d02c1b55604ddef06a92e952fc2432a957f6401eea8c5cf968bb3e01c55d44979b85a
data/README.md CHANGED
@@ -511,19 +511,16 @@ t.each(&:join)
511
511
 
512
512
  If you want to get a summarize stats of your http communication you need to set `NiceHttp.create_stats = true`
513
513
 
514
- Then whenever you want to access the stats: `NiceHttp.stats`
514
+ Then whenever you want to access the stats: `NiceHttp.stats` and if you want to save it on a file: `NiceHttp.save_stats`
515
515
 
516
- After the run is finished there will be few files starting by nice_http_stats and extension yaml on your project root folder including all http stats.
516
+ After the run is finished the stats will automatically be saved even if you didn't call `save_stats`. The stats files will use the name and path on `NiceHttp.log`.
517
517
 
518
518
  If you are using RSpec and you want to generate the stats files after every test is finished, add to your spec_helper.rb file:
519
519
 
520
520
  ```ruby
521
521
  RSpec.configure do |config|
522
522
  config.after(:each) do
523
- require 'yaml'
524
- NiceHttp.stats.keys.each do |key|
525
- File.open("./nice_http_stats_#{key}.yaml", "w") { |file| file.write(NiceHttp.stats[key].to_yaml) }
526
- end
523
+ NiceHttp.save_stats
527
524
  end
528
525
  end
529
526
  ```
@@ -582,16 +579,24 @@ www.reqres.in:443:
582
579
  If you want to add specific stats for your processes you can use the method `NiceHttp.add_stats`
583
580
 
584
581
  ```ruby
582
+ # random customer name
583
+ customer_name = "10-20:L".gen
585
584
  started = Time.now
586
- @http.send_request Requests::Customer.add_customer
585
+ @http.send_request Requests::Customer.add_customer(name: customer_name)
587
586
  30.times do
588
- resp = @http.get(Requests::Customer.get_customer)
587
+ resp = @http.get(Requests::Customer.get_customer(name: customer_name))
589
588
  break if resp.code == 200
590
589
  sleep 0.5
591
590
  end
592
591
  NiceHttp.add_stats(:customer, :create, started, Time.now)
593
592
  ```
594
593
 
594
+ To add the items for every specific stats to be accessed as an array you can add it as the last parameter of `add_stats`
595
+ ```ruby
596
+ NiceHttp.add_stats(:customer, :create, started, Time.now, customer_name)
597
+ ```
598
+ This will generate an items key that will contain an array of the values you added.
599
+
595
600
  ## Contributing
596
601
 
597
602
  Bug reports are very welcome on GitHub at https://github.com/marioruiz/nice_http.
data/lib/nice_http.rb CHANGED
@@ -72,10 +72,7 @@ class NiceHttp
72
72
 
73
73
  at_exit do
74
74
  if self.create_stats
75
- require "yaml"
76
- self.stats.keys.each do |key|
77
- File.open("./nice_http_stats_#{key}.yaml", "w") { |file| file.write(self.stats[key].to_yaml) }
78
- end
75
+ self.save_stats
79
76
  end
80
77
  end
81
78
 
@@ -186,9 +183,44 @@ class NiceHttp
186
183
  time_elapsed[:maximum] = (finished - started) if time_elapsed[:maximum] < (finished - started)
187
184
  time_elapsed[:minimum] = (finished - started) if time_elapsed[:minimum] > (finished - started)
188
185
  time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][state][:num]
189
-
190
186
  end
191
187
 
188
+ ######################################################
189
+ # It will save the NiceHttp.stats on different files, each key of the hash in a different file.
190
+ #
191
+ # @param file_name [String] path and file name to be used to store the stats.
192
+ # In case no one supplied it will be used the value in NiceHttp.log and it will be saved on YAML format.
193
+ # In case extension is .yaml will be saved on YAML format.
194
+ # In case extension is .json will be saved on JSON format.
195
+ #
196
+ # @example
197
+ # NiceHttp.save_stats
198
+ # NiceHttp.save_stats('./stats/my_stats.yaml')
199
+ # NiceHttp.save_stats('./stats/my_stats.json')
200
+ ######################################################
201
+ def self.save_stats(file_name = '')
202
+ if file_name == ''
203
+ if self.log.is_a?(String)
204
+ file_name = self.log
205
+ else
206
+ file_name = './nice_http.log'
207
+ end
208
+ end
209
+ if file_name.match?(/\.json$/)
210
+ require 'json'
211
+ self.stats.keys.each do |key|
212
+ File.open("#{file_name.gsub(/.json$/, "_stats_")}#{key}.json", "w") { |file| file.write(self.stats[key].to_json) }
213
+ end
214
+ else
215
+ puts file_name.gsub(/.\w+$/, "_stats_")
216
+ require 'yaml'
217
+ self.stats.keys.each do |key|
218
+ File.open("#{file_name.gsub(/.\w+$/, "_stats_")}#{key}.yaml", "w") { |file| file.write(self.stats[key].to_yaml) }
219
+ end
220
+ end
221
+ end
222
+
223
+
192
224
  ######################################################
193
225
  # Creates a new http connection.
194
226
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.6
4
+ version: 1.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-02 00:00:00.000000000 Z
11
+ date: 2019-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nice_hash