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.

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.


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

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


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

def connection
  @connection
end

#credentialsObject

username & password has a hash


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

def credentials
  @credentials
end

#methodObject (readonly)

Returns the value of attribute method


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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#post_dataObject

Returns the value of attribute post_data


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

def post_data
  @post_data
end

#proxy_credentialObject

credential supplied to proxy servers


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

def proxy_credential
  @proxy_credential
end

#requestObject

Returns the value of attribute request


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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response


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

def response
  @response
end

#response_headersObject (readonly)

Returns the value of attribute response_headers


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

def response_headers
  @response_headers
end

#response_sizeObject (readonly)

Returns the value of attribute response_size


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

def response_size
  @response_size
end

#status_codeObject (readonly)

Returns the value of attribute status_code


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

def status_code
  @status_code
end

Instance Method Details

#connectionDidFinishLoading(connection) ⇒ Object


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

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


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

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