Class: Wordniknik::Configuration
- Inherits:
-
Object
- Object
- Wordniknik::Configuration
- Defined in:
- lib/wordniknik/configuration.rb
Overview
Wordnik API configuration class
Instance Attribute Summary collapse
-
#api_host ⇒ Object
Returns the value of attribute api_host.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_port ⇒ Object
Returns the value of attribute api_port.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #inspect ⇒ Object
- #look_for_config_file ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wordniknik/configuration.rb', line 11 def initialize defaults = { api_host: 'api.wordnik.com', api_port: 443, api_version: 'v4' } loaded = look_for_config_file @api_key = loaded['api_key'] || ENV.fetch('WORDNIK_API_KEY', nil) @api_host = loaded['api_host'] || defaults[:api_host] @api_port = loaded['api_port'] || defaults[:api_port] @api_version = loaded['api_version'] || defaults[:api_version] return unless @api_key.nil? raise "No API key found. Please set WORDNIK_API_KEY or in a #{WORDNIK_CONFIG_FILE} file." end |
Instance Attribute Details
#api_host ⇒ Object
Returns the value of attribute api_host.
9 10 11 |
# File 'lib/wordniknik/configuration.rb', line 9 def api_host @api_host end |
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/wordniknik/configuration.rb', line 9 def api_key @api_key end |
#api_port ⇒ Object
Returns the value of attribute api_port.
9 10 11 |
# File 'lib/wordniknik/configuration.rb', line 9 def api_port @api_port end |
#api_version ⇒ Object
Returns the value of attribute api_version.
9 10 11 |
# File 'lib/wordniknik/configuration.rb', line 9 def api_version @api_version end |
#connection ⇒ Object
Returns the value of attribute connection.
9 10 11 |
# File 'lib/wordniknik/configuration.rb', line 9 def connection @connection end |
Instance Method Details
#inspect ⇒ Object
41 42 43 |
# File 'lib/wordniknik/configuration.rb', line 41 def inspect to_s end |
#look_for_config_file ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/wordniknik/configuration.rb', line 27 def look_for_config_file if File.exist?(WORDNIK_CONFIG_FILE) YAML.load_file(WORDNIK_CONFIG_FILE) elsif File.exist?(File.join(Dir.home, WORDNIK_CONFIG_FILE)) YAML.load_file(File.join(Dir.home, WORDNIK_CONFIG_FILE)) else {} end end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/wordniknik/configuration.rb', line 37 def to_s "<Configuration api_key: *****, api_host: #{@api_host}:#{@api_port}, api_version: #{@api_version}>" end |