Class: HashAuth::Config::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/hash-auth/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



48
49
50
51
# File 'lib/hash-auth/config.rb', line 48

def initialize(&block)
  @config = Config.new
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hash-auth/config.rb', line 85

def method_missing(method, *args, &block)
  match = /set_(default_.*)/.match method.to_s
  if match
    default_var_name = match[1]
    @config.instance_variable_set("@#{default_var_name}", args[0])
    if @config.respond_to?("#{default_var_name}".to_sym) == false
      singleton = (class << @config; self end)
      singleton.send :define_method, "#{default_var_name}".to_sym do instance_variable_get("@#{default_var_name}") end
    end
  else
    var_name = method
    @config.instance_variable_set("@#{var_name}", args[0])
    if @config.respond_to?("#{var_name}".to_sym) == false
      singleton = (class << @config; self end)
      singleton.send :define_method, "#{var_name}".to_sym do instance_variable_get("@#{var_name}") end
    end
  end
end

Instance Method Details

#add_client(client) ⇒ Object



62
63
64
# File 'lib/hash-auth/config.rb', line 62

def add_client(client)
  (HashAuth.clients ||= []) << create_client_from_hash_if_valid(client)
end

#add_clients(clients) ⇒ Object

add_clients calls add_client on a list of client hashes

Parameters:

  • clients (Hash)
    • an Array of client hashes



69
70
71
72
73
# File 'lib/hash-auth/config.rb', line 69

def add_clients(clients)
  clients.each do |client| 
    add_client client
  end
end

#buildObject



53
54
55
# File 'lib/hash-auth/config.rb', line 53

def build
  @config
end

#create_client_from_hash_if_valid(client) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/hash-auth/config.rb', line 75

def create_client_from_hash_if_valid(client)
  [:customer_key, :customer_identifier, :valid_domains].each do |required_val|
    raise "Client hash is missing #{required_val}" unless client[required_val]
  end
  client[:strategy] = HashAuth.find_strategy(client[:strategy]) if client[:strategy]
  client[:valid_domains] = [client[:valid_domains]] unless client[:valid_domains].kind_of? Array
  client[:valid_ips] = [client[:valid_ips]] unless client[:valid_ips].kind_of? Array
  HashAuth::Client.new client
end

#set_default_strategy(val) ⇒ Object



57
58
59
60
# File 'lib/hash-auth/config.rb', line 57

def set_default_strategy(val)
  strategy = HashAuth.find_strategy val
  @config.instance_variable_set("@default_strategy", strategy)
end