Class: Vertebrae::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector, Authorization, Constants
Defined in:
lib/vertebrae/configuration.rb

Constant Summary collapse

VALID_OPTIONS_KEYS =
[
    :prefix,
    :ssl,
    :mime_type,
    :user_agent,
    :host,
    :username,
    :password,
    :connection_options,
    :content_type,
    :scheme,
    :port,
    :additional_headers
].freeze
DEFAULT_SCHEME =

The default HTTP scheme configuration

'https'
DEFAULT_SSL =

The default SSL configuration

{}
DEFAULT_PORT =

by default do not set a port so that it is not specified on endpoint

nil
DEFAULT_MIME_TYPE =

By default the Accept header will make a request for JSON

:json
DEFAULT_USER_AGENT =

The value sent in the http header for ‘User-Agent’ if none is set

"Vertebrae REST Gem".freeze
DEFAULT_HOST =

by default do not set a host. this is specific to AK instance

nil
DEFAULT_PREFIX =

The api endpoint used to connect to AK if none is set

'/'.freeze
DEFAULT_USERNAME =

By default, don’t set a user ame

nil
DEFAULT_PASSWORD =

By default, don’t set a user password

nil
DEFAULT_CONNECTION_OPTIONS =

By default uses the Faraday connection options if none is set

{}
DEFAULT_CONTENT_TYPE =
'application/json'.freeze
DEFAULT_ADDITIONAL_HEADERS =
{}.freeze

Constants included from Constants

Vertebrae::Constants::ACCEPT, Vertebrae::Constants::ACCEPT_CHARSET, Vertebrae::Constants::CACHE_CONTROL, Vertebrae::Constants::CONTENT_LENGTH, Vertebrae::Constants::CONTENT_TYPE, Vertebrae::Constants::DATE, Vertebrae::Constants::ETAG, Vertebrae::Constants::LOCATION, Vertebrae::Constants::RATELIMIT_LIMIT, Vertebrae::Constants::RATELIMIT_REMAINING, Vertebrae::Constants::SERVER, Vertebrae::Constants::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authorization

#authenticated?, #authentication, #basic_auth, #password?, #username?

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



84
85
86
87
88
89
90
91
# File 'lib/vertebrae/configuration.rb', line 84

def initialize(options)
  self.options = options
  self.default_options = {}

  VALID_OPTIONS_KEYS.each do |key|
    default_options[key] = "Vertebrae::Configuration::DEFAULT_#{key.to_s.upcase}".constantize
  end
end

Instance Attribute Details

#default_optionsObject

Returns the value of attribute default_options.



82
83
84
# File 'lib/vertebrae/configuration.rb', line 82

def default_options
  @default_options
end

#optionsObject

Returns the value of attribute options.



81
82
83
# File 'lib/vertebrae/configuration.rb', line 81

def options
  @options
end

Instance Method Details

#adapterObject



73
74
75
# File 'lib/vertebrae/configuration.rb', line 73

def adapter
  options[:adapter] || ::Faraday.default_adapter
end

#adapter=(value) ⇒ Object



77
78
79
# File 'lib/vertebrae/configuration.rb', line 77

def adapter=(value)
  options[:adapter] = value
end

#endpointObject



119
120
121
# File 'lib/vertebrae/configuration.rb', line 119

def endpoint
  "#{self.scheme}://#{self.host}#{self.port.present? ? ":#{self.port}" : ''}#{self.prefix}"
end

#faraday_optionsObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vertebrae/configuration.rb', line 93

def faraday_options
  {
    :headers => {
      ACCEPT           => "application/json;q=0.1",
      ACCEPT_CHARSET   => "utf-8",
      USER_AGENT       => user_agent,
      CONTENT_TYPE     => content_type
    }.merge(additional_headers),
    :ssl => ssl,
    :url => endpoint
  }.merge(connection_options)
end

#process_basic_auth(auth) ⇒ Object

Extract login and password from basic_auth parameter



109
110
111
112
113
114
115
116
117
# File 'lib/vertebrae/configuration.rb', line 109

def process_basic_auth(auth)
  case auth
    when String
      self.username, self.password = auth.split(':', 2)
    when Hash
      self.username = auth[:username]
      self.password = auth[:password]
  end
end