-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to make this work the latest versions of Crystal.
- Loading branch information
Showing
7 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,11 @@ version: 0.1.0 | |
authors: | ||
- Russ Smith <[email protected]> | ||
|
||
crystal: 0.24.2 | ||
crystal: ">=0.36.1, < 2.0.0" | ||
|
||
license: MIT | ||
|
||
dependencies: | ||
habitat: | ||
github: luckyframework/habitat | ||
branch: master | ||
version: ~> 0.4.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "./spec_helper" | ||
|
||
describe EightTrack do | ||
it "..." do | ||
EightTrack.use_tape("weather") do | ||
response = HTTP::Client.get(URI.parse("https://ipapi.co/8.8.8.8/json"), headers: headers) | ||
|
||
json = JSON.parse(response.body) | ||
json["ip"].should eq "8.8.8.8" | ||
end | ||
end | ||
end | ||
|
||
private def headers | ||
HTTP::Headers{ | ||
"Content-Type" => "application/json;", | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require "spec" | ||
require "json" | ||
require "../src/eighttrack" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,53 @@ | ||
require "http/client" | ||
require "digest" | ||
require "file_utils" | ||
|
||
class HTTP::Request | ||
def to_json | ||
def data_for_hash_digest | ||
{ | ||
method: method, | ||
host: host, | ||
hostname: hostname, | ||
resource: resource, | ||
headers: headers.to_h, | ||
body: body.to_s, | ||
query_params: query_params.to_h, | ||
}.to_json | ||
}.to_s | ||
end | ||
end | ||
|
||
class HTTP::Client | ||
private def exec_internal(request : HTTP::Request) | ||
private def exec_internal_single(request) | ||
tape_name = EightTrack.tape_name | ||
|
||
# If we do not have a tape_name, perform a normal request | ||
if (tape_name.nil?) | ||
request.to_io(socket) | ||
socket.flush | ||
return HTTP::Client::Response.from_io(socket, request.ignore_body?) | ||
end | ||
|
||
# Create an md5 for the request | ||
req_md5 = Digest::MD5.hexdigest(request.to_json) | ||
|
||
# Create path vars | ||
tape_dir = File.join(EightTrack.settings.tape_library_dir, tape_name) | ||
# Create a dir for our tape | ||
FileUtils.mkdir(tape_dir) unless (Dir.exists?(tape_dir)) | ||
tape_path = File.join(tape_dir, "#{EightTrack.sequence}.#{req_md5}.vcr") | ||
|
||
# If it exists, load and return the data | ||
if File.exists?(tape_path) | ||
HTTP::Client::Response.from_io(File.open(tape_path)) | ||
# If we do not have a tape_name, perform a normal request | ||
decompress = send_request(request) | ||
HTTP::Client::Response.from_io?(io, ignore_body: request.ignore_body?, decompress: decompress) | ||
else | ||
request.to_io(socket) | ||
socket.flush | ||
response = HTTP::Client::Response.from_io(socket, request.ignore_body?) | ||
|
||
tape_file = File.open(tape_path, "w+") | ||
response.to_io(tape_file) | ||
tape_file.flush | ||
|
||
response | ||
# Create an md5 for the request | ||
req_md5 = Digest::MD5.hexdigest(request.data_for_hash_digest) | ||
|
||
# Create path vars | ||
tape_dir = File.join(EightTrack.settings.tape_library_dir, tape_name) | ||
|
||
# Create a dir for our tape | ||
FileUtils.mkdir_p(tape_dir) unless (Dir.exists?(tape_dir)) | ||
tape_path = File.join(tape_dir, "#{EightTrack.sequence}.#{req_md5}.vcr") | ||
|
||
if File.exists?(tape_path) | ||
# If it exists, load and return the data | ||
HTTP::Client::Response.from_io(File.open(tape_path)) | ||
else | ||
# Doesn't exist, fetch and save the data | ||
tape_file = File.open(tape_path, "w+") | ||
decompress = send_request(request) | ||
|
||
if response = HTTP::Client::Response.from_io?(io, ignore_body: request.ignore_body?, decompress: decompress) | ||
response.to_io(tape_file) | ||
tape_file.flush | ||
response | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.