Skip to content

Commit

Permalink
Refactor common code from Twitter::SearchResults
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 12, 2013
1 parent e949d2e commit fdd8388
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions lib/twitter/search_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,69 @@ def next_results?

# Returns a Hash of query parameters for the next result in the search
#
# Returned Hash can be merged into the previous search options list
# to easily access the next page
#
# @return [Hash]
# @note Returned Hash can be merged into the previous search options list to easily access the next page.
# @return [Hash] The parameters needed to fetch the next page.
def next_results
Faraday::Utils.parse_nested_query(@attrs[:search_metadata][:next_results][1..-1]).inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo} if next_results?
if next_results?
query_string = strip_first_character(@attrs[:search_metadata][:next_results])
query_string_to_hash(query_string)
end
end
alias next_page next_results

# Returns a Hash of query parameters for the refresh URL in the search
#
# Returned Hash can be merged into the previous search options list
# to easily access the refresh page
#
# @return [Hash]
# @note Returned Hash can be merged into the previous search options list to easily access the refresh page.
# @return [Hash] The parameters needed to refresh the page.
def refresh_url
Faraday::Utils.parse_nested_query(@attrs[:search_metadata][:refresh_url][1..-1]).inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo}
query_string = strip_first_character(@attrs[:search_metadata][:refresh_url])
query_string_to_hash(query_string)
end
alias refresh_page refresh_url

private

# 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
end

# Converts query string to a hash
#
# @param query_string [String] The query string of a URL.
# @return [Hash] The query string converted to a hash (with symbol keys).
# @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.map{|key, value| [key.to_sym, value]}]
end

end
end

0 comments on commit fdd8388

Please sign in to comment.