Class: Rig::HTTP
- Inherits:
-
Object
show all
- Defined in:
- lib/rig/http.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*options) ⇒ HTTP
Returns a new instance of HTTP.
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/rig/http.rb', line 16
def initialize *options
@options = normalize_options( options )
@body = HTTPBody.new( @options )
@options.merge!(
:content_type => @body.content_type,
:content_length => @body.content_length
)
@header = HTTPHeader.new( @options )
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/rig/http.rb', line 89
def method_missing name, *args, &block
if options[name]
return options[name]
else
super
end
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
14
15
16
|
# File 'lib/rig/http.rb', line 14
def body
@body
end
|
Returns the value of attribute header.
14
15
16
|
# File 'lib/rig/http.rb', line 14
def
@header
end
|
#options ⇒ Object
Returns the value of attribute options.
14
15
16
|
# File 'lib/rig/http.rb', line 14
def options
@options
end
|
Class Method Details
.method_missing(name, *args, &block) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rig/http.rb', line 109
def self.method_missing name, *args, &block
name = name.to_s.upcase
if HTTPMethods.include?( name )
case options_mode( args )
when :simple
self.new( args.push( :http_method => name ) )
when :mixed
args.last.merge!( :http_method => name )
self.new( args )
when :advanced
self.new( args.first.merge( :http_method => name ) )
end
else
super
end
end
|
.options_mode(options) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/rig/http.rb', line 97
def self.options_mode options
if options.length == 1 && options.first.is_a?( String )
:simple
elsif options.length == 2 && options.map(&:class) == [String, Hash]
:mixed
elsif options.length == 1 && options.first.is_a?( Hash )
:advanced
else
:error
end
end
|
Instance Method Details
#http_method ⇒ Object
46
47
48
|
# File 'lib/rig/http.rb', line 46
def http_method
@options[:http_method] || "GET"
end
|
#normalize_options(options) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/rig/http.rb', line 50
def normalize_options options
options.flatten!
options = case HTTP.options_mode( options )
when :simple
uri = URI.parse( options.first )
{
:host => uri.host,
:port => uri.port,
:path => uri.path,
:query => uri.query
}
when :mixed
uri = URI.parse( options.first )
uri_options = {
:host => uri.host,
:port => uri.port,
:path => uri.path,
:query => uri.query
}
uri_options.merge( options.last )
when :advanced
options.first[:http_method] ||= "GET"
options.first[:host]
options.first[:port] ||= 80
options.first[:path] ||= "/"
options.first[:query]
options.first
else
raise ArgumentError
end
if options[:path].nil? || options[:path].empty?
options[:path] = "/"
end
options
end
|
#send ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rig/http.rb', line 28
def send
begin
tcp_socket = TCPSocket.new( @options[:host], @options[:port] )
tcp_socket.write( @header.to_s + @body.to_s )
response = tcp_socket.read
rescue => exception
puts exception.message
ensure
tcp_socket.close
end
HTTPResponse.new( response ) || exception.message
end
|
#with_body? ⇒ Boolean
42
43
44
|
# File 'lib/rig/http.rb', line 42
def with_body?
%w(POST PUT).include?( options[:http_method] ) && options[:body]
end
|