Class: Restafari::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/restafari/configuration.rb

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/restafari/configuration.rb', line 8

def initialize

  @defaults = {
    base_url: "",
    http_method: :post,
    scheme: :http,
    before_request: [],
    after_response: []
  }

  define_singleton_method :defaults do
    @defaults
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

def method_missing(name, *args)
  name_stripped = name.to_s.sub(/[\?=]$/, '').to_sym
  return super unless @defaults.key?(name_stripped)

  if name.to_s.end_with?('=')
    @defaults[name_stripped] = args.first
  else
    @defaults[name_stripped]
  end
end

Instance Method Details

#after_response(&block) ⇒ Object



42
43
44
# File 'lib/restafari/configuration.rb', line 42

def after_response(&block)
  @defaults[:after_response] << block if block_given?
end

#before_request(&block) ⇒ Object



38
39
40
# File 'lib/restafari/configuration.rb', line 38

def before_request(&block)
  @defaults[:before_request] << block if block_given?
end

#run_after_response_hook(*params) ⇒ Object



52
53
54
55
56
# File 'lib/restafari/configuration.rb', line 52

def run_after_response_hook(*params)
  @defaults[:after_response].each do |hook|
    hook.call(*params)
  end
end

#run_before_request_hook(*params) ⇒ Object



46
47
48
49
50
# File 'lib/restafari/configuration.rb', line 46

def run_before_request_hook(*params)
  @defaults[:before_request].each do |hook|
    hook.call(*params)
  end
end

#urlObject



34
35
36
# File 'lib/restafari/configuration.rb', line 34

def url
  "#{Restafari.config.scheme}://#{Restafari::config.base_url}"
end