lib/contextio/api.rb in contextio-1.5.0 vs lib/contextio/api.rb in contextio-1.6.0
- old
+ new
@@ -50,10 +50,12 @@
def user_agent_string
self.class.user_agent_string
end
+ attr_accessor :base_url, :version
+
# @!attribute [r] key
# @return [String] The OAuth key for the user's Context.IO account.
# @!attribute [r] secret
# @return [String] The OAuth secret for the user's Context.IO account.
# @!attribute [r] opts
@@ -65,20 +67,22 @@
# @param [Hash] opts Optional options for OAuth connections. ie. :timeout and :open_timeout are supported
def initialize(key, secret, opts={})
@key = key
@secret = secret
@opts = opts || {}
+ @base_url = self.class.base_url
+ @version = self.class.version
end
# Generates the path for a resource_path and params hash for use with the API.
#
# @param [String] resource_path The resource_path or full resource URL for
# the resource being acted on.
# @param [{String, Symbol => String, Symbol, Array<String, Symbol>}] params
# A Hash of the query parameters for the action represented by this path.
def path(resource_path, params = {})
- "/#{API.version}/#{API.strip_resource_path(resource_path)}#{API.hash_to_url_params(params)}"
+ "/#{version}/#{strip_resource_path(resource_path)}#{API.hash_to_url_params(params)}"
end
# Makes a request against the Context.IO API.
#
# @param [String, Symbol] method The HTTP verb for the request (lower case).
@@ -127,29 +131,33 @@
# request.
#
# @return [Net::HTTP*] The response object from the request.
def oauth_request(method, resource_path, params, headers=nil)
headers ||= { 'Accept' => 'application/json', 'User-Agent' => user_agent_string }
+ normalized_params = params.inject({}) do |normalized_params, (key, value)|
+ normalized_params[key.to_sym] = value
+ normalized_params
+ end
# The below array used to include put, too, but there is a weirdness in
# the oauth gem with PUT and signing requests. See
# https://github.com/oauth/oauth-ruby/pull/34#issuecomment-5862199 for
# some discussion on the matter. This is a work-around.
if %w(post).include? method.to_s.downcase
- token.request(method, path(resource_path), params, headers)
+ token.request(method, path(resource_path), normalized_params, headers)
else # GET, DELETE, HEAD, etc.
- token.request(method, path(resource_path, params), nil, headers)
+ token.request(method, path(resource_path, normalized_params), nil, headers)
end
end
# So that we can accept full URLs, this strips the domain and version number
# out and returns just the resource path.
#
# @param [#to_s] resource_path The full URL or path for a resource.
#
# @return [String] The resource path.
- def self.strip_resource_path(resource_path)
+ def strip_resource_path(resource_path)
resource_path.to_s.gsub("#{base_url}/#{version}/", '')
end
# Context.IO's API expects query parameters that are arrays to be comma
# separated, rather than submitted more than once. This munges those arrays
@@ -173,10 +181,10 @@
# @!attribute [r] consumer
# @return [OAuth::Consumer] An Oauth consumer object for credentials
# purposes.
def consumer
- @consumer ||= OAuth::Consumer.new(key, secret, @opts.merge(site: API.base_url))
+ @consumer ||= OAuth::Consumer.new(key, secret, @opts.merge(site: base_url))
end
# @!attribute [r] token
# @return [Oauth::AccessToken] An Oauth token object for credentials
# purposes.