Class: Bookafy::Appointment

Inherits:
BaseService show all
Defined in:
lib/bookafy/appointment.rb

Constant Summary

Constants inherited from BaseService

BaseService::API_VERSION

Instance Method Summary collapse

Methods inherited from BaseService

#access_token, #bookafy_api_url, #get

Constructor Details

#initializeAppointment

Returns a new instance of Appointment.



4
5
6
7
# File 'lib/bookafy/appointment.rb', line 4

def initialize
  super
  @page = 1
end

Instance Method Details

#all(query_params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bookafy/appointment.rb', line 13

def all(query_params = {})
  query_params[:page] = @page
  response = get(uri, query_params)
  unless response.code == 200
    return []
  end

  response_json = JSON.parse(response.body)['response']
  appointments_json = response_json['appointments']
  appointments = appointments_json.map do |data|
    Bookafy::Model::Appointment.new(data)
  end

  if response_json['remaining'] > 0
    @page = @page + 1
    appointments += all(query_params)
  end

  appointments
rescue => e
  puts "Error at fetching appointments: #{e.message}"
  return []
end

#uriObject



9
10
11
# File 'lib/bookafy/appointment.rb', line 9

def uri
  'appointments'
end