nice_http 1.7.17 → 1.7.18

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
  SHA256:
3
- metadata.gz: 48bf7154612b1597e2f1f1b87dfe422cdd00a74cde1af648b7ae8cba93726c49
4
- data.tar.gz: d7f1cf705b45df1275b31e258bfb323f19af7c380395dbe5634ecb5ea5091823
3
+ metadata.gz: 9418c4236f2c9077573f228f2478643a9e7ac6c5918f2c9a7f2d5012b82a3371
4
+ data.tar.gz: 1223acc605b2151b908a028ec75697e756a4e156343b9f99cc2147691084344c
5
5
  SHA512:
6
- metadata.gz: ccd9483801b198cd6b6541a5f3fbbb3342f1a5676d3f28a9a2d42693083201f59519f06a8bd9caedb50d2491b16e35eaf01a216ed4286f3ae4557ca5904fdeda
7
- data.tar.gz: bb4242f88713b47e3b4aa277d4b2644a3121955e4d085449b66ab2b0e76170fa93265f2e0d0853707fef33e496a855f2171f9939ee8ededb470c04a7c71e568e
6
+ metadata.gz: 41a646bc6a3845c9fef92254b60d5a8fb5be20a2128cd30ef4ed5a287259609cb7906a8e2105edf8d9a07ca1928ea24db16d5cb239ee1a4e9b038210f6c851e2
7
+ data.tar.gz: 0b82a07eacfa669c3c25778c73ed9540a404ead59d422962e1b2a51cc837c9b216ef1c5dd17fa09b3aa5dcf06099dddac39bd4934e88428150ff305864f92833
data/.yardopts CHANGED
@@ -2,4 +2,4 @@
2
2
  --title 'nice_http - NiceHttp -- simplest library for accessing and testing HTTP and REST resources.'
3
3
  --charset utf-8
4
4
  --markup markdown
