Skip to content

Commit

Permalink
Updates to make this work the latest versions of Crystal.
Browse files Browse the repository at this point in the history
  • Loading branch information
russ committed May 4, 2021
1 parent 8bb7e83 commit 853f091
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/lib/
/bin/
/.shards/
/spec/fixtures/eighttracks/**/*

# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/eighttrack_spec.cr
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
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require "spec"
require "json"
require "../src/eighttrack"
19 changes: 14 additions & 5 deletions src/eighttrack.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module EightTrack
setting tape_library_dir : String = "spec/fixtures/eighttracks"
end

def self.record
self.record = true
yield
self.record = false
end

# The name of the tape
def self.tape_name
@@tape_name
Expand All @@ -23,7 +29,7 @@ module EightTrack
@@tape_name = tape_name
@@sequence = 0

reset_casset(tape_name) if record?
reset_casset(@@tape_name.not_nil!) if record?

block.call
reset!
Expand All @@ -43,10 +49,13 @@ module EightTrack
end

private def self.reset_casset(casset)
dir = File.join(EightTrack.settings.tape_library_dir, casset)
Dir.open(dir).each do |file|
if file =~ /\.vcr$/
File.delete(File.join(dir, file))
casset_dir_path = File.join(EightTrack.settings.tape_library_dir, casset)

if Dir.exists?(casset_dir_path)
Dir.open(casset_dir_path).each do |file|
if file =~ /\.vcr$/
File.delete(File.join(casset_dir_path, file))
end
end
end
end
Expand Down
63 changes: 32 additions & 31 deletions src/eighttrack/core_ext.cr
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
5 changes: 0 additions & 5 deletions src/eighttrack/macros.cr

This file was deleted.

0 comments on commit 853f091

Please sign in to comment.