Permalink
Browse files

Recursively merge connection options

Closes #192.
  • Loading branch information...
1 parent 591cbf1 commit 23b613feb873fc2d1386cbc66ac96e53e1fa498d @sferik committed Oct 31, 2011
Showing with 25 additions and 7 deletions.
  1. +5 −3 lib/twitter/connection.rb
  2. +20 −4 lib/twitter/core_ext/hash.rb
@@ -1,4 +1,5 @@
require 'faraday'
+require 'twitter/core_ext/hash'
require 'twitter/request/gateway'
require 'twitter/request/multipart_with_file'
require 'twitter/request/phoenix'
@@ -13,18 +14,19 @@ module Connection
# Returns a Faraday::Connection object
#
+ # @param options [Hash] A hash of options
# @return [Faraday::Connection]
def connection(options={})
- merged_options = connection_options.merge({
+ default_options = {
:headers => {
:accept => 'application/json',
:user_agent => user_agent,
},
:proxy => proxy,
:ssl => {:verify => false},
:url => options.fetch(:endpoint, endpoint),
- })
- Faraday.new(merged_options) do |builder|
+ }
+ Faraday.new(connection_options.deep_merge(default_options)) do |builder|
builder.use Twitter::Request::Phoenix if options[:phoenix]
builder.use Twitter::Request::MultipartWithFile
builder.use Twitter::Request::TwitterOAuth, credentials if credentials?
@@ -1,6 +1,22 @@
class Hash
- # Take a single user ID or screen name and merge it into the hash with the correct key
+ # Merges self with another hash, recursively
+ #
+ # @param hash [Hash] The hash to merge
+ # @return [Hash]
+ def deep_merge(hash)
+ target = self.dup
+ hash.keys.each do |key|
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
+ target[key] = target[key].deep_merge(hash[key])
+ next
+ end
+ target[key] = hash[key]
+ end
+ target
+ end
+
+ # Take a single user ID or screen name and merge it into self with the correct key
#
# @param user_id_or_screen_name [Integer, String] A Twitter user ID or screen_name.
# @return [Hash]
@@ -14,7 +30,7 @@ def merge_user!(user_id_or_screen_name)
self
end
- # Take a multiple user IDs and screen names and merge them into the hash with the correct keys
+ # Take a multiple user IDs and screen names and merge them into self with the correct keys
#
# @param users_id_or_screen_names [Array] An array of Twitter user IDs or screen_names.
# @return [Hash]
@@ -33,7 +49,7 @@ def merge_users!(user_ids_or_screen_names)
self
end
- # Take a single owner ID or owner screen name and merge it into the hash with the correct key
+ # Take a single owner ID or owner screen name and merge it into self with the correct key
# (for Twitter API endpoints that want :owner_id and :owner_screen_name)
#
# @param owner_id_or_owner_screen_name [Integer, String] A Twitter user ID or screen_name.
@@ -48,7 +64,7 @@ def merge_owner!(owner_id_or_owner_screen_name)
self
end
- # Take a single list ID or slug and merge it into the hash with the correct key
+ # Take a single list ID or slug and merge it into self with the correct key
#
# @param list_id_or_slug [Integer, String] A Twitter list ID or slug.
# @return [Hash]

0 comments on commit 23b613f

Please sign in to comment.