Class: Himg::CLI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/himg/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Document

Returns a new instance of Document.



98
99
100
101
102
# File 'lib/himg/cli.rb', line 98

def initialize(source, options)
  @source = source
  @options = options
  @http_headers = options[:http_headers]
end

Class Method Details

.http_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/himg/cli.rb', line 104

def self.http_url?(url)
  url.to_s =~ %r{\Ahttps?\://}
end

Instance Method Details

#load(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/himg/cli.rb', line 112

def load(&block)
  if stdin?
    yield($stdin.read)
  elsif self.class.http_url?(@source)
    args = [@source]
    args << @http_headers if @http_headers

    URI.open(*args) do |input|
      yield(input.binmode.read)
    end
  else
    File.open(@source) do |f|
      yield(f.read)
    end
  end
end

#stdin?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/himg/cli.rb', line 108

def stdin?
  @source.nil?
end