Method: Thor::Actions#get

Defined in:
lib/thor/actions/file_manipulation.rb

#get(source, *args, &block) ⇒ Object

Gets the content at the given address and places it at the given relative destination. If a block is given instead of destination, the content of the url is yielded and used as location.

get relies on open-uri, so passing application user input would provide a command injection attack vector.

Parameters

source<String>

the address of the given content.

destination<String>

the relative path to the destination root.

config<Hash>

give :verbose => false to not log the status, and :http_headers => <Hash> to add headers to an http request.

Examples

get "http://gist.github.com/103208", "doc/README"

get "http://gist.github.com/103208", "doc/README", :http_headers => {"Content-Type" => "application/json"}

get "http://gist.github.com/103208" do |content|
  content.split("\n").first
end
[View source]

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/thor/actions/file_manipulation.rb', line 81

def get(source, *args, &block)
  config = args.last.is_a?(Hash) ? args.pop : {}
  destination = args.first

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

  destination ||= if block_given?
    block.arity == 1 ? yield(render) : yield
  else
    File.basename(source)
  end

  create_file destination, render, config
end