Class: Aladtec::Client
- Inherits:
-
Object
- Object
- Aladtec::Client
- Defined in:
- lib/aladtec/client.rb
Overview
Aladtec API Client
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #configure {|config| ... } ⇒ Object
-
#events(options = {}) ⇒ Object
Public: Get a list of events for a date or range of dates.
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
-
#members ⇒ Object
Public: Get a list of members.
-
#scheduled_now(options = {}) ⇒ Object
Public: Get list of members scheduled now.
-
#scheduled_range(options = {}) ⇒ Object
Public: Get list of members scheduled in a time range.
-
#schedules ⇒ Object
Public: Get a list of schedules.
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 |
# File 'lib/aladtec/client.rb', line 15 def initialize(args = {}) @config = Aladtec.config.dup.tap do |c| c.update(args) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/aladtec/client.rb', line 14 def config @config end |
Instance Method Details
#authenticate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/aladtec/client.rb', line 25 def authenticate if config.client_id.nil? || config.client_secret.nil? raise Aladtec::Error, 'client_id and client_secret are required' end body = { grant_type: 'client_credentials', client_id: config.client_id, client_secret: config.client_secret } response = HTTP.post(URI.join(config.endpoint, 'oauth/token'), json: body) body = response.parse @auth_token = body.fetch('token') @auth_expiration = Time.at body.fetch('expires') response.status.success? end |
#configure {|config| ... } ⇒ Object
21 22 23 |
# File 'lib/aladtec/client.rb', line 21 def configure yield config end |
#events(options = {}) ⇒ Object
Public: Get a list of events for a date or range of dates
options - The Hash options used to refine the selection (default: {}): :begin_time - The begin date to return events for (required). :end_time - The end date to return events for (required).
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aladtec/client.rb', line 43 def events( = {}) bd = .fetch(:begin_time) do raise ArgumentError, 'You must supply a :begin_time option!' end ed = .fetch(:end_time) do raise ArgumentError, 'You must supply a :end_time option!' end events = request('events', range_start: format_time(bd), range_stop: format_time(ed)) events.values.flatten.map { |event| Event.new(event) } end |
#members ⇒ Object
Public: Get a list of members
57 58 59 60 |
# File 'lib/aladtec/client.rb', line 57 def members res = request('members', include_attributes: true) res.map { |member| Member.new(member) } end |
#scheduled_now(options = {}) ⇒ Object
Public: Get list of members scheduled now
70 71 72 73 |
# File 'lib/aladtec/client.rb', line 70 def scheduled_now( = {}) res = request('scheduled-time/members-scheduled-now', ) res.map { |schedule| ScheduledNow.new(schedule) } end |
#scheduled_range(options = {}) ⇒ Object
Public: Get list of members scheduled in a time range
options - The Hash options used to refine the selection (default: {}): :begin_time - The begin time to return events for (required). :end_time - The end time to return events for (required).
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aladtec/client.rb', line 80 def scheduled_range( = {}) bt = .fetch(:begin_time) do raise ArgumentError, 'You must supply a :begin_time!' end et = .fetch(:end_time) do raise ArgumentError, 'You must supply an :end_time!' end scheduled_time = request('scheduled-time', range_start: format_time(bt), range_stop: format_time(et)) scheduled_time.values.flatten.map { |range| Range.new(range) } end |
#schedules ⇒ Object
Public: Get a list of schedules
64 65 66 67 |
# File 'lib/aladtec/client.rb', line 64 def schedules res = request('schedules') res.map { |schedule| Schedule.new(schedule) } end |