motion/http.rb in bubble-wrap-1.1.3 vs motion/http.rb in bubble-wrap-1.1.4

- old
+ new

@@ -16,59 +16,36 @@ # # GET request with basic auth credentials # BubbleWrap::HTTP.get("https://api.github.com/users/mattetti", {credentials: {username: 'matt', password: 'aimonetti'}}) do |response| # p response.body.to_str # prints the response's body # end # - def self.get(url, options={}, &block) - create_query(url, :get, options, block) - end - # Make a POST request - def self.post(url, options={}, &block) - create_query(url, :post, options, block) - end + [:get, :post, :put, :delete, :head, :patch].each do |http_verb| - # Make a PUT request - def self.put(url, options={}, &block) - create_query(url, :put, options, block) - end + define_singleton_method(http_verb) do |url, options = {}, &block| + options[:action] = block if block + HTTP::Query.new(url, http_verb, options) + end - # Make a DELETE request - def self.delete(url, options={}, &block) - create_query(url, :delete, options, block) end - # Make a HEAD request - def self.head(url, options={}, &block) - create_query(url, :head, options, block) - end - - # Make a PATCH request - def self.patch(url, options={}, &block) - create_query(url, :patch, options, block) - end - - def self.create_query(url, method, options, block) - options[:action] = block if block - HTTP::Query.new(url, method, options) - end - # Response class wrapping the results of a Query's response class Response attr_reader :body attr_reader :headers - attr_accessor :status_code, :error_message + attr_accessor :status_code, :status_description, :error_message attr_reader :url def initialize(values={}) self.update(values) end def update(values) values.each do |k,v| self.instance_variable_set("@#{k}", v) end + update_status_description end def ok? status_code.to_s =~ /20\d/ ? true : false end @@ -76,10 +53,13 @@ def to_s "#<#{self.class}:#{self.object_id} - url: #{self.url}, body: #{self.body}, headers: #{self.headers}, status code: #{self.status_code}, error message: #{self.error_message} >" end alias description to_s + def update_status_description + @status_description = status_code.nil? ? nil : NSHTTPURLResponse.localizedStringForStatusCode(status_code) + end end # Class wrapping NSConnection and often used indirectly by the BubbleWrap::HTTP module methods. class Query attr_accessor :request @@ -354,10 +334,10 @@ def escape_line_feeds(hash) return nil if hash.nil? escaped_hash = {} - hash.each{|k,v| escaped_hash[k] = v.gsub("\n", CLRF) } + hash.each{|k,v| escaped_hash[k] = v.gsub("\n", CLRF) if v } escaped_hash end def patch_nsurl_request(request) request.instance_variable_set("@done_loading", false)