lib/bubbles/endpoint.rb in bubbles-rest-client-0.0.8 vs lib/bubbles/endpoint.rb in bubbles-rest-client-0.1.0
- old
+ new
@@ -7,35 +7,55 @@
# In order to access an API Endpoint, an {RestEnvironment} must also be provided. This class is an abstract
# representation of an +Endpoint+, without any information provided as part of the Environment. In other words, an
# Endpoint can be used with any +RestEnvironment+.
#
class Endpoint
- ## Controls the method used to access the endpoint. Must be one of {Endpoint::Methods}.
+ ##
+ # Controls the method used to access the endpoint. Must be one of {Endpoint::Methods}.
# @return [Symbol] the method used to access the endpoint. Will always be one of the symbols defined in {Endpoint::METHODS}.
attr_accessor :method
- ## Controls the location, relative to the web root of the host, used to access the endpoint.
+ ##
+ # Controls the location, relative to the web root of the host, used to access the endpoint.
# @return [String] the location relative to the web root of the host used to access the endpoint
attr_accessor :location
- ## Controls whether authentication is required to access this endpoint. Defaults to false.
+ ##
+ # Controls whether authentication is required to access this endpoint. Defaults to false.
# @return [Boolean] true, if authentication is required to access this endpoint; false, otherwise.
attr_accessor :authentication_required
- ## Controls whether an API key is required to access this endpoint. Defaults to false.
+ ##
+ # Controls whether an API key is required to access this endpoint. Defaults to false.
# @return [Boolean] true, if an API key is required to access this endpoint; false, otherwise.
attr_accessor :api_key_required
+ ##
+ # Controls whether JSON is the expected form of output from this +Endpoint+.
+ # @return [Boolean] true, if JSON is the expected form of output from this +Endpoint+; false, otherwise.
+ attr_accessor :expect_json
+
+ ##
+ # Controls which data values should be encoded as part of an Authorization header. They will be separated with a
+ # colon in the order they are received and Base64-encoded.
+ # @return [Array] An array of +Symbol+s specifying which of the data attributes should be Base64-encoded as part of
+ # an Authorization header. The values will be encoded in the order they are received.
+ attr_accessor :encode_authorization
+
+ ##
+ # An array of parameters that are specified on the URI of this endpoint for each call.
+ attr_accessor :uri_params
+
## A template for specifying the complete URL for endpoints.
API_URL = ::Addressable::Template.new("{scheme}://{host}/{endpoint}")
## A template for specifying the complete URL for endpoints, with a port attached to the host.
API_URL_WITH_PORT = ::Addressable::Template.new("{scheme}://{host}:{port}/{endpoint}")
## The HTTP methods supported by a rest client utilizing Bubbles.
- METHODS = %w[get].freeze
+ METHODS = %w[get post patch put delete head].freeze
##
# Construct a new instance of an Endpoint.
#
# @param [Symbol] method The type of the new Endpoint to create. Must be one of the methods in {Endpoint::METHODS}.
@@ -44,22 +64,36 @@
# Defaults to +false+.
# @param [Boolean] api_key_required If true, then an API key is required to access this endpoint. Defaults to
# +false+.
# @param [String] name An optional name which will be given to the method that will execute this {Endpoint} within
# the context of a {RestClientResources} object.
+ # @param [Boolean] expect_json Whether or not to expect a JSON response from this +Endpoint+. Defaults to +false+.
#
- def initialize(method, location, auth_required = false, api_key_required = false, name = nil)
+ def initialize(method, location, auth_required = false, api_key_required = false, name = nil, expect_json = false, encode_authorization = {})
@method = method
@location = location
@auth_required = auth_required
@api_key_required = api_key_required
@name = name
+ @expect_json = expect_json
+ @encode_authorization = encode_authorization
+ @uri_params = []
# Strip the leading slash from the endpoint location, if it's there
if @location.to_s[0] == '/'
@location = @location.to_s.slice(1, @location.to_s.length)
end
+
+ # Extract URI parameters and create symbols for them
+ # URI parameters are enclosed by curly braces '{' and '}'
+ @location.to_s.split('/').each do |uri_segment|
+
+ match_data = /\{(.*)\}/.match(uri_segment)
+ unless match_data == nil
+ @uri_params.push(match_data[1].to_sym)
+ end
+ end
end
##
# Retrieve a +String+ that will identify this +Endpoint+ uniquely within a hash table.
#
@@ -99,20 +133,26 @@
#
# @param [RestEnvironment] env The +RestEnvironment+ to use to access this +Endpoint+.
#
# @return [String] A +String+ containing the full URL to access this +Endpoint+ on the given {RestEnvironment}.
#
- def get_expanded_url(env)
+ def get_expanded_url(env, uri_params = {})
url = get_base_url env
if is_complex?
- special_url_string = '{scheme}://{environment_host}/'
+ special_url_string = '{scheme}://{host}/'
unless @port == 80 || @port == 443
- special_url_string = '{scheme}://{environment_host}:{port}/'
+ special_url_string = '{scheme}://{host}:{port}/'
end
special_url_string = special_url_string + @location
+
+ uri_params.each do |param, value|
+ needle = "{#{param.to_s}}"
+ special_url_string = special_url_string.sub(needle, value.to_s)
+ end
+
url = ::Addressable::Template.new(special_url_string)
return url.expand(scheme: env.scheme, host: env.host, port: env.port)
end
@@ -148,10 +188,18 @@
def authenticated?
@auth_required
end
##
+ # Determine if an API key is required
+ #
+ # @return [Boolean] true, if an API key is required to make the request; false, otherwise.
+ def api_key_required?
+ api_key_required
+ end
+
+ ##
# Set the name of the method on {RestClientResources} used to access this {Endpoint}.
#
# @param [String] name The name of the method used to access this {Endpoint}.
#
def name=(name)
@@ -174,8 +222,22 @@
# @return [Boolean] true, if this {Endpoint} has a method name that is different than the +location+ name specified
# for the +Endpoint+, to be defined on {RestClientResources}; false, otherwise.
#
def name?
@name == nil
+ end
+
+ ##
+ # Whether or not an Authorization header should be Base64-encoded.
+ #
+ # @return [Boolean] true, if attributes from the data array have been specified to be Base64-encoded as part of an
+ # Authorization header; false, otherwise.
+ #
+ def encode_authorization_header?
+ @encode_authorization.length > 0
+ end
+
+ def has_uri_params?
+ !@uri_params.empty?
end
end
end
\ No newline at end of file