Class: Recras::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/recras/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Client

Initializer to transform a Hash into an Client object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/recras/client.rb', line 15

def initialize(args=nil)
  required_args = [] # [:username, :password]
  for arg in required_args
    if args.nil? || args[arg].nil?
      raise RecrasError.new(self), "Insufficient login credentials. Please provide @username, @password and @host"
    end
  end

  return if args.nil?
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/recras/client.rb', line 8

def host
  @host
end

#passwordString



7
8
9
# File 'lib/recras/client.rb', line 7

def password
  @password
end

#usernameObject

Note:

The is a required parameter.



5
6
7
# File 'lib/recras/client.rb', line 5

def username
  @username
end

Instance Method Details

#combination(id) ⇒ Object

find a specific combination



55
56
57
58
# File 'lib/recras/client.rb', line 55

def combination(id)
  json = make_request("arrangementen/#{id}")
  return Recras.parse_json(json: json, endpoint: "arrangementen", client: self)
end

#combinationsObject

returns an array of available combinations



73
74
75
76
77
78
79
80
# File 'lib/recras/client.rb', line 73

def combinations
  result = make_request("arrangementen")
  a = []
  for json in result
    a << Recras.parse_json(json: json, endpoint: "arrangementen", client: self)
  end
  return a
end

#make_request(endpoint, body: {}, http_method: :get) ⇒ Object

communicates with the server and returns either:

  • an object (Person, Booking)

  • an error

  • a json object



33
34
35
# File 'lib/recras/client.rb', line 33

def make_request(endpoint, body: {}, http_method: :get)
  Recras.make_request(endpoint, body: body, http_method: http_method, client: self)
end

#meObject

returns a Me object



49
50
51
52
# File 'lib/recras/client.rb', line 49

def me
  json = make_request("personeel/me")
  Recras.parse_json(json: json, endpoint: "personeel")
end

#payment_methodsObject

returns an array of available combinations



62
63
64
65
66
67
68
69
# File 'lib/recras/client.rb', line 62

def payment_methods
  result = make_request("betaalmethoden")
  a = []
  for json in result
    a << Recras.parse_json(json: json, endpoint: "betaalmethoden")
  end
  return a
end

#validObject Also known as: valid?

returns true if credentials are valid, false otherwise



38
39
40
41
42
43
44
45
# File 'lib/recras/client.rb', line 38

def valid
  begin
    me
    return true
  rescue RecrasError
    return false
  end
end