okay 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/lib/okay/http.rb +53 -20
- data/lib/okay/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 821cdf5fa5035a799e732e2988fee9ec3666155a
|
4
|
+
data.tar.gz: dd1f9595882d529f39e18e6bb7205637b555c05d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df138d22cd54965b5c7e788a99a5c09d4b8952dc13026e0f6d3190529e503f50e34798e4c37f21b2976384dc6977b31bdb519d6d987ac2d910180cda102c0bfb
|
7
|
+
data.tar.gz: f5bde4d3e7b9863580717bdd3c5c892c88e518eae484a5892ae3fd61e778d06bd7fcb6d4bd50655a630e93fd97f83ce851aedfef672eab6bb18d0e9e2917d8ca
|
data/README.md
CHANGED
@@ -59,7 +59,17 @@ require 'okay/http'
|
|
59
59
|
|
60
60
|
Okay::HTTP.get("https://smallest.dog") #=> #<Net::HTTPOK 200 OK readbody=true>
|
61
61
|
Okay::HTTP.get("https://smallest.dog").body # => returns the page contents.
|
62
|
-
|
62
|
+
|
63
|
+
# Generates a query string based on +parameters+, ultimately requesting
|
64
|
+
# https://httpbin.org/get?foo=bar
|
65
|
+
Okay::HTTP.get("https://httpbin.org/get", parameters: { "foo" => "bar" })
|
66
|
+
|
67
|
+
# Encodes +form_data+ as though it were a form, and sets the result of
|
68
|
+
# that as the request body.
|
69
|
+
Okay::HTTP.post("https://httpbin.org/post", form_data: { "foo" => "bar" })
|
70
|
+
|
71
|
+
# Uses +data+ as the request body.
|
72
|
+
Okay::HTTP.post("https://httpbin.org/post", data: "hello, world!")
|
63
73
|
```
|
64
74
|
|
65
75
|
## Development
|
data/lib/okay/http.rb
CHANGED
@@ -3,48 +3,80 @@ require 'net/https'
|
|
3
3
|
|
4
4
|
class Okay
|
5
5
|
module HTTP
|
6
|
-
MethodError = Class.new(ArgumentError)
|
7
6
|
RedirectLimitError = Class.new(StandardError)
|
8
7
|
|
9
8
|
DEFAULT_REDIRECT_LIMIT = 10
|
10
9
|
|
11
|
-
#
|
12
|
-
# (Yay overlapping terminology.)
|
10
|
+
# Make an HTTP GET request.
|
13
11
|
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}
|
19
|
-
|
20
|
-
def self.get(url, parameters = {})
|
21
|
-
send_request(METHODS[:get], url, parameters)
|
12
|
+
# @param url [String] The URL to request.
|
13
|
+
# @param parameters [Hash] A hash representing a query string.
|
14
|
+
def self.get(url, parameters: {})
|
15
|
+
send_request(:Get, url, parameters, nil)
|
22
16
|
end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
# Make an HTTP POST request.
|
19
|
+
#
|
20
|
+
# @param url [String] The URL to request.
|
21
|
+
# @param data [String] Raw data to for the body of the POST request.
|
22
|
+
# @param form_data [Hash] Form data, treated as though.
|
23
|
+
def self.post(url, data: nil, form_data: nil)
|
24
|
+
if !data.nil? && !form_data.nil?
|
25
|
+
raise ArgumentError, "cannot specify data and form_data arguments simultaneously."
|
26
|
+
end
|
27
27
|
|
28
|
-
|
28
|
+
if form_data.nil?
|
29
|
+
body = data
|
30
|
+
else
|
31
|
+
body = URI.encode_www_form(form_data)
|
32
|
+
end
|
29
33
|
|
30
|
-
|
34
|
+
send_request(:Post, url, nil, body)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.send_request(http_method, url, parameters, body, limit = DEFAULT_REDIRECT_LIMIT)
|
31
38
|
if limit <= 0
|
32
39
|
raise RedirectLimitError, "request exceeded redirect limit"
|
33
40
|
end
|
34
41
|
|
42
|
+
# Convert the URL (a String) into a URI object.
|
35
43
|
uri = URI.parse(url)
|
36
|
-
uri.query = URI.encode_www_form(parameters)
|
37
44
|
|
38
|
-
|
45
|
+
# Set the query string for the request.
|
46
|
+
uri.query = URI.encode_www_form(parameters) unless parameters.nil?
|
47
|
+
|
48
|
+
options = {
|
49
|
+
# If the URI starts with "https://", enable SSL/TLS.
|
50
|
+
use_ssl: (uri.scheme == 'https')
|
51
|
+
}
|
39
52
|
|
53
|
+
# Net::HTTP.start() keeps a connection to the host alive
|
54
|
+
# for all requests that occur inside the block.
|
40
55
|
Net::HTTP.start(uri.host, uri.port, options) do |http|
|
41
|
-
|
56
|
+
# Get a reference to the class for the specified request type.
|
57
|
+
# E.g., if it's a post request, this returns Net::HTTP::Post.
|
58
|
+
request_class = Net::HTTP.const_get(http_method)
|
59
|
+
|
60
|
+
# Create the request object, but don't send it yet.
|
61
|
+
request = request_class.new(uri)
|
62
|
+
# Set the request body, if there is one.
|
63
|
+
request.body = body unless body.nil?
|
64
|
+
|
65
|
+
# Send the request, storing the result in +response+.
|
66
|
+
response = http.request(request)
|
42
67
|
|
68
|
+
# Handle responses.
|
43
69
|
case response
|
44
70
|
when Net::HTTPSuccess
|
71
|
+
# Request succeeded; return the result.
|
45
72
|
response
|
46
73
|
when Net::HTTPRedirection
|
47
|
-
|
74
|
+
# Follow a redirect.
|
75
|
+
# Decrements +limit+ while doing so, to avoid redirect loops.
|
76
|
+
#
|
77
|
+
# NOTE: This does not handle HTTP 307 correctly, as it always
|
78
|
+
# changes to a GET request. https://httpstatuses.com/307
|
79
|
+
send_request(:Get, response['location'], parameters, body, limit - 1)
|
48
80
|
else
|
49
81
|
# This seemingly-innocent method raises an exception if the request
|
50
82
|
# isn't successful.
|
@@ -52,5 +84,6 @@ class Okay
|
|
52
84
|
end
|
53
85
|
end
|
54
86
|
end
|
87
|
+
private_class_method :send_request
|
55
88
|
end
|
56
89
|
end
|
data/lib/okay/version.rb
CHANGED