Class: ShiftPlanning::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/shift_planning/client.rb', line 8

def initialize(config = {})
  raise ArgumentError.new('Missing username') unless config.key? :username
  @username = config[:username]
  raise ArgumentError.new('Missing password') unless config.key? :password
  @password = config[:password]
  raise ArgumentError.new('Missing api key') unless config.key? :key
  @key = config[:key]
  @url = 'http://www.shiftplanning.com/api/'
  @headers = {
    "Content-Type" => "application/x-www-form-urlencoded"
  }
end

Instance Method Details

#authenticateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shift_planning/client.rb', line 25

def authenticate
  body = body_formatter({
    "key" => @key,
    "request" => {
      "module" => "staff.login",
      "method" => "GET",
      "username" => @username,
      "password" => @password
    }
  })
  response = HTTP.with(@headers).post(@url, body)
  @token = JSON.parse(response)["token"]
end

#authenticated?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/shift_planning/client.rb', line 21

def authenticated?
  !@token.nil?
end

#create(api_module, request = {}) ⇒ Object



43
44
45
# File 'lib/shift_planning/client.rb', line 43

def create(api_module, request={})
  request("CREATE", api_module, request)
end

#delete(api_module, request = {}) ⇒ Object



51
52
53
# File 'lib/shift_planning/client.rb', line 51

def delete(api_module, request={})
  request("DELETE", api_module, request)
end

#get(api_module, request = {}) ⇒ Object



39
40
41
# File 'lib/shift_planning/client.rb', line 39

def get(api_module, request={})
  request("GET", api_module, request)
end

#update(api_module, request = {}) ⇒ Object



47
48
49
# File 'lib/shift_planning/client.rb', line 47

def update(api_module, request={})
  request("UPDATE", api_module, request)
end