Skip to content

Commit

Permalink
Defines get/post/put/delete in BaseClient and override changes in chi…
Browse files Browse the repository at this point in the history
…ldren
  • Loading branch information
philnash committed Mar 17, 2015
1 parent d0231a6 commit b7da723
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
26 changes: 26 additions & 0 deletions lib/twilio-ruby/rest/base_client.rb
Expand Up @@ -42,6 +42,25 @@ def initialize(*args)
set_up_subresources
end

##
# Define #get, #put, #post and #delete helper methods for sending HTTP
# requests to Twilio. You shouldn't need to use these methods directly,
# but they can be useful for debugging. Each method returns a hash
# obtained from parsing the JSON object in the response body.
[:get, :put, :post, :delete].each do |method|
method_class = Net::HTTP.const_get method.to_s.capitalize
define_method method do |path, *args|
params = twilify(args[0])
params = {} if params.empty?
# build the full path unless already given
path = build_full_path(path, params, method) unless args[1]
request = method_class.new(path, HTTP_HEADERS)
request.basic_auth(@account_sid, @auth_token)
request.form_data = params if [:post, :put].include?(method)
connect_and_send(request)
end
end

protected

##
Expand All @@ -51,6 +70,13 @@ def get_defaults
DEFAULTS
end

##
# Builds up full request path
# Needs implementation in child classes
def build_full_path(path, params, method)
raise NotImplementedError
end

##
# Set up and cache a Net::HTTP object to use when making requests. This is
# a private method documented for completeness.
Expand Down
28 changes: 8 additions & 20 deletions lib/twilio-ruby/rest/client.rb
Expand Up @@ -127,26 +127,6 @@ def inspect # :nodoc:
"<Twilio::REST::Client @account_sid=#{@account_sid}>"
end

##
# Define #get, #put, #post and #delete helper methods for sending HTTP
# requests to Twilio. You shouldn't need to use these methods directly,
# but they can be useful for debugging. Each method returns a hash
# obtained from parsing the JSON object in the response body.
[:get, :put, :post, :delete].each do |method|
method_class = Net::HTTP.const_get method.to_s.capitalize
define_method method do |path, *args|
params = twilify args[0]; params = {} if params.empty?
unless args[1] # build the full path unless already given
path = "#{path}.json"
path << "?#{url_encode(params)}" if method == :get && !params.empty?
end
request = method_class.new path, HTTP_HEADERS
request.basic_auth @account_sid, @auth_token
request.form_data = params if [:post, :put].include? method
connect_and_send request
end
end

##
# Delegate account methods from the client. This saves having to call
# <tt>client.account</tt> every time for resources on the default
Expand Down Expand Up @@ -175,6 +155,14 @@ def set_up_subresources # :doc:
@accounts = Twilio::REST::Accounts.new "/#{API_VERSION}/Accounts", self
@account = @accounts.get @account_sid
end

##
# Builds up full request path
def build_full_path(path, params, method)
path = "#{path}.json"
path << "?#{url_encode(params)}" if method == :get && !params.empty?
path
end
end
end
end
28 changes: 8 additions & 20 deletions lib/twilio-ruby/rest/task_router_client.rb
Expand Up @@ -94,26 +94,6 @@ def initialize(*args)
super(*args)
end

##
# Define #get, #put, #post and #delete helper methods for sending HTTP
# requests to Twilio. You shouldn't need to use these methods directly,
# but they can be useful for debugging. Each method returns a hash
# obtained from parsing the JSON object in the response body.
[:get, :put, :post, :delete].each do |method|
method_class = Net::HTTP.const_get method.to_s.capitalize
define_method method do |path, *args|
params = twilify args[0]; params = {} if params.empty?
unless args[1] # build the full path unless already given
path = path.dup
path << "?#{url_encode(params)}" if method == :get && !params.empty?
end
request = method_class.new path, HTTP_HEADERS
request.basic_auth @account_sid, @auth_token
request.form_data = params if [:post, :put].include? method
connect_and_send request
end
end

def inspect # :nodoc:
"<Twilio::REST::TaskRouterClient @account_sid=#{@account_sid}>"
end
Expand Down Expand Up @@ -210,6 +190,14 @@ def set_up_subresources # :doc:
@workspace = @workspaces.get @workspace_sid
end

##
# Builds up full request path
def build_full_path(path, params, method)
path = path.dup
path << "?#{url_encode(params)}" if method == :get && !params.empty?
path
end

end
end
end

0 comments on commit b7da723

Please sign in to comment.