Skip to content

Commit 58ecd7a

Browse files
author
Daniel Grieve
committed
Add support for providing http headers to get
1 parent e4907fd commit 58ecd7a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/thor/actions/file_manipulation.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ def link_file(source, *args)
6666
# ==== Parameters
6767
# source<String>:: the address of the given content.
6868
# destination<String>:: the relative path to the destination root.
69-
# config<Hash>:: give :verbose => false to not log the status.
69+
# config<Hash>:: give :verbose => false to not log the status, and
70+
# :http_headers => <Hash> to add headers to an http request.
7071
#
7172
# ==== Examples
7273
#
7374
# get "http://gist.github.com/103208", "doc/README"
7475
#
76+
# get "http://gist.github.com/103208", "doc/README", :http_headers => {"Content-Type" => "application/json"}
77+
#
7578
# get "http://gist.github.com/103208" do |content|
7679
# content.split("\n").first
7780
# end
@@ -82,7 +85,7 @@ def get(source, *args, &block)
8285

8386
render = if source =~ %r{^https?\://}
8487
require "open-uri"
85-
URI.send(:open, source) { |input| input.binmode.read }
88+
URI.send(:open, source, config.fetch(:http_headers, {})) { |input| input.binmode.read }
8689
else
8790
source = File.expand_path(find_in_source_paths(source.to_s))
8891
open(source) { |input| input.binmode.read }

spec/actions/file_manipulation_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ def file
159159
expect(content).to eq(body)
160160
end
161161
end
162+
163+
it "accepts http headers" do
164+
body = "__start__\nHTTPFILE\n__end__\n"
165+
headers = {"Content-Type" => "application/json"}
166+
stub_request(:get, "https://example.com/file.txt").with(:headers => headers).to_return(:body => body.dup)
167+
action :get, "https://example.com/file.txt", {:http_headers => headers} do |content|
168+
expect(a_request(:get, "https://example.com/file.txt")).to have_been_made
169+
expect(content).to eq(body)
170+
end
171+
end
162172
end
163173

164174
describe "#template" do

0 commit comments

Comments
 (0)