Class: Bubbles::Endpoint
- Inherits:
-
Object
- Object
- Bubbles::Endpoint
- Defined in:
- lib/bubbles/endpoint.rb
Overview
Representation of a single API endpoint within the Bubbles infrastructure.
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
.
Constant Summary collapse
- API_URL =
A template for specifying the complete URL for endpoints.
::Addressable::Template.new("{scheme}://{host}/{endpoint}")
- API_URL_WITH_PORT =
A template for specifying the complete URL for endpoints, with a port attached to the host.
::Addressable::Template.new("{scheme}://{host}:{port}/{endpoint}")
- METHODS =
The HTTP methods supported by a rest client utilizing Bubbles.
%w[get post patch put delete head].freeze
- RETURN_TYPES =
The possible return types for successful REST calls. Defaults to :body_as_string.
%w[full_response body_as_string body_as_object].freeze
Instance Attribute Summary collapse
-
#api_key_required ⇒ Boolean
Controls whether an API key is required to access this endpoint.
-
#authentication_required ⇒ Boolean
Controls whether authentication is required to access this endpoint.
-
#encode_authorization ⇒ Array
Controls which data values should be encoded as part of an Authorization header.
-
#location ⇒ String
Controls the location, relative to the web root of the host, used to access the endpoint.
-
#method ⇒ Symbol
Controls the method used to access the endpoint.
-
#return_type ⇒ Object
Retrieve the return type of this REST endpoint.
-
#uri_params ⇒ Object
An array of parameters that are specified on the URI of this endpoint for each call.
Instance Method Summary collapse
- #additional_headers ⇒ Object
-
#api_key_required? ⇒ Boolean
Determine if an API key is required.
-
#authenticated? ⇒ Boolean
Determine if this
Endpoint
requires authentication/authorization to utilize. -
#encode_authorization_header? ⇒ Boolean
Whether or not an Authorization header should be Base64-encoded.
-
#get_base_url(env) ⇒ Addressable::Template
Retrieve the base URL template for this
Endpoint
, given aRestEnvironment
. -
#get_expanded_url(env, uri_params = {}) ⇒ Addressable::URI
Retrieve the URL to access this
Endpoint
, as aString
with all parameters expanded. -
#get_key_string ⇒ String
Retrieve a
String
that will identify thisEndpoint
uniquely within a hash table. -
#get_location_string ⇒ String
Retrieve a String representing the location of this Endpoint.
- #has_additional_headers? ⇒ Boolean
- #has_uri_params? ⇒ Boolean
-
#initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, encode_authorization = {}, headers = {}) ⇒ Endpoint
constructor
Construct a new instance of an Endpoint.
-
#is_complex? ⇒ Boolean
Determine if the location for this Endpoint is complex.
-
#name ⇒ String
Retrieve the name of the method on RestClientResources used to access this Endpoint.
-
#name=(name) ⇒ Object
Set the name of the method on RestClientResources used to access this Endpoint.
-
#name? ⇒ Boolean
Determine if this Endpoint has a method name, different from the
location
name, specified for it.
Constructor Details
#initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, encode_authorization = {}, headers = {}) ⇒ Endpoint
Construct a new instance of an Endpoint.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bubbles/endpoint.rb', line 77 def initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, = {}, headers = {}) @method = method @location = location @auth_required = auth_required @api_key_required = api_key_required @name = name @encode_authorization = @additional_headers = headers unless Endpoint::RETURN_TYPES.include? return_type.to_s return_type = :body_as_string end @return_type = return_type @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 |
Instance Attribute Details
#api_key_required ⇒ Boolean
Controls whether an API key is required to access this endpoint. Defaults to false.
31 32 33 |
# File 'lib/bubbles/endpoint.rb', line 31 def api_key_required @api_key_required end |
#authentication_required ⇒ Boolean
Controls whether authentication is required to access this endpoint. Defaults to false.
26 27 28 |
# File 'lib/bubbles/endpoint.rb', line 26 def authentication_required @authentication_required end |
#encode_authorization ⇒ Array
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.
44 45 46 |
# File 'lib/bubbles/endpoint.rb', line 44 def @encode_authorization end |
#location ⇒ String
Controls the location, relative to the web root of the host, used to access the endpoint.
21 22 23 |
# File 'lib/bubbles/endpoint.rb', line 21 def location @location end |
#method ⇒ Symbol
Controls the method used to access the endpoint. Must be one of Endpoint::Methods.
16 17 18 |
# File 'lib/bubbles/endpoint.rb', line 16 def method @method end |
#return_type ⇒ Object
Retrieve the return type of this REST endpoint.
This will always be one of:
-
full_response
: Indicates that the fullResponse
object should be returned so that headers and return code can be used. -
body_as_object
: Indicates that the body of theResponse
should be parsed as a fullOpenStruct
object and returned. -
body_as_string
: Indicates that only the body of theResponse
object should be returned, as aString
.
By default, if this is not specified, it will be body_asstring+.
37 38 39 |
# File 'lib/bubbles/endpoint.rb', line 37 def return_type @return_type end |
#uri_params ⇒ Object
An array of parameters that are specified on the URI of this endpoint for each call.
48 49 50 |
# File 'lib/bubbles/endpoint.rb', line 48 def uri_params @uri_params end |
Instance Method Details
#additional_headers ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/bubbles/endpoint.rb', line 274 def additional_headers unless @additional_headers @additional_headers = {} end @additional_headers end |
#api_key_required? ⇒ Boolean
Determine if an API key is required
210 211 212 |
# File 'lib/bubbles/endpoint.rb', line 210 def api_key_required? api_key_required end |
#authenticated? ⇒ Boolean
Determine if this Endpoint
requires authentication/authorization to utilize
202 203 204 |
# File 'lib/bubbles/endpoint.rb', line 202 def authenticated? @auth_required end |
#encode_authorization_header? ⇒ Boolean
Whether or not an Authorization header should be Base64-encoded.
249 250 251 |
# File 'lib/bubbles/endpoint.rb', line 249 def !@encode_authorization.nil? and @encode_authorization.length > 0 end |
#get_base_url(env) ⇒ Addressable::Template
Retrieve the base URL template for this Endpoint
, given a RestEnvironment
.
136 137 138 139 140 141 142 |
# File 'lib/bubbles/endpoint.rb', line 136 def get_base_url(env) unless env.port == 80 || env.port == 443 return API_URL_WITH_PORT end API_URL end |
#get_expanded_url(env, uri_params = {}) ⇒ Addressable::URI
Retrieve the URL to access this Endpoint
, as a String
with all parameters expanded.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/bubbles/endpoint.rb', line 152 def (env, uri_params = {}) url = get_base_url env if is_complex? special_url_string = '{scheme}://{host}/' unless @port == 80 || @port == 443 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.(scheme: env.scheme, host: env.host, port: env.port) end url.(scheme: env.scheme, host: env.host, port: env.port, endpoint: @location) end |
#get_key_string ⇒ String
Retrieve a String
that will identify this Endpoint
uniquely within a hash table.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bubbles/endpoint.rb', line 115 def get_key_string auth_string = '-unauthenticated' if @auth_required auth_string = '-authenticated' end api_key_string = '' if @api_key_required api_key_string = '-with-api-key' end method.to_s + "-" + @location.to_s + auth_string + api_key_string end |
#get_location_string ⇒ String
Retrieve a String representing the location of this Endpoint.
Complex Endpoints will have instances of ‘/’ replaced with ‘_’.
190 191 192 193 194 195 196 |
# File 'lib/bubbles/endpoint.rb', line 190 def get_location_string unless is_complex? return @location end @location.to_s.gsub('/', '_') end |
#has_additional_headers? ⇒ Boolean
282 283 284 |
# File 'lib/bubbles/endpoint.rb', line 282 def has_additional_headers? not additional_headers.empty? end |
#has_uri_params? ⇒ Boolean
270 271 272 |
# File 'lib/bubbles/endpoint.rb', line 270 def has_uri_params? !@uri_params.empty? end |
#is_complex? ⇒ Boolean
Determine if the location for this Endpoint is complex.
180 181 182 |
# File 'lib/bubbles/endpoint.rb', line 180 def is_complex? @location.include? '/' end |
#name ⇒ String
Retrieve the name of the method on RestClientResources used to access this Bubbles::Endpoint.
229 230 231 |
# File 'lib/bubbles/endpoint.rb', line 229 def name @name end |
#name=(name) ⇒ Object
Set the name of the method on RestClientResources used to access this Bubbles::Endpoint.
219 220 221 |
# File 'lib/bubbles/endpoint.rb', line 219 def name=(name) @name = name end |
#name? ⇒ Boolean
Determine if this Bubbles::Endpoint has a method name, different from the location
name, specified for it.
239 240 241 |
# File 'lib/bubbles/endpoint.rb', line 239 def name? @name != nil end |