class Glass::ApiKeys

just a small class to organize the api key logic info from the yml file in config.

Attributes

client_id[RW]
client_secret[RW]
google_api_keys[RW]
keys[RW]
yaml_config[RW]
yaml_file_contents[RW]

Public Class Methods

generate!() click to toggle source
# File lib/glass/api_keys.rb, line 16
def self.generate!
  keys_instance = self.new
  keys_instance.keys
  ::Glass.client_secret = keys_instance.keys.client_secret
  ::Glass.client_id = keys_instance.keys.client_id
  ::Glass._api_keys = keys_instance.keys
end
new() click to toggle source
# File lib/glass/api_keys.rb, line 9
def initialize
  self.google_api_keys = {}
  preload_yaml_file_keys
  load_google_keys_from_whereever
  load_keys
  return self.keys
end

Private Instance Methods

google_api_keys_are_empty?() click to toggle source
# File lib/glass/api_keys.rb, line 63
def google_api_keys_are_empty?
  google_api_keys[:client_id].nil? or google_api_keys[:client_secret].nil?
end
load_google_keys_from_whereever() click to toggle source
# File lib/glass/api_keys.rb, line 43
def load_google_keys_from_whereever 
  [:client_id, :client_secret].each do |key|
    google_api_keys[key] = ::Glass.send(key) 
    unless google_api_keys[key]
      load_yaml_as_auxiliary_method(key)
    end
  end
end
load_keys() click to toggle source
# File lib/glass/api_keys.rb, line 27
def load_keys
  google_api_keys_are_empty? ? (raise APIKeyConfigurationError) : set_client_keys
end
load_yaml_as_auxiliary_method(key) click to toggle source
# File lib/glass/api_keys.rb, line 52
def load_yaml_as_auxiliary_method(key)
  if yaml_config.has_key?(key.to_s)
    google_api_keys[key] = yaml_config[key.to_s] 
  else
    raise YAMLDoesNotHaveTheRightKeysForExtraction
  end
end
preload_yaml_file_keys() click to toggle source
# File lib/glass/api_keys.rb, line 36
def preload_yaml_file_keys
  self.yaml_config = {}
  if yaml_config_hash.has_key?(::Rails.env)
    self.yaml_config = yaml_config_hash[::Rails.env] 
  end
end
set_client_keys() click to toggle source
# File lib/glass/api_keys.rb, line 59
def set_client_keys
  keys_struct = Struct.new(:client_id, :client_secret)
  self.keys = keys_struct.new(google_api_keys[:client_id], google_api_keys[:client_secret])
end
yaml_config_hash() click to toggle source
# File lib/glass/api_keys.rb, line 30
def yaml_config_hash
  return self.yaml_file_contents if self.yaml_file_contents
  self.yaml_file_contents = {}
  self.yaml_file_contents = ::YAML.load(File.read(yaml_file_path)) if File.exists?(yaml_file_path)
  self.yaml_file_contents
end
yaml_file_path() click to toggle source
# File lib/glass/api_keys.rb, line 24
def yaml_file_path
  "#{Dir.pwd}/config/google-api-keys.yml"
end