Class: ShiftPlanning::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Connection

Returns a new instance of Connection.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/shift_planning/connection.rb', line 4

def initialize(key)
  @key = key
  @token = nil
  @http = Faraday.new(url: "http://www.shiftplanning.com") do |conn|
    conn.request :url_encoded

    conn.response :json
    conn.response :logger

    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



2
3
4
# File 'lib/shift_planning/connection.rb', line 2

def http
  @http
end

Instance Method Details

#call(method, command, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shift_planning/connection.rb', line 17

def call(method, command, params = {})
  request_params = {
    request: params.merge(
      method: method,
      module: command
    ),
    output: 'json'
  }

  response = http.post('/api/', data: JSON::generate(signed_params(request_params))).body
  ShiftPlanning::ApiError.parse(response['status'])

  parse_data response do |data|
    @token = data['token'] if data.has_key?('token')
  end
end