Module: Swiftype::Configuration

Included in:
Swiftype
Defined in:
lib/swiftype/configuration.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
"https://api.swiftype.com/api/v1/"
DEFAULT_USER_AGENT =
"Swiftype-Ruby/#{Swiftype::VERSION}"
VALID_OPTIONS_KEYS =
[
  :api_key,
  :user_agent,
  :platform_client_id,
  :platform_client_secret,
  :endpoint
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



19
20
21
# File 'lib/swiftype/configuration.rb', line 19

def self.extended(base)
  base.reset
end

Instance Method Details

#authenticated_url=(url) ⇒ Object

Set api_key and endpoint based on a URL with HTTP authentication. Useful if you’re using the Swiftype Heroku add-on.



50
51
52
53
54
55
56
# File 'lib/swiftype/configuration.rb', line 50

def authenticated_url=(url)
  uri = URI(url)
  self.api_key = uri.user
  uri.user = nil
  uri.password = nil
  self.endpoint = uri.to_s
end

#configure {|_self| ... } ⇒ Object

Yields the Swiftype::Configuration module which can be used to set configuration options.

Yields:

  • (_self)

Yield Parameters:

Returns:

  • self



36
37
38
39
# File 'lib/swiftype/configuration.rb', line 36

def configure
  yield self
  self
end

#endpoint=(endpoint) ⇒ Object

setter for endpoint that ensures it always ends in ‘/’



59
60
61
62
63
64
65
# File 'lib/swiftype/configuration.rb', line 59

def endpoint=(endpoint)
  if endpoint.end_with?('/')
    @endpoint = endpoint
  else
    @endpoint = "#{endpoint}/"
  end
end

#optionsObject

Return a hash of the configured options.



42
43
44
45
46
# File 'lib/swiftype/configuration.rb', line 42

def options
  options = {}
  VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
  options
end

#resetObject

Reset configuration to default values.



24
25
26
27
28
29
30
31
# File 'lib/swiftype/configuration.rb', line 24

def reset
  self.api_key = nil
  self.endpoint = DEFAULT_ENDPOINT
  self.user_agent = DEFAULT_USER_AGENT
  self.platform_client_id = nil
  self.platform_client_secret = nil
  self
end