simple_request 0.1.1 → 0.1.2
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 +4 -4
- data/.gitignore +1 -0
- data/Changelog.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +6 -0
- data/lib/simple_helper/array/to_csv.rb +15 -0
- data/lib/simple_helper/array/wrap.rb +49 -0
- data/lib/simple_helper/const.rb +12 -0
- data/lib/simple_helper/float/trim.rb +9 -0
- data/lib/simple_helper/hash/keys.rb +168 -0
- data/lib/simple_helper/headers_processor.rb +1 -1
- data/lib/simple_helper/http.rb +4 -3
- data/lib/simple_helper/https.rb +4 -3
- data/lib/simple_helper/response_parser.rb +17 -9
- data/lib/simple_helper/string/string_colorize.rb +97 -0
- data/lib/simple_helper/version.rb +1 -1
- data/lib/simple_request.rb +13 -9
- data/simple_request.gemspec +1 -1
- metadata +7 -3
- data/lib/simple_helper/utils.rb +0 -48
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a380dc73a4972d042555cffee97ee309aeb6d81d4835227e637af3c5f57b5561
|
|
4
|
+
data.tar.gz: b8c806ca0039fd0e40e134cd43756135a2ce01fab77cdc8f48d794a2df279610
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dd9e89b641c451af6e22baf4963222f83cd47299601fd146ab9da0b0ae8be481829e1756ad73f355fc47852ad2b6ee9a87c6ed37357e05161f23e59f7b58f307
|
|
7
|
+
data.tar.gz: 4d7f9ada57b7935e8bec920179b6ce1bb55314e2782bc0828d8063515cf2dd50e37ebda0d133b65be3415451417079974b9cff649af1c97192b256fb399d19c4
|
data/.gitignore
CHANGED
data/Changelog.md
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changeslog
|
|
2
|
+
|
|
3
|
+
## 2020-02-04
|
|
4
|
+
- Changed rspec examples from localhost project to JSONPlaceholder API
|
|
5
|
+
- Added `to_csv` in array class to convert array to csv
|
|
6
|
+
- Added `wrap` in array class to convert hash or nill to an array (Powered by rails core repo)
|
|
7
|
+
- Added `keys` in hash class to symbolize and stringify hash keys
|
|
8
|
+
- Added `string_colorize` in String class to colorize any string
|
|
9
|
+
- Supported csv format you can export the response to csv by `request.csv`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -65,8 +65,14 @@ request.json
|
|
|
65
65
|
request.plain
|
|
66
66
|
# OUTPUT
|
|
67
67
|
# "{\"message\":\"City created successfully\",\"city\":{\"data\":{\"id\":\"10\",\"type\":\"city_details\",\"attributes\":{\"name_en\":\"Mukalla\",\"name_ar\":\"المكلا\",\"suggested_time\":0,\"suggested\":false,\"status\":\"active\"}}}}"
|
|
68
|
+
|
|
69
|
+
request.csv
|
|
70
|
+
# This will export the whole response you can specify the path you want to export by passing the exact path as string split by comma
|
|
71
|
+
# example
|
|
72
|
+
request.csv('city,data,attributes')
|
|
68
73
|
```
|
|
69
74
|
|
|
75
|
+
|
|
70
76
|
### PUT, Patch Request
|
|
71
77
|
```ruby
|
|
72
78
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
|
|
5
|
+
# Usage [{first_name: 'MaJeD', last_name: 'BoJaN'}, {first_name: 'Mohammed', last_name: 'majed'}].to_csv('file_name.csv')
|
|
6
|
+
class Array
|
|
7
|
+
def to_csv(csv_filename = 'file_name.csv')
|
|
8
|
+
CSV.open(csv_filename, 'wb') do |csv|
|
|
9
|
+
csv << first.keys
|
|
10
|
+
each do |hash|
|
|
11
|
+
csv << hash.values # _at(*keys)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Array
|
|
4
|
+
# Powered by rails core
|
|
5
|
+
# Wraps its argument in an array unless it is already an array (or array-like).
|
|
6
|
+
#
|
|
7
|
+
# Specifically:
|
|
8
|
+
#
|
|
9
|
+
# * If the argument is +nil+ an empty array is returned.
|
|
10
|
+
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
|
11
|
+
# * Otherwise, returns an array with the argument as its single element.
|
|
12
|
+
#
|
|
13
|
+
# Array.wrap(nil) # => []
|
|
14
|
+
# Array.wrap([1, 2, 3]) # => [1, 2, 3]
|
|
15
|
+
# Array.wrap(0) # => [0]
|
|
16
|
+
#
|
|
17
|
+
# This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
|
|
18
|
+
#
|
|
19
|
+
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
|
20
|
+
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
|
21
|
+
# an array with the argument as its single element right away.
|
|
22
|
+
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
|
23
|
+
# raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
|
|
24
|
+
# * It does not call +to_a+ on the argument, if the argument does not respond to +to_ary+
|
|
25
|
+
# it returns an array with the argument as its single element.
|
|
26
|
+
#
|
|
27
|
+
# The last point is easily explained with some enumerables:
|
|
28
|
+
#
|
|
29
|
+
# Array(foo: :bar) # => [[:foo, :bar]]
|
|
30
|
+
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
|
|
31
|
+
#
|
|
32
|
+
# There's also a related idiom that uses the splat operator:
|
|
33
|
+
#
|
|
34
|
+
# [*object]
|
|
35
|
+
#
|
|
36
|
+
# which returns <tt>[]</tt> for +nil+, but calls to <tt>Array(object)</tt> otherwise.
|
|
37
|
+
#
|
|
38
|
+
# The differences with <tt>Kernel#Array</tt> explained above
|
|
39
|
+
# apply to the rest of <tt>object</tt>s.
|
|
40
|
+
def self.wrap(object)
|
|
41
|
+
if object.nil?
|
|
42
|
+
[]
|
|
43
|
+
elsif object.respond_to?(:to_ary)
|
|
44
|
+
object.to_ary || [object]
|
|
45
|
+
else
|
|
46
|
+
[object]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/simple_helper/const.rb
CHANGED
|
@@ -19,5 +19,17 @@ module SimpleHelper
|
|
|
19
19
|
def self.supported_format
|
|
20
20
|
ConstantData::SUPPORTED_FORMAT
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
def self.colors
|
|
24
|
+
ConstantData::COLORS
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.bg_colors
|
|
28
|
+
ConstantData::BG_COLORS
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.fonts
|
|
32
|
+
ConstantData::FONTS
|
|
33
|
+
end
|
|
22
34
|
end
|
|
23
35
|
end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Hash
|
|
4
|
+
# Returns a new hash with all keys converted to strings.
|
|
5
|
+
#
|
|
6
|
+
# hash = { name: 'Rob', age: '28' }
|
|
7
|
+
#
|
|
8
|
+
# hash.stringify_keys
|
|
9
|
+
# # => {"name"=>"Rob", "age"=>"28"}
|
|
10
|
+
def stringify_keys
|
|
11
|
+
transform_keys(&:to_s)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Destructively converts all keys to strings. Same as
|
|
15
|
+
# +stringify_keys+, but modifies +self+.
|
|
16
|
+
def stringify_keys!
|
|
17
|
+
transform_keys!(&:to_s)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns a new hash with all keys converted to symbols, as long as
|
|
21
|
+
# they respond to +to_sym+.
|
|
22
|
+
#
|
|
23
|
+
# hash = { 'name' => 'Rob', 'age' => '28' }
|
|
24
|
+
#
|
|
25
|
+
# hash.symbolize_keys
|
|
26
|
+
# # => {:name=>"Rob", :age=>"28"}
|
|
27
|
+
def symbolize_keys
|
|
28
|
+
transform_keys do |key|
|
|
29
|
+
begin
|
|
30
|
+
key.to_sym
|
|
31
|
+
rescue StandardError
|
|
32
|
+
key
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
alias to_options symbolize_keys
|
|
37
|
+
|
|
38
|
+
# Destructively converts all keys to symbols, as long as they respond
|
|
39
|
+
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
|
|
40
|
+
def symbolize_keys!
|
|
41
|
+
transform_keys! do |key|
|
|
42
|
+
begin
|
|
43
|
+
key.to_sym
|
|
44
|
+
rescue StandardError
|
|
45
|
+
key
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
alias to_options! symbolize_keys!
|
|
50
|
+
|
|
51
|
+
# Validates all keys in a hash match <tt>*valid_keys</tt>, raising
|
|
52
|
+
# +ArgumentError+ on a mismatch.
|
|
53
|
+
#
|
|
54
|
+
# Note that keys are treated differently than HashWithIndifferentAccess,
|
|
55
|
+
# meaning that string and symbol keys will not match.
|
|
56
|
+
#
|
|
57
|
+
# { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
|
|
58
|
+
# { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
|
|
59
|
+
# { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
|
|
60
|
+
def assert_valid_keys(*valid_keys)
|
|
61
|
+
valid_keys.flatten!
|
|
62
|
+
each_key do |k|
|
|
63
|
+
unless valid_keys.include?(k)
|
|
64
|
+
raise ArgumentError, "Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Returns a new hash with all keys converted by the block operation.
|
|
70
|
+
# This includes the keys from the root hash and from all
|
|
71
|
+
# nested hashes and arrays.
|
|
72
|
+
#
|
|
73
|
+
# hash = { person: { name: 'Rob', age: '28' } }
|
|
74
|
+
#
|
|
75
|
+
# hash.deep_transform_keys{ |key| key.to_s.upcase }
|
|
76
|
+
# # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
|
|
77
|
+
def deep_transform_keys(&block)
|
|
78
|
+
_deep_transform_keys_in_object(self, &block)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Destructively converts all keys by using the block operation.
|
|
82
|
+
# This includes the keys from the root hash and from all
|
|
83
|
+
# nested hashes and arrays.
|
|
84
|
+
def deep_transform_keys!(&block)
|
|
85
|
+
_deep_transform_keys_in_object!(self, &block)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Returns a new hash with all keys converted to strings.
|
|
89
|
+
# This includes the keys from the root hash and from all
|
|
90
|
+
# nested hashes and arrays.
|
|
91
|
+
#
|
|
92
|
+
# hash = { person: { name: 'Rob', age: '28' } }
|
|
93
|
+
#
|
|
94
|
+
# hash.deep_stringify_keys
|
|
95
|
+
# # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
|
|
96
|
+
def deep_stringify_keys
|
|
97
|
+
deep_transform_keys(&:to_s)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Destructively converts all keys to strings.
|
|
101
|
+
# This includes the keys from the root hash and from all
|
|
102
|
+
# nested hashes and arrays.
|
|
103
|
+
def deep_stringify_keys!
|
|
104
|
+
deep_transform_keys!(&:to_s)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Returns a new hash with all keys converted to symbols, as long as
|
|
108
|
+
# they respond to +to_sym+. This includes the keys from the root hash
|
|
109
|
+
# and from all nested hashes and arrays.
|
|
110
|
+
#
|
|
111
|
+
# hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
|
|
112
|
+
#
|
|
113
|
+
# hash.deep_symbolize_keys
|
|
114
|
+
# # => {:person=>{:name=>"Rob", :age=>"28"}}
|
|
115
|
+
def deep_symbolize_keys
|
|
116
|
+
deep_transform_keys do |key|
|
|
117
|
+
begin
|
|
118
|
+
key.to_sym
|
|
119
|
+
rescue StandardError
|
|
120
|
+
key
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Destructively converts all keys to symbols, as long as they respond
|
|
126
|
+
# to +to_sym+. This includes the keys from the root hash and from all
|
|
127
|
+
# nested hashes and arrays.
|
|
128
|
+
def deep_symbolize_keys!
|
|
129
|
+
deep_transform_keys! do |key|
|
|
130
|
+
begin
|
|
131
|
+
key.to_sym
|
|
132
|
+
rescue StandardError
|
|
133
|
+
key
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
# Support methods for deep transforming nested hashes and arrays.
|
|
141
|
+
def _deep_transform_keys_in_object(object, &block)
|
|
142
|
+
case object
|
|
143
|
+
when Hash
|
|
144
|
+
object.each_with_object({}) do |(key, value), result|
|
|
145
|
+
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
|
|
146
|
+
end
|
|
147
|
+
when Array
|
|
148
|
+
object.map { |e| _deep_transform_keys_in_object(e, &block) }
|
|
149
|
+
else
|
|
150
|
+
object
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def _deep_transform_keys_in_object!(object, &block)
|
|
155
|
+
case object
|
|
156
|
+
when Hash
|
|
157
|
+
object.keys.each do |key|
|
|
158
|
+
value = object.delete(key)
|
|
159
|
+
object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
|
|
160
|
+
end
|
|
161
|
+
object
|
|
162
|
+
when Array
|
|
163
|
+
object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
|
|
164
|
+
else
|
|
165
|
+
object
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -8,7 +8,7 @@ module SimpleHelper
|
|
|
8
8
|
def self.perform(headers)
|
|
9
9
|
return {} if headers.nil? || !headers.respond_to?(:to_hash)
|
|
10
10
|
|
|
11
|
-
@headers =
|
|
11
|
+
@headers = headers.stringify_keys!
|
|
12
12
|
|
|
13
13
|
return @headers unless @headers['Content-Type'].nil?
|
|
14
14
|
|
data/lib/simple_helper/http.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'uri'
|
|
4
4
|
require 'net/http'
|
|
5
5
|
require_relative 'const'
|
|
6
|
+
require_relative 'string/string_colorize'
|
|
6
7
|
|
|
7
8
|
module SimpleHelper
|
|
8
9
|
module Http
|
|
@@ -14,11 +15,11 @@ module SimpleHelper
|
|
|
14
15
|
req.body = params.to_json
|
|
15
16
|
http.request(req)
|
|
16
17
|
rescue Timeout::Error || Net::OpenTimeout
|
|
17
|
-
puts
|
|
18
|
+
puts 'Time out'.bold.red.gray
|
|
18
19
|
rescue Net::HTTPBadResponse || SocketError
|
|
19
|
-
puts
|
|
20
|
+
puts 'Request Failed!'.bold.red.gray
|
|
20
21
|
rescue StandardError
|
|
21
|
-
puts
|
|
22
|
+
puts 'An unknown error occurred!'.bold.red.gray
|
|
22
23
|
end
|
|
23
24
|
end
|
|
24
25
|
end
|
data/lib/simple_helper/https.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'uri'
|
|
4
4
|
require 'net/https'
|
|
5
5
|
require 'pry'
|
|
6
|
+
require_relative 'string/string_colorize'
|
|
6
7
|
module SimpleHelper
|
|
7
8
|
module Https
|
|
8
9
|
class << self
|
|
@@ -16,11 +17,11 @@ module SimpleHelper
|
|
|
16
17
|
req.body = params.to_json
|
|
17
18
|
https.request(req)
|
|
18
19
|
rescue Timeout::Error || Net::OpenTimeout
|
|
19
|
-
puts
|
|
20
|
+
puts 'Time out'.bold.red.gray
|
|
20
21
|
rescue Net::HTTPBadResponse || SocketError
|
|
21
|
-
puts
|
|
22
|
+
puts 'Request Failed!'.bold.red.gray
|
|
22
23
|
rescue StandardError
|
|
23
|
-
puts
|
|
24
|
+
puts 'An unknown error occurred!'.bold.red.gray
|
|
24
25
|
end
|
|
25
26
|
end
|
|
26
27
|
end
|
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'pry'
|
|
4
|
+
require_relative 'array/wrap'
|
|
5
|
+
require_relative 'array/to_csv'
|
|
6
|
+
require_relative 'string/string_colorize'
|
|
4
7
|
|
|
5
8
|
module SimpleHelper
|
|
6
9
|
class ResponseParser
|
|
7
|
-
attr_reader :body, :format
|
|
10
|
+
attr_reader :body, :format, :path
|
|
8
11
|
|
|
9
|
-
def initialize(body, format)
|
|
12
|
+
def initialize(body, format, path)
|
|
10
13
|
@body = body
|
|
11
14
|
@format = format
|
|
15
|
+
@path = path
|
|
12
16
|
end
|
|
13
17
|
|
|
14
|
-
def self.perform(body, format)
|
|
15
|
-
new(body, format).parse
|
|
18
|
+
def self.perform(body, format, path)
|
|
19
|
+
new(body, format, path).parse
|
|
16
20
|
end
|
|
17
21
|
|
|
18
22
|
def parse
|
|
@@ -26,23 +30,27 @@ module SimpleHelper
|
|
|
26
30
|
|
|
27
31
|
protected
|
|
28
32
|
|
|
29
|
-
# TODO: Support other formats like xml
|
|
33
|
+
# TODO: Support other formats like xml
|
|
30
34
|
|
|
31
35
|
UTF8_BOM = "\xEF\xBB\xBF"
|
|
32
36
|
|
|
33
37
|
def json
|
|
34
38
|
JSON.parse(body, quirks_mode: true, allow_nan: true)
|
|
35
39
|
rescue JSON::ParserError
|
|
36
|
-
puts "Response cannot be parsed because it's not a string
|
|
40
|
+
puts "Response cannot be parsed because it's not a string nor valid JSON. please use .plain to get the the plain response".bold.brown.gray
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
def plain
|
|
40
44
|
body
|
|
41
45
|
end
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
def csv
|
|
48
|
+
data = path.nil? ? json : json.dig(*path.split(','))
|
|
49
|
+
raise ArgumentError, 'Cannot export nil or empty hash please pass proper path'.bold.brown.gray if data.nil?
|
|
50
|
+
|
|
51
|
+
Array.wrap(data).to_csv('file_name.csv')
|
|
52
|
+
end
|
|
53
|
+
|
|
46
54
|
# def xml
|
|
47
55
|
# MultiXml.parse(body)
|
|
48
56
|
# end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
##---------------bg Color-----------------##
|
|
5
|
+
|
|
6
|
+
def black
|
|
7
|
+
"\e[30m#{self}\e[0m"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def red
|
|
11
|
+
"\e[31m#{self}\e[0m"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def green
|
|
15
|
+
"\e[32m#{self}\e[0m"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def brown
|
|
19
|
+
"\e[33m#{self}\e[0m"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def blue
|
|
23
|
+
"\e[34m#{self}\e[0m"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def magenta
|
|
27
|
+
"\e[35m#{self}\e[0m"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cyan
|
|
31
|
+
"\e[36m#{self}\e[0m"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def gray
|
|
35
|
+
"\e[37m#{self}\e[0m"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
##---------------bg Color-----------------##
|
|
39
|
+
|
|
40
|
+
def bg_black
|
|
41
|
+
"\e[40m#{self}\e[0m"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def bg_red
|
|
45
|
+
"\e[41m#{self}\e[0m"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def bg_green
|
|
49
|
+
"\e[42m#{self}\e[0m"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def bg_brown
|
|
53
|
+
"\e[43m#{self}\e[0m"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def bg_blue
|
|
57
|
+
"\e[44m#{self}\e[0m"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def bg_magenta
|
|
61
|
+
"\e[45m#{self}\e[0m"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def bg_cyan
|
|
65
|
+
"\e[46m#{self}\e[0m"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def bg_gray
|
|
69
|
+
"\e[47m#{self}\e[0m"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
##---------------Fonts--------------------##
|
|
73
|
+
def bold
|
|
74
|
+
"\e[1m#{self}\e[22m"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def italic
|
|
78
|
+
"\e[3m#{self}\e[23m"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def underline
|
|
82
|
+
"\e[4m#{self}\e[24m"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def blink
|
|
86
|
+
"\e[5m#{self}\e[25m"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def reverse_color
|
|
90
|
+
"\e[7m#{self}\e[27m"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Usage
|
|
95
|
+
# puts 'I'm back green'.bg_green
|
|
96
|
+
# puts 'I'm red and back cyan'.red.bg_cyan
|
|
97
|
+
# puts 'I'm bold and green and backround red'.bold.green.bg_red
|
data/lib/simple_request.rb
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'uri'
|
|
3
|
+
# require 'uri'
|
|
4
4
|
require 'pry'
|
|
5
|
-
require 'json'
|
|
6
|
-
|
|
7
|
-
#
|
|
5
|
+
# require 'json'
|
|
6
|
+
|
|
7
|
+
# require_relative 'simple_helper/array/wrap'
|
|
8
|
+
# require_relative 'simple_helper/string/string_colorize'
|
|
9
|
+
# require_relative 'simple_helper/array/to_csv'
|
|
10
|
+
require_relative 'simple_helper/hash/keys'
|
|
8
11
|
|
|
9
12
|
require_relative 'simple_helper/const'
|
|
10
13
|
require_relative 'simple_helper/http'
|
|
11
14
|
require_relative 'simple_helper/https'
|
|
12
|
-
require_relative 'simple_helper/utils'
|
|
13
15
|
require_relative 'simple_helper/version'
|
|
14
16
|
require_relative 'simple_helper/exceptions'
|
|
15
17
|
require_relative 'simple_helper/response_parser'
|
|
@@ -19,7 +21,7 @@ class SimpleRequest
|
|
|
19
21
|
attr_reader :options, :response
|
|
20
22
|
|
|
21
23
|
def initialize(**options)
|
|
22
|
-
@options =
|
|
24
|
+
@options = options.symbolize_keys!
|
|
23
25
|
validate
|
|
24
26
|
end
|
|
25
27
|
|
|
@@ -49,10 +51,14 @@ class SimpleRequest
|
|
|
49
51
|
|
|
50
52
|
SimpleHelper::Const.supported_format.each do |key|
|
|
51
53
|
define_method key do
|
|
52
|
-
SimpleHelper::ResponseParser.perform(body_response, key)
|
|
54
|
+
SimpleHelper::ResponseParser.perform(body_response, key, nil)
|
|
53
55
|
end
|
|
54
56
|
end
|
|
55
57
|
|
|
58
|
+
def csv(path = nil)
|
|
59
|
+
SimpleHelper::ResponseParser.perform(body_response, 'csv', path)
|
|
60
|
+
end
|
|
61
|
+
|
|
56
62
|
private
|
|
57
63
|
|
|
58
64
|
def requested_class
|
|
@@ -83,8 +89,6 @@ class SimpleRequest
|
|
|
83
89
|
URI(options[:url])
|
|
84
90
|
end
|
|
85
91
|
|
|
86
|
-
def response_processor; end
|
|
87
|
-
|
|
88
92
|
def validate
|
|
89
93
|
# raise SimpleHelper::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
|
|
90
94
|
|
data/simple_request.gemspec
CHANGED
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
|
8
8
|
spec.authors = ["MaJeD BoJaN"]
|
|
9
9
|
spec.email = ["bojanmajed@gmail.com"]
|
|
10
10
|
|
|
11
|
-
spec.summary =
|
|
11
|
+
spec.summary = 'Simple ruby gem that helps to make HTTP and HTTPS request from ruby projects.'
|
|
12
12
|
spec.homepage = "https://github.com/MajedBojan/simple_request"
|
|
13
13
|
spec.license = "MIT"
|
|
14
14
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_request
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- MaJeD BoJaN
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-
|
|
11
|
+
date: 2020-02-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -73,14 +73,18 @@ files:
|
|
|
73
73
|
- Rakefile
|
|
74
74
|
- bin/console
|
|
75
75
|
- bin/setup
|
|
76
|
+
- lib/simple_helper/array/to_csv.rb
|
|
77
|
+
- lib/simple_helper/array/wrap.rb
|
|
76
78
|
- lib/simple_helper/const.rb
|
|
77
79
|
- lib/simple_helper/constant_data.rb
|
|
78
80
|
- lib/simple_helper/exceptions.rb
|
|
81
|
+
- lib/simple_helper/float/trim.rb
|
|
82
|
+
- lib/simple_helper/hash/keys.rb
|
|
79
83
|
- lib/simple_helper/headers_processor.rb
|
|
80
84
|
- lib/simple_helper/http.rb
|
|
81
85
|
- lib/simple_helper/https.rb
|
|
82
86
|
- lib/simple_helper/response_parser.rb
|
|
83
|
-
- lib/simple_helper/
|
|
87
|
+
- lib/simple_helper/string/string_colorize.rb
|
|
84
88
|
- lib/simple_helper/version.rb
|
|
85
89
|
- lib/simple_request.rb
|
|
86
90
|
- simple_request-0.1.0.gem
|
data/lib/simple_helper/utils.rb
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module SimpleHelper
|
|
4
|
-
module Utils
|
|
5
|
-
def self.stringify_keys(hash)
|
|
6
|
-
return hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys)
|
|
7
|
-
|
|
8
|
-
hash.each_with_object({}) do |(key, value), new_hash|
|
|
9
|
-
new_hash[key.to_s] = value
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def self.symbolize_keys(obj)
|
|
14
|
-
case obj
|
|
15
|
-
when Array
|
|
16
|
-
obj.inject([]){|res, val|
|
|
17
|
-
res << case val
|
|
18
|
-
when Hash, Array
|
|
19
|
-
symbolize_keys(val)
|
|
20
|
-
else
|
|
21
|
-
val
|
|
22
|
-
end
|
|
23
|
-
res
|
|
24
|
-
}
|
|
25
|
-
when Hash
|
|
26
|
-
obj.inject({}){|res, (key, val)|
|
|
27
|
-
nkey = case key
|
|
28
|
-
when String
|
|
29
|
-
key.to_sym
|
|
30
|
-
else
|
|
31
|
-
key
|
|
32
|
-
end
|
|
33
|
-
nval = case val
|
|
34
|
-
when Hash, Array
|
|
35
|
-
symbolize_keys(val)
|
|
36
|
-
else
|
|
37
|
-
val
|
|
38
|
-
end
|
|
39
|
-
res[nkey] = nval
|
|
40
|
-
res
|
|
41
|
-
}
|
|
42
|
-
else
|
|
43
|
-
obj
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
end
|