Skip to content

Commit

Permalink
Use URI and CGI to convert query string into a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 24, 2014
1 parent 300bae7 commit 6dd9d97
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
5 changes: 5 additions & 0 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def initialize(attrs, key, klass, request)

private

# @return [Integer]
def next_cursor
@attrs[:next_cursor] || -1
end
Expand All @@ -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
44 changes: 9 additions & 35 deletions lib/twitter/search_results.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'cgi'
require 'twitter/enumerable'
require 'twitter/utils'
require 'uri'

module Twitter
class SearchResults
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6dd9d97

Please sign in to comment.