-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The response became an instance of Fetch::Response
- Loading branch information
Showing
7 changed files
with
253 additions
and
30 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
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,50 @@ | ||
module Fetch | ||
class Headers | ||
include Enumerable | ||
|
||
def initialize(init = []) | ||
@entries = [] | ||
|
||
init.each do |k, v| | ||
append k, v | ||
end | ||
end | ||
|
||
attr_reader :entries | ||
|
||
def append(key, value) | ||
@entries << [key.to_s.downcase, value] | ||
end | ||
|
||
def delete(key) | ||
@entries.delete_if {|k,| k == key.to_s.downcase } | ||
end | ||
|
||
def get(key) | ||
@entries.select {|k,| k == key.to_s.downcase }.map(&:last).join(', ') | ||
end | ||
|
||
def has(key) | ||
@entries.any? {|k,| k == key.to_s.downcase } | ||
end | ||
|
||
def keys | ||
@entries.map(&:first) | ||
end | ||
|
||
def set(key, value) | ||
delete key | ||
append key, value | ||
end | ||
|
||
def values | ||
@entries.map(&:last) | ||
end | ||
|
||
def each(&block) | ||
return enum_for(:each) unless block_given? | ||
|
||
@entries.each(&block) | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'json' | ||
require 'rack/utils' | ||
|
||
module Fetch | ||
Response = Data.define(:url, :status, :headers, :body, :redirected) { | ||
def ok | ||
status.between?(200, 299) | ||
end | ||
|
||
def status_text | ||
Rack::Utils::HTTP_STATUS_CODES[status] | ||
end | ||
|
||
def json(**opts) | ||
JSON.parse(body, **opts) | ||
end | ||
} | ||
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
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,96 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Fetch::Headers do | ||
example 'append' do | ||
headers = Fetch::Headers.new | ||
|
||
headers.append :foo, 'bar' | ||
headers.append :foo, 'baz' | ||
|
||
expect(headers.entries).to eq([ | ||
['foo', 'bar'], | ||
['foo', 'baz'] | ||
]) | ||
end | ||
|
||
example 'delete' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
headers.delete :baz | ||
|
||
expect(headers.entries).to eq([ | ||
['foo', 'bar'] | ||
]) | ||
end | ||
|
||
example 'get' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
expect(headers.get(:foo)).to eq('bar') | ||
expect(headers.get(:baz)).to eq('qux, quux') | ||
end | ||
|
||
example 'has' do | ||
headers = Fetch::Headers.new(foo: 'bar') | ||
|
||
expect(headers.has(:foo)).to eq(true) | ||
expect(headers.has(:bar)).to eq(false) | ||
end | ||
|
||
example 'keys' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
expect(headers.keys).to eq(%w[foo baz baz]) | ||
end | ||
|
||
example 'set' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
headers.set :baz, 'foobar' | ||
|
||
expect(headers.entries).to eq([ | ||
['foo', 'bar'], | ||
['baz', 'foobar'] | ||
]) | ||
end | ||
|
||
example 'values' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
expect(headers.values).to eq(%w[bar qux quux]) | ||
end | ||
|
||
example 'each' do | ||
headers = Fetch::Headers.new([ | ||
[:foo, 'bar'], | ||
[:baz, 'qux'], | ||
[:baz, 'quux'] | ||
]) | ||
|
||
expect(headers.to_a).to eq([ | ||
['foo', 'bar'], | ||
['baz', 'qux'], | ||
['baz', 'quux'] | ||
]) | ||
end | ||
end |
Oops, something went wrong.