Class: Higo::Transport::Web
- Inherits:
-
Object
- Object
- Higo::Transport::Web
- Defined in:
- lib/higo/transport/web.rb
Instance Attribute Summary collapse
-
#_raw_data ⇒ Object
Returns the value of attribute _raw_data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #finish(data = nil) ⇒ Object
-
#initialize(path: nil) ⇒ Web
constructor
A new instance of Web.
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_data ⇒ Object
Returns the value of attribute _raw_data.
6 7 8 |
# File 'lib/higo/transport/web.rb', line 6 def _raw_data @_raw_data end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/higo/transport/web.rb', line 5 def path @path end |
#response ⇒ Object
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
#call ⇒ Object
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 |