5
- 'lib/*.rb' - '*.md' - 'LICENSE'
5
+ 'lib/*.rb' - 'lib/nice_http/*.rb' - '*.md' - 'LICENSE'
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/nice_http.svg)](https://rubygems.org/gems/nice_http)
4
4
  [![Build Status](https://travis-ci.com/MarioRuiz/nice_http.svg?branch=master)](https://github.com/MarioRuiz/nice_http)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/nice_http/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/nice_http?branch=master)
6
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_shield)
7
6
 
8
7
  NiceHttp the simplest library for accessing and testing HTTP and REST resources.
9
8
 
@@ -21,6 +20,24 @@ To be able to generate random requests take a look at the documentation for nice
21
20
 
22
21
  Example that creates 1000 good random and unique requests to register an user and test that the validation of the fields are correct by the user was able to be registered. Send 800 requests where just one field is wrong and verify the user was not able to be created: https://gist.github.com/MarioRuiz/824d7a462b62fd85f02c1a09455deefb
23
22
 
23
+ # Table of Contents
24
+
25
+ - [Installation](#Installation)
26
+ - [A very simple first example](#A-very-simple-first-example)
27
+ - [Create a connection](#Create-a-connection)
28
+ - [Creating requests](#Creating-requests)
29
+ - [Responses](#Responses)
30
+ - [Special settings](#Special-settings)
31
+ - [Authentication requests](#Authentication-requests)
32
+ - [Http logs](#Http-logs)
33
+ - [Multithreading](#Multithreading)
34
+ - [Http stats](#Http-stats)
35
+ - [Tips](#Tips)
36
+ - [Download a file](#Download-a-file)
37
+ - [Send multipart content](#Send-multipart-content)
38
+ - [Contributing](#Contributing)
39
+ - [License](#License)
40
+
24
41
  ## Installation
25
42
 
26
43
  Install it yourself as:
@@ -343,23 +360,6 @@ NiceHttp.headers[:Authorization] = lambda {get_token()}
343
360
 
344
361
  NiceHttp will call the get_token method you created every time a new Http connection is created.
345
362
 
346
- ## Send multipart content
347
-
348
- Example posting a csv file:
349
-
350
- ```ruby
351
-
352
- require 'net/http/post/multipart'
353
- request = {
354
- path: "/customer/profile/",
355
- headers: {'Content-Type' => 'multipart/form-data'},
356
- data: (Net::HTTP::Post::Multipart.new "/customer/profile/",
357
- "file" => UploadIO.new("./path/to/my/file.csv", "text/csv"))
358
- }
359
- response=@http.post(request)
360
-
361
- ```
362
-
363
363
  ## Http logs
364
364
 
365
365
  You can set where the http logs will be stored by using the log attribute of the NiceHttp.
@@ -599,6 +599,40 @@ NiceHttp.add_stats(:customer, :create, started, Time.now, customer_name)
599
599
  ```
600
600
  This will generate an items key that will contain an array of the values you added.
601
601
 
602
+ ## Tips
603
+
604
+ ### Download a file
605
+
606
+ * Direct download:
607
+
608
+ ```ruby
609
+ resp = NiceHttp.new("https://euruko2019.org").get("/assets/images/logo.png", save_data: './tmp/')
610
+ ```
611
+
612
+ * Get the data and store it like you want:
613
+ ```ruby
614
+ resp = NiceHttp.new("https://euruko2019.org").get("/assets/images/logo.png")
615
+ File.open('./logo.png', 'wb') { |fp| fp.write(resp.data) }
616
+ ```
617
+
618
+ ### Send multipart content
619
+
620
+ Example posting a csv file:
621
+
622
+ ```ruby
623
+
624
+ require 'net/http/post/multipart'
625
+ request = {
626
+ path: "/customer/profile/",
627
+ headers: {'Content-Type' => 'multipart/form-data'},
628
+ data: (Net::HTTP::Post::Multipart.new "/customer/profile/",
629
+ "file" => UploadIO.new("./path/to/my/file.csv", "text/csv"))
630
+ }
631
+ response=@http.post(request)
632
+
633
+ ```
634
+
635
+
602
636
  ## Contributing
603
637
 
604
638
  Bug reports are very welcome on GitHub at https://github.com/marioruiz/nice_http.
@@ -608,8 +642,3 @@ If you want to contribute please follow [GitHub Flow](https://guides.github.com/
608
642
  ## License
609
643
 
610
644
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
611
-
612
-
613
-
614
-
615
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FMarioRuiz%2Fnice_http?ref=badge_large)
@@ -5,6 +5,7 @@ module NiceHttpHttpMethods
5
5
  #
6
6
  # @param arg [Hash] containing at least key :path
7
7
  # @param arg [String] the path
8
+ # @options save_data [String] the path or path and file name where we want to save the response data
8
9
  #
9
10
  # @return [Hash] response
10
11
  # Including at least the symbol keys:
@@ -21,8 +22,12 @@ module NiceHttpHttpMethods
21
22
  # @example
22
23
  # resp = @http.get("/customers/1223")
23
24
  # assert resp.message == "OK"
25
+ # @example
26
+ # resp = @http.get("/assets/images/logo.png", save_data: './tmp/')
27
+ # @example
28
+ # resp = @http.get("/assets/images/logo.png", save_data: './tmp/example.png')
24
29
  ######################################################
25
- def get(arg)
30
+ def get(arg, save_data: '')
26
31
  begin
27
32
  path, data, headers_t = manage_request(arg)
28
33
 
@@ -110,6 +115,24 @@ module NiceHttpHttpMethods
110
115
  else
111
116
  @num_redirects = 0
112
117
  end
118
+ if save_data!=''
119
+ require 'pathname'
120
+ pn_get = Pathname.new(path)
121
+
122
+ if Dir.exist?(save_data)
123
+ save = save_data + "/" + pn_get.basename.to_s
124
+ elsif save_data[-1]=="/"
125
+ save = save_data + pn_get.basename.to_s
126
+ else
127
+ save = save_data
128
+ end
129
+ if Dir.exist?(Pathname.new(save).dirname)
130
+ File.open(save, 'wb') { |fp| fp.write(@response.data) }
131
+ else
132
+ @logger.fatal "The folder #{Pathname.new(save).dirname} doesn't exist"
133
+ end
134
+ #jal9
135
+ end
113
136
  return @response
114
137
  rescue Exception => stack
115
138
  @logger.fatal stack
@@ -107,19 +107,27 @@ module NiceHttpManageResponse
107
107
  if key.kind_of?(Symbol)
108
108
  if key == :code or key == :data or key == :header or key == :message
109
109
  if key == :data and !@response[:'content-type'].to_s.include?("text/html")
110
- begin
111
- JSON.parse(value_orig)
112
- data_s = JSON.pretty_generate(JSON.parse(value_orig))
113
- rescue
114
- data_s = value_orig
115
- end
116
- if @debug
117
- self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
118
- end
119
- if value_orig != value
120
- message += "\n " + key.to_s() + ": '" + value.gsub("<", "&lt;") + "'\n"
110
+ if key == :data and
111
+ (!@response[:'content-type'].include?('text') and
112
+ !@response[:'content-type'].include?('json') and
113
+ !@response[:'content-type'].include?('xml') and
114
+ !@response[:'content-type'].include?('charset=utf-8'))
115
+ message += "\n data: It's not text data so won't be in the logs."
121
116
  else
122
- message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
117
+ begin
118
+ JSON.parse(value_orig)
119
+ data_s = JSON.pretty_generate(JSON.parse(value_orig))
120
+ rescue
121
+ data_s = value_orig
122
+ end
123
+ if @debug
124
+ self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
125
+ end
126
+ if value_orig != value
127
+ message += "\n " + key.to_s() + ": '" + value.gsub("<", "&lt;") + "'\n"
128
+ else
129
+ message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
130
+ end
123
131
  end
124
132
  else
125
133
  if @debug
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.17
4
+ version: 1.7.18
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-08-30 00:00:00.000000000 Z
11
+ date: 2019-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nice_hash