From 6dd9d97aaef3917868b870e01896ab937cbacfbb Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Fri, 24 Jan 2014 01:24:04 +0100 Subject: [PATCH] Use URI and CGI to convert query string into a hash --- lib/twitter/cursor.rb | 5 ++++ lib/twitter/search_results.rb | 44 +++++++---------------------------- 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/lib/twitter/cursor.rb b/lib/twitter/cursor.rb index 75d41eba8..4c12012e4 100644 --- a/lib/twitter/cursor.rb +++ b/lib/twitter/cursor.rb @@ -43,6 +43,7 @@ def initialize(attrs, key, klass, request) private + # @return [Integer] def next_cursor @attrs[:next_cursor] || -1 end @@ -53,16 +54,20 @@ def last? next_cursor.zero? end + # @return [Hash] def fetch_next_page response = @client.send(@request_method, @path, @options.merge(:cursor => next_cursor)) self.attrs = response[:body] end + # @param attrs [Hash] + # @return [Hash] def attrs=(attrs) @attrs = attrs Array(attrs[@key]).each do |element| @collection << (@klass ? @klass.new(element) : element) end + @attrs end end end diff --git a/lib/twitter/search_results.rb b/lib/twitter/search_results.rb index 8c206def7..c0ad8e63e 100644 --- a/lib/twitter/search_results.rb +++ b/lib/twitter/search_results.rb @@ -1,5 +1,7 @@ +require 'cgi' require 'twitter/enumerable' require 'twitter/utils' +require 'uri' module Twitter class SearchResults @@ -53,42 +55,24 @@ def next_page? # @return [Hash] The parameters needed to fetch the next page. def next_page if next_page? - query_string = strip_first_character(@attrs[:search_metadata][:next_results]) - query_string_to_hash(query_string) + query_string_to_hash(@attrs[:search_metadata][:next_results]) end end + # @return [Hash] def fetch_next_page response = @client.send(@request_method, @path, next_page) self.attrs = response[:body] end + # @param attrs [Hash] + # @return [Hash] def attrs=(attrs) @attrs = attrs Array(@attrs[:statuses]).collect do |tweet| @collection << Tweet.new(tweet) end - end - - # Returns the string with the first character removed - # - # @param string [String] - # @return [String] A copy of string without the first character. - # @example Returns the query string with the question mark removed - # strip_first_character("?foo=bar&baz=qux") #=> "foo=bar&baz=qux" - def strip_first_character(string) - strip_first_character!(string.dup) - end - - # Removes the first character from a string - # - # @param string [String] - # @return [String] The string without the first character. - # @example Remove the first character from a query string - # strip_first_character!("?foo=bar&baz=qux") #=> "foo=bar&baz=qux" - def strip_first_character!(string) - string[0] = '' - string + @attrs end # Converts query string to a hash @@ -98,18 +82,8 @@ def strip_first_character!(string) # @example Convert query string to a hash # query_string_to_hash("foo=bar&baz=qux") #=> {:foo=>"bar", :baz=>"qux"} def query_string_to_hash(query_string) - symbolize_keys(Faraday::Utils.parse_nested_query(query_string)) - end - - # Converts hash's keys to symbols - # - # @note Does not support nested hashes. - # @param hash [Hash] - # @return [Hash] The hash with symbols as keys. - # @example Convert hash's keys to symbols - # symbolize_keys({"foo"=>"bar", "baz"=>"qux"}) #=> {:foo=>"bar", :baz=>"qux"} - def symbolize_keys(hash) - Hash[hash.collect { |key, value| [key.to_sym, value] }] + query = CGI.parse(URI.parse(query_string).query) + Hash[query.collect { |key, value| [key.to_sym, value.first] }] end end end