okay 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +6 -13
- data/lib/okay/http.rb +30 -22
- data/lib/okay/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9995a83167cde5a2a18a0ae55c1db1492f4c6ee8642658879f0ab3ca8ed33ad3
|
4
|
+
data.tar.gz: 796de237563159535c7e8493b4cc1a41badcffd9b21888ff3faa3110f2842de1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9ec2986376209d09718a1eb1583385595f4eb623022c6d05e801dd0c6de811f413dd62337b35a477d2718cdc415eeb092f6bdaba2633a90256865010712800f
|
7
|
+
data.tar.gz: ba34fcc968de9b609509510596e687e25bbdc339e1bcf64fc9c4b9b3c5a119704d123069967f2b1b0b6f99a5ad765c6084495af981ef21c98025b3a845a515e8
|
data/README.md
CHANGED
@@ -8,8 +8,7 @@ Goals:
|
|
8
8
|
|
9
9
|
* Concise, but easy-to-understand code.
|
10
10
|
* Be reasonably robust, but don't chase every potential edgecase. Handle
|
11
|
-
them as they come
|
12
|
-
understand.
|
11
|
+
them as they come.
|
13
12
|
* Well-documented codebase.
|
14
13
|
* Document known limitations, not just features.
|
15
14
|
* Document tests, not just the implementation.
|
@@ -23,17 +22,6 @@ or ask questions if you aren't sure it'll work for your usecase.
|
|
23
22
|
If it doesn't, I may decide I want to add support for it, or be able to
|
24
23
|
help you find something that works for you!
|
25
24
|
|
26
|
-
## Utilities
|
27
|
-
|
28
|
-
Currently implemented utilities:
|
29
|
-
|
30
|
-
* HTTP fetcher.
|
31
|
-
* `GET` requests implemented.
|
32
|
-
* `POST` requests implemented.
|
33
|
-
* Other types of requests should be fairly easy to add, but haven't
|
34
|
-
been added.
|
35
|
-
* TLS is supported, using [`openssl/better_defaults`](https://github.com/duckinator/openssl-better_defaults/) to improve security on old Ruby versions.
|
36
|
-
|
37
25
|
## Installation
|
38
26
|
|
39
27
|
Add this line to your application's Gemfile:
|
@@ -54,6 +42,11 @@ Or install it yourself as:
|
|
54
42
|
|
55
43
|
### HTTP
|
56
44
|
|
45
|
+
* `GET` and `POST` requests supported.
|
46
|
+
* TLS is supported, using [`openssl/better_defaults`](https://github.com/duckinator/openssl-better_defaults/) to improve security on old Ruby versions.
|
47
|
+
* Does not handle HTTP 307 redirects correctly. (Because it changes it to a GET
|
48
|
+
request.)
|
49
|
+
|
57
50
|
```ruby
|
58
51
|
require 'okay/http'
|
59
52
|
|
data/lib/okay/http.rb
CHANGED
@@ -11,16 +11,17 @@ class Okay
|
|
11
11
|
#
|
12
12
|
# @param url [String] The URL to request.
|
13
13
|
# @param parameters [Hash] A hash representing a query string.
|
14
|
-
def self.get(url, parameters: {})
|
15
|
-
send_request(:Get, url, parameters, nil)
|
14
|
+
def self.get(url, parameters: {}, headers: {})
|
15
|
+
send_request(:Get, url, parameters, nil, headers)
|
16
16
|
end
|
17
17
|
|
18
18
|
# Make an HTTP POST request.
|
19
19
|
#
|
20
20
|
# @param url [String] The URL to request.
|
21
|
-
# @param data [String] Raw data to for the body of the
|
22
|
-
# @param form_data [Hash]
|
23
|
-
|
21
|
+
# @param data [String] Raw data to for the body of the request.
|
22
|
+
# @param form_data [Hash] Data for the request body, encoded as though it
|
23
|
+
# were a form.
|
24
|
+
def self.post(url, data: nil, form_data: nil, headers: {})
|
24
25
|
if !data.nil? && !form_data.nil?
|
25
26
|
raise ArgumentError, "cannot specify data and form_data arguments simultaneously."
|
26
27
|
end
|
@@ -31,11 +32,19 @@ class Okay
|
|
31
32
|
body = URI.encode_www_form(form_data)
|
32
33
|
end
|
33
34
|
|
34
|
-
send_request(:Post, url, nil, body)
|
35
|
+
send_request(:Post, url, nil, body, headers)
|
35
36
|
end
|
36
37
|
|
37
|
-
|
38
|
-
|
38
|
+
# Helper method for actually creating a request.
|
39
|
+
#
|
40
|
+
# @param http_method [Symbol] A symbol representing the class name for
|
41
|
+
# +Net::HTTP+. E.g., +:Get+ for GET requests, +:Post+ for POST, etc.
|
42
|
+
# @param url [String] URL for the request.
|
43
|
+
# @param parameters [Hash, nil] Request parameters (for the query string).
|
44
|
+
# @param body [String, nil] Request body.
|
45
|
+
# @param redirect_limit [Numeric] The maximum number of redirects allowed.
|
46
|
+
def self.send_request(http_method, url, parameters, body, headers, redirect_limit = DEFAULT_REDIRECT_LIMIT)
|
47
|
+
if redirect_limit <= 0
|
39
48
|
raise RedirectLimitError, "request exceeded redirect limit"
|
40
49
|
end
|
41
50
|
|
@@ -57,8 +66,13 @@ class Okay
|
|
57
66
|
# E.g., if it's a post request, this returns Net::HTTP::Post.
|
58
67
|
request_class = Net::HTTP.const_get(http_method)
|
59
68
|
|
60
|
-
# Create the request object, but don't send it
|
69
|
+
# Create the request object, but don't send it.
|
61
70
|
request = request_class.new(uri)
|
71
|
+
|
72
|
+
headers.each do |k, v|
|
73
|
+
request[k] = v
|
74
|
+
end
|
75
|
+
|
62
76
|
# Set the request body, if there is one.
|
63
77
|
request.body = body unless body.nil?
|
64
78
|
|
@@ -66,24 +80,18 @@ class Okay
|
|
66
80
|
response = http.request(request)
|
67
81
|
|
68
82
|
# Handle responses.
|
69
|
-
|
70
|
-
when Net::HTTPSuccess
|
71
|
-
# Request succeeded; return the result.
|
72
|
-
response
|
73
|
-
when Net::HTTPRedirection
|
83
|
+
if response.is_a?(Net::HTTPRedirection)
|
74
84
|
# Follow a redirect.
|
75
|
-
# Decrements +
|
76
|
-
#
|
77
|
-
|
78
|
-
# changes to a GET request. https://httpstatuses.com/307
|
79
|
-
send_request(:Get, response['location'], parameters, body, limit - 1)
|
85
|
+
# Decrements +redirect_limit+ while doing so, to avoid redirect loops.
|
86
|
+
# NOTE: Does not handle HTTP 307. https://httpstatuses.com/307
|
87
|
+
send_request(:Get, response['location'], parameters, body, headers, redirect_limit - 1)
|
80
88
|
else
|
81
|
-
|
82
|
-
# isn't successful.
|
83
|
-
response.value
|
89
|
+
response
|
84
90
|
end
|
85
91
|
end
|
86
92
|
end
|
93
|
+
|
94
|
+
# Make +send_request+ a private method.
|
87
95
|
private_class_method :send_request
|
88
96
|
end
|
89
97
|
end
|
data/lib/okay/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: okay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ellen Marie Dash
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: openssl-better_defaults
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
110
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.7.2
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Okay, minimalist implementations of common utilities.
|