Class: Higo::Transport::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/higo/transport/web.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Web

Returns a new instance of Web.



8
9
10
11
12
# File 'lib/higo/transport/web.rb', line 8

def initialize(path: nil)
  @path      = URI(path)
  @_raw_data = nil
  @response  = nil
end

Instance Attribute Details

#_raw_dataObject

Returns the value of attribute _raw_data.



6
7
8
# File 'lib/higo/transport/web.rb', line 6

def _raw_data
  @_raw_data
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/higo/transport/web.rb', line 5

def path
  @path
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/higo/transport/web.rb', line 6

def response
  @response
end

Class Method Details

.transport(url) ⇒ Object



14
15
16
17
18
# File 'lib/higo/transport/web.rb', line 14

def self.transport(url)
  request  = new(url)
  request._raw_data = request.call
  request
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
# File 'lib/higo/transport/web.rb', line 20

def call
  Fiber.new do
    http         = Net::HTTP.new(path.host, path.port)
    http.use_ssl = true if path.port == 443
    http.request(Net::HTTP::Get.new(path.to_s))
  end
end

#finish(data = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/higo/transport/web.rb', line 28

def finish(data=nil)
  result = data.respond_to?(:resume) ? data.resume : self._raw_data.resume
  raise 'Provide an object that responds to resume' if data.nil?
  temp_file = Tempfile.new([self.path.host.tr('.',''), ::Pathname.new(self.path.to_s).extname])

  if success?(result.code) && result.body.bytesize > 0
    ::File.open(temp_file.path, 'w') { |f| f.write(result.body) }
    response = Transport::File.transport(path: temp_file.path).finish
    temp_file.unlink
  else
    raise 'No Data at endpoint'
  end

  response
rescue => e
  raise e if e.class == SocketError
end