Class: BubbleWrap::HTTP::Query

Inherits:
Object
  • Object
show all
Defined in:
motion/http.rb

Overview

Class wrapping NSConnection and often used indirectly by the BubbleWrap::HTTP module methods.

Constant Summary collapse

CLRF =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_string, http_method = :get, options = {}) ⇒ Query

Parameters

url<String>

url of the resource to download

http_method<Symbol>

Value representing the HTTP method to use

options<Hash>

optional options used for the query

Options

:payload<String> - data to pass to a POST, PUT, DELETE query. :delegator - Proc, class or object to call when the file is downloaded. a proc will receive a Response object while the passed object will receive the handle_query_response method :headers<Hash> - headers send with the request Anything else will be available via the options attribute reader.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'motion/http.rb', line 111

def initialize(url_string, http_method = :get, options={})
  @method = http_method.upcase.to_s
  @delegator = options.delete(:action) || self
  @payload = options.delete(:payload)
  @files = options.delete(:files)
  @boundary = options.delete(:boundary) || BW.create_uuid
  @credentials = options.delete(:credentials) || {}
  @credentials = {:username => '', :password => ''}.merge(@credentials)
  @timeout = options.delete(:timeout) || 30.0
  @headers = escape_line_feeds(options.delete :headers)
  @format = options.delete(:format)
  @cache_policy = options.delete(:cache_policy) || NSURLRequestUseProtocolCachePolicy
  @options = options
  @response = HTTP::Response.new

  @url = create_url(url_string)
  @body = create_request_body
  @request = create_request

  @connection = create_connection(request, self)
  @connection.start

  UIApplication.sharedApplication.networkActivityIndicatorVisible = true
end

Instance Attribute Details

#connection(connection, didReceiveAuthenticationChallenge: challenge) ⇒ Object

This delegate method get called every time a chunk of data is being received



149
150
151
# File 'motion/http.rb', line 149

def connection
  @connection
end

#credentialsObject

username & password has a hash



87
88
89
# File 'motion/http.rb', line 87

def credentials
  @credentials
end

#methodObject (readonly)

Returns the value of attribute method.



90
91
92
# File 'motion/http.rb', line 90

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



96
97
98
# File 'motion/http.rb', line 96

def options
  @options
end

#post_dataObject

Returns the value of attribute post_data.



89
90
91
# File 'motion/http.rb', line 89

def post_data
  @post_data
end

#proxy_credentialObject

credential supplied to proxy servers



88
89
90
# File 'motion/http.rb', line 88

def proxy_credential
  @proxy_credential
end

#requestObject

Returns the value of attribute request.



85
86
87
# File 'motion/http.rb', line 85

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



92
93
94
# File 'motion/http.rb', line 92

def response
  @response
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



94
95
96
# File 'motion/http.rb', line 94

def response_headers
  @response_headers
end

#response_sizeObject (readonly)

Returns the value of attribute response_size.



95
96
97
# File 'motion/http.rb', line 95

def response_size
  @response_size
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



93
94
95
# File 'motion/http.rb', line 93

def status_code
  @status_code
end

Instance Method Details

#connectionDidFinishLoading(connection) ⇒ Object



187
188
189
190
191
192
193
194
# File 'motion/http.rb', line 187

def connectionDidFinishLoading(connection)
  UIApplication.sharedApplication.networkActivityIndicatorVisible = false
  @request.done_loading!
  response_body = NSData.dataWithData(@received_data) if @received_data
  @response.update(status_code: status_code, body: response_body, headers: response_headers, url: @url)

  call_delegator_with_response
end

#to_sObject Also known as: description



136
137
138
139
# File 'motion/http.rb', line 136

def to_s
  "#<#{self.class}:#{self.object_id} - Method: #{@method}, url: #{@url.description}, body: #{@body.description}, Payload: #{@payload}, Headers: #{@headers} Credentials: #{@credentials}, Timeout: #{@timeout}, \
Cache policy: #{@cache_policy}, response: #{@response.inspect} >"
end