Class: RAWS::HTTP::Typhoeus::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/raws/http/typhoeus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Request

Returns a new instance of Request.



21
22
23
# File 'lib/raws/http/typhoeus.rb', line 21

def initialize(uri)
  @uri, @header, @method, @before_send = uri, {}, :get , nil
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



18
19
20
# File 'lib/raws/http/typhoeus.rb', line 18

def header
  @header
end

#methodObject

Returns the value of attribute method.



19
20
21
# File 'lib/raws/http/typhoeus.rb', line 19

def method
  @method
end

#uriObject

Returns the value of attribute uri.



19
20
21
# File 'lib/raws/http/typhoeus.rb', line 19

def uri
  @uri
end

Instance Method Details

#before_send(&block) ⇒ Object



25
26
27
# File 'lib/raws/http/typhoeus.rb', line 25

def before_send(&block)
  @before_send = block
end

#send(body = nil, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/raws/http/typhoeus.rb', line 29

def send(body=nil, &block)
  RAWS.logger.debug self
  @before_send && @before_send.call(self)
  response = Response.new(
    ::Typhoeus::Request.__send__(
      @method.to_s.downcase.to_sym,
      @uri,
      :headers => @header,
      :body => if block_given?
        # TODO エラーにした方が。。。 
        io = StringIO.new
        block.call(io)
        if io.size > 0
          io.rewind
          io.read
        end
      else
        body
      end
    )
  )
  case response.code
  when 200...300
    response
  when 300...400
    response.parse
    raise RAWS::HTTP::Redirect.new(response)
  else
    response.parse
    raise RAWS::HTTP::Error.new(response)
  end
end