Class: Coach4rb::Proxy::Base

Inherits:
Object
  • Object
show all
Includes:
Mixin::BasicAuth
Defined in:
lib/coach4rb/proxy.rb

Overview

This class provides an interface that all subclasses must implement.

Direct Known Subclasses

Access, InvalidAccess

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::BasicAuth

included

Constructor Details

#initialize(coach) ⇒ Base

Base access proxy which is extended by the subclasses to provide more specific access to the coach client.

Parameters:



19
20
21
# File 'lib/coach4rb/proxy.rb', line 19

def initialize(coach)
  @coach = coach
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegates all messages send to this proxy to the coach client



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/coach4rb/proxy.rb', line 55

def method_missing(meth, *args, &block)
  params = *args

  # handle options hash
  if params.last.is_a?(Hash) # if last param is a hash then it's a option hash.
    params[-1] = params.last.merge(proxy_options)
  else # otherwise add a option hash with the given proxy options.
    params << proxy_options
  end

  # check if coach responds to the message meth / or method meth
  if coach.respond_to?(meth)
    begin # try to pass the options hash
      coach.send meth, *params, &block
    rescue # otherwise do it without options hash
      coach.send meth, *args, &block
    end
  else
    raise 'Error: Method missing in coach!'
  end
end

Instance Attribute Details

#coachObject (readonly)

Returns the value of attribute coach.



13
14
15
# File 'lib/coach4rb/proxy.rb', line 13

def coach
  @coach
end

Instance Method Details

#passwordObject



29
30
31
# File 'lib/coach4rb/proxy.rb', line 29

def password
  raise 'Not implemented!'
end

#proxy_optionsHash

Always returns an empty hash.

Returns:

  • (Hash)


36
37
38
# File 'lib/coach4rb/proxy.rb', line 36

def proxy_options
  {}
end

#usernameObject



24
25
26
# File 'lib/coach4rb/proxy.rb', line 24

def username
  raise 'Not implemented!'
end

#valid?Boolean

Tests if the provided user credentials are valid.

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/coach4rb/proxy.rb', line 44

def valid?
  begin
    coach.authenticate(username, password)
  rescue
    false
  end
end