Module: CodeMutiny::HttpClient

Defined in:
lib/httpclient/httpclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#obtained_bodyObject (readonly)

Returns the value of attribute obtained_body.



165
166
167
# File 'lib/httpclient/httpclient.rb', line 165

def obtained_body
  @obtained_body
end

#obtained_codeObject (readonly)

Returns the value of attribute obtained_code.



165
166
167
# File 'lib/httpclient/httpclient.rb', line 165

def obtained_code
  @obtained_code
end

#obtained_headersObject (readonly)

Returns the value of attribute obtained_headers.



165
166
167
# File 'lib/httpclient/httpclient.rb', line 165

def obtained_headers
  @obtained_headers
end

Instance Method Details

#admin(key, &block) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/httpclient/httpclient.rb', line 167

def admin( key, &block )
 if key == nil
   anonymous( &block )
 else
   @auth = { :cmuser => key }
   instance_eval( &block )
 end
end

#anonymous(&block) ⇒ Object



182
183
184
185
# File 'lib/httpclient/httpclient.rb', line 182

def anonymous(  &block )
  @auth = { :cmuser => 'anonymous' }
   instance_eval( &block )
end

#connect(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/httpclient/httpclient.rb', line 65

def connect( &block )
  uri = URI.parse( @@host )
 h = Net::HTTP.new( uri.host, uri.port )			
 if( uri.scheme == 'https' )
   h.use_ssl = uri.scheme
    h.verify_mode = OpenSSL::SSL::VERIFY_NONE
 end
 headers = {}
 headers.merge!( @auth ) if @auth
 headers.desym!
 block.call( h, headers )
end

#create(path, form = nil, &block) ⇒ Object Also known as: update



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/httpclient/httpclient.rb', line 126

def create( path, form = nil, &block )
  connect do |http, headers|
    data = nil
    headers[ 'Content-Length' ] = '0'

    if form
      data, extra_headers = Multipart::Post.prepare_query( form )
      headers.merge!( extra_headers )
      headers[ 'Content-Length' ] = data.length.to_s
    end

    debug_request( :post, path, headers, form )
 
    res = http.post path, data, headers
    handle_response( res, block )
  end
end

#debug(&block) ⇒ Object



78
79
80
81
82
# File 'lib/httpclient/httpclient.rb', line 78

def debug( &block )
  @debug = true
  instance_eval( &block )
  @debug = false
end

#debug_request(http_method, path, headers, form = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/httpclient/httpclient.rb', line 84

def debug_request( http_method, path, headers, form = nil )
  if @debug
    puts ""
    puts "Request"
   puts "==========================="
   puts "method: #{http_method}"
     puts "path: #{path}"
   puts "headers: #{headers}"
   puts "form: #{form}" if form
   puts ""
 end
end

#debug_response(code, headers, body) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/httpclient/httpclient.rb', line 97

def debug_response( code, headers, body )
 if @debug
   puts ""
  puts "Response"
  puts "==========================="
  puts "status code: #{code}"
  puts "headers: #{headers}"
  puts "body: #{body}"
  puts ""
 end
end

#handle_response(res, block = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/httpclient/httpclient.rb', line 147

def handle_response( res, block = nil )
  code = res.code.to_i
  headers = res.to_hash
  body = res.body
  debug_response( code, headers, body )
  case res[ 'Content-Type' ]
     when 'text/x-json' then body = JSON.parse body
     when 'text/xml' then body = REXML::Document.new( body )
   end
  if block
    block.call( code, body, headers ) 
  else
    @obtained_code = code
    @obtained_body = body
    @obtained_headers = headers
  end
end

#host(host) ⇒ Object



61
62
63
# File 'lib/httpclient/httpclient.rb', line 61

def host( host  )
  @@host = host
end

#remove(path, &block) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/httpclient/httpclient.rb', line 117

def remove( path, &block )
  connect do |http, headers|
    debug_request( :delete, path, headers )
     res = http.delete path, headers
   handle_response( res, block )
 end
end

#retrieve(path, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/httpclient/httpclient.rb', line 109

def retrieve( path, &block )
  connect do |http, headers|
   debug_request( :get, path, headers )
   res = http.get path, headers
   handle_response( res, block )
 end
end

#user(username, password = nil, &block) ⇒ Object



176
177
178
179
180
# File 'lib/httpclient/httpclient.rb', line 176

def user( username, password = nil, &block )
   @auth = { :cmuser => username  }
   @auth[ :cmpasswd ] = password if password 
   instance_eval( &block )
end