aladtec 0.2.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5833472c1eeb7a7f359005a98ba355d114fb8223
4
- data.tar.gz: 969f3e879943297a501a15253cece2cefbb7facb
2
+ SHA256:
3
+ metadata.gz: 5fe187974f6bb3afdfee9fa734097e8e9aa644e3d8e6d62eb5c3aeca466e3f84
4
+ data.tar.gz: 31d54f6839c062dba60924565d4d42e65b4521d106dc0bcc614d61889b87cf63
5
5
  SHA512:
6
- metadata.gz: afdf0a35e2d5e5960a4958b41044ef0f5a7eb1c6484ccac93d8786ac7c7b6f2b910f3b8cf52f49e73b7d687de4c86b73522911748d000b3a1d2e767966385203
7
- data.tar.gz: 648d57e16f2ded50b0d62f966a3c59464032cf3d93d6502699b476c99607ccb8639e9d6d45ff8bdef6d79e24605c59edb510fb91c5b22e28d12f65bffd3fba63
6
+ metadata.gz: 3e29cd58cef7d9a02caacc815d6db9fb8b5a4e3279c6b4530d4d7ee6851289b759539881aa2660020c00c9fd0085ba411853cabb442a32a048c52e8ddc084af6
7
+ data.tar.gz: 6eafb4634d7659680d1b99d305b95ed0e8e9ca46c05885c8a145d837018a88286219b1dc6c5572e97591d1b0d4056a2333648eea69403986cd649c096f662e10
data/.travis.yml CHANGED
@@ -1,4 +1,11 @@
1
1
  sudo: false
2
+ dist: bionic
2
3
  language: ruby
4
+ cache: bundler
3
5
  rvm:
4
- - 2.3.1
6
+ - 2.5.3
7
+ - 2.6.3
8
+ before_install:
9
+ - gem update --system
10
+ - gem install bundler
11
+
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+
2
+ # 0.3.1
3
+ - Fix for dry-configurable 0.11 config#update does not return self
4
+
5
+ # 0.3.0
6
+ - Updated to JSON api
7
+ - Added http dependency
8
+
1
9
  # 0.2.0
2
10
  - Removed dependencies on happy_mapper and rest_client.
3
11
  - Removed RangeDenormalizer. ScheduledRanges will no longer include member and position attributes.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in aladtec.gemspec
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Aladtec
2
+ [![Gem Version](https://badge.fury.io/rb/aladtec.svg)](https://badge.fury.io/rb/aladtec)
2
3
  [![Build Status](https://travis-ci.org/travisdahlke/aladtec.svg)](https://travis-ci.org/travisdahlke/aladtec)
3
4
  [![Inline docs](http://inch-ci.org/github/travisdahlke/aladtec.svg?branch=master)](http://inch-ci.org/github/travisdahlke/aladtec)
4
5
 
@@ -23,8 +24,8 @@ Or install it yourself as:
23
24
  Configure the gem with your credentials:
24
25
 
25
26
  Aladtec.configure do |config|
26
- config.acc_key = ENV['ALADTEC_ACC_KEY']
27
- config.acc_id = ENV['ALADTEC_ACC_ID']
27
+ config.client_id = ENV['ALADTEC_CLIENT_ID']
28
+ config.client_secret = ENV['ALADTEC_CLIENT_SECRET']
28
29
  config.endpoint = ENV['ALADTEC_ENDPOINT']
29
30
  end
30
31
 
@@ -35,12 +36,20 @@ Get members
35
36
 
36
37
  Get events
37
38
 
38
- client.events(begin_date: Date.today)
39
+ client.events(begin_time: Time.now, end_time: Time.now + 60 * 60 * 24)
39
40
 
40
41
  Get schedules
41
42
 
42
43
  client.schedules
43
44
 
45
+ Get scheduled time ranges
46
+
47
+ client.scheduled_range(begin_time: Time.new(2019,10,1), end_time: Time.new(2019,10,31))
48
+
49
+ Get members scheduled now
50
+
51
+ client.scheduled_now
52
+
44
53
  Refer to the [documentation](http://www.rubydoc.info/github/travisdahlke/aladtec) for more detailed usage.
45
54
 
46
55
  ## Contributing
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/aladtec.gemspec CHANGED
@@ -1,28 +1,34 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'aladtec/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "aladtec"
8
+ spec.name = 'aladtec'
8
9
  spec.version = Aladtec::VERSION
9
- spec.authors = ["Travis Dahlke"]
10
- spec.email = ["dahlke.travis@gmail.com"]
11
- spec.summary = %q{Client library for the Aladtec API}
12
- spec.description = %q{Retrieve schedules and events from the Aladtec API. Works with EMS Manager, Fire Manager, Zanager.}
13
- spec.homepage = ""
14
- spec.license = "MIT"
10
+ spec.authors = ['Travis Dahlke']
11
+ spec.email = ['dahlke.travis@gmail.com']
12
+ spec.summary = 'Client library for the Aladtec API'
13
+ spec.description = %(Retrieve schedules and events from the Aladtec API.
14
+ Works with EMS Manager, Fire Manager, Zanager. )
15
+ spec.homepage = 'https://github.com/travisdahlke/aladtec'
16
+ spec.license = 'MIT'
17
+
18
+ spec.required_ruby_version = '> 2.5'
15
19
 
16
20
  spec.files = `git ls-files -z`.split("\x0")
17
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
23
+ spec.require_paths = ['lib']
20
24
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "webmock"
25
- spec.add_development_dependency "pry"
25
+ spec.add_development_dependency 'bundler', '~> 2.0'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'webmock'
26
30
 
27
- spec.add_dependency "multi_xml"
31
+ spec.add_dependency 'dry-configurable', '~> 0.11', '>= 0.11.4'
32
+ spec.add_dependency 'dry-initializer'
33
+ spec.add_dependency 'http'
28
34
  end
@@ -1,67 +1,75 @@
1
- require 'net/http'
2
- require 'multi_xml'
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
3
4
  require 'aladtec/event'
4
5
  require 'aladtec/member'
5
- require 'aladtec/authentication'
6
6
  require 'aladtec/range'
7
7
  require 'aladtec/schedule'
8
+ require 'aladtec/scheduled_now'
8
9
  require 'aladtec/exceptions'
9
10
 
10
11
  module Aladtec
12
+ # Aladtec API Client
11
13
  class Client
14
+ attr_reader :config
15
+ def initialize(args = {})
16
+ @config = Aladtec.config.dup.tap do |c|
17
+ c.update(args)
18
+ end
19
+ end
12
20
 
21
+ def configure
22
+ yield config
23
+ end
13
24
 
14
- attr_accessor *Configuration::VALID_CONFIG_KEYS
15
-
16
- def initialize(options={})
17
- merged_options = Aladtec.options.merge(options)
18
-
19
- Configuration::VALID_CONFIG_KEYS.each do |key|
20
- public_send("#{key}=", merged_options[key])
25
+ def authenticate
26
+ if config.client_id.nil? || config.client_secret.nil?
27
+ raise Aladtec::Error, 'client_id and client_secret are required'
21
28
  end
29
+ body = { grant_type: 'client_credentials', client_id: config.client_id,
30
+ client_secret: config.client_secret }
31
+ response = HTTP.post(URI.join(config.endpoint, 'oauth/token'), json: body)
32
+ body = response.parse
33
+ @auth_token = body.fetch('token')
34
+ @auth_expiration = Time.at body.fetch('expires')
35
+ response.status.success?
22
36
  end
23
37
 
24
38
  # Public: Get a list of events for a date or range of dates
25
39
  #
26
40
  # options - The Hash options used to refine the selection (default: {}):
27
- # :begin_date - The begin date to return events for (required).
28
- # :end_date - The end date to return events for (optional).
41
+ # :begin_time - The begin date to return events for (required).
42
+ # :end_time - The end date to return events for (required).
29
43
  def events(options = {})
30
- bd = options.fetch(:begin_date) { raise ArgumentError, "You must supply a :begin_date option!"}
31
- ed = options.fetch(:end_date, nil)
32
- bd = bd.is_a?(Date) ? bd.iso8601 : Date.parse(bd).iso8601
33
- if ed
34
- ed = ed.is_a?(Date) ? ed.iso8601 : Date.parse(ed).iso8601
44
+ bd = options.fetch(:begin_time) do
45
+ raise ArgumentError, 'You must supply a :begin_time option!'
46
+ end
47
+ ed = options.fetch(:end_time) do
48
+ raise ArgumentError, 'You must supply a :end_time option!'
35
49
  end
36
- body = request(:getEvents, bd: bd, ed: ed)
37
- fetch_map(body, "events", "event", Event)
50
+ events = request('events', range_start: format_time(bd),
51
+ range_stop: format_time(ed))
52
+ events.values.flatten.map { |event| Event.new(event) }
38
53
  end
39
54
 
40
55
  # Public: Get a list of members
41
56
  #
42
57
  def members
43
- body = request(:getMembers, ia: 'all')
44
- fetch_map(body, "members", "member", Member)
45
- end
46
-
47
- # Public: Authenticate member
48
- #
49
- def auth(username, password)
50
- body = request(:authenticateMember, memun: username, mempw: password)
51
- Authentication.new(body["results"]["authentication"])
58
+ res = request('members', include_attributes: true)
59
+ res.map { |member| Member.new(member) }
52
60
  end
53
61
 
54
62
  # Public: Get a list of schedules
55
63
  #
56
64
  def schedules
57
- body = request(:getSchedules, isp: 1)
58
- fetch_map(body, "schedules", "schedule", Schedule)
65
+ res = request('schedules')
66
+ res.map { |schedule| Schedule.new(schedule) }
59
67
  end
60
68
 
61
69
  # Public: Get list of members scheduled now
62
70
  def scheduled_now(options = {})
63
- body = request(:getScheduledTimeNow, {isp: 1}.merge(options))
64
- fetch_map(body, "schedules", "schedule", Schedule)
71
+ res = request('scheduled-time/members-scheduled-now', options)
72
+ res.map { |schedule| ScheduledNow.new(schedule) }
65
73
  end
66
74
 
67
75
  # Public: Get list of members scheduled in a time range
@@ -69,61 +77,38 @@ module Aladtec
69
77
  # options - The Hash options used to refine the selection (default: {}):
70
78
  # :begin_time - The begin time to return events for (required).
71
79
  # :end_time - The end time to return events for (required).
72
- # :sch - Array of schedule ids to fetch
73
80
  def scheduled_range(options = {})
74
- bt = options.fetch(:begin_time) { raise ArgumentError, "You must supply a :begin_time!"}
75
- et = options.fetch(:end_time) { raise ArgumentError, "You must supply an :end_time!"}
76
- sch = Array(options.fetch(:sch, "all")).join(",")
77
- bt = bt.is_a?(Time) ? bt.clone.utc.iso8601 : Time.parse(bt).utc.iso8601
78
- et = et.is_a?(Time) ? et.clone.utc.iso8601 : Time.parse(et).utc.iso8601
79
- body = request(:getScheduledTimeRanges, bt: bt, et: et, isp: 1, sch: sch)
80
- fetch_map(body, "ranges", "range", Range)
81
- end
82
-
83
- def fetch_map(body, collection, key, klass)
84
- results = body["results"][collection][key] if body["results"][collection]
85
- return [] unless results
86
- # Array.wrap
87
- results = results.respond_to?(:to_ary) ? results.to_ary : [results]
88
- results.map{|r| klass.new(r)}
89
- end
90
- private :fetch_map
91
-
92
- def auth_params
93
- {accid: acc_id, acckey: acc_key}
81
+ bt = options.fetch(:begin_time) do
82
+ raise ArgumentError, 'You must supply a :begin_time!'
83
+ end
84
+ et = options.fetch(:end_time) do
85
+ raise ArgumentError, 'You must supply an :end_time!'
86
+ end
87
+ scheduled_time = request('scheduled-time', range_start: format_time(bt),
88
+ range_stop: format_time(et))
89
+ scheduled_time.values.flatten.map { |range| Range.new(range) }
94
90
  end
95
- private :auth_params
96
91
 
97
- def request(cmd, options = {})
98
- post_params = options.merge(cmd: cmd)
99
- req = Net::HTTP::Post.new(uri)
100
- req.set_form_data(auth_params.merge(post_params))
101
- req['User-Agent'] = user_agent
102
- req['Accept'] = 'application/xml'
92
+ private
103
93
 
104
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
105
- http.use_ssl = true
106
- http.request(req)
94
+ def request(path, options = {})
95
+ if auth_expired? && !authenticate
96
+ raise Aladtec::Error, 'authentication failed'
107
97
  end
98
+ res = HTTP[user_agent: config.user_agent]
99
+ .auth("Bearer #{@auth_token}")
100
+ .get(URI.join(config.endpoint, path), params: options)
101
+ raise Aladtec::Error, res.status.reason unless res.status.success?
108
102
 
109
- case res
110
- when Net::HTTPSuccess, Net::HTTPRedirection
111
- body = MultiXml.parse(res.body)
112
- if body["results"]["errors"]
113
- raise Aladtec::Error, body["results"]["errors"]["error"]["__content__"]
114
- else
115
- return body
116
- end
117
- else
118
- raise Aladtec::Error, res.msg
119
- end
103
+ res.parse
120
104
  end
121
- private :request
122
105
 
123
- def uri
124
- @uri ||= URI(endpoint)
106
+ def format_time(time)
107
+ (time.is_a?(Time) ? time : Time.parse(time)).strftime('%FT%R')
125
108
  end
126
- private :uri
127
109
 
110
+ def auth_expired?
111
+ !@auth_token || !@auth_expiration || Time.now >= @auth_expiration
112
+ end
128
113
  end
129
114
  end
data/lib/aladtec/event.rb CHANGED
@@ -1,13 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
4
+
1
5
  module Aladtec
6
+ # Event
2
7
  class Event
3
- attr_accessor :title, :description, :location, :begin, :end
4
-
5
- def initialize(args = {})
6
- @title = args["title"]
7
- @description = args["description"]
8
- @location = args["location"]
9
- @begin = Time.parse(args["begin"]) if args["begin"]
10
- @end = Time.parse(args["end"]) if args["end"]
8
+ extend Dry::Initializer
9
+
10
+ option :event_id, as: :id
11
+ option :title
12
+ option :description
13
+ option :location
14
+ option :start_datetime, Time.method(:parse), as: :starts_at
15
+ option :stop_datetime, Time.method(:parse), as: :ends_at, optional: true
16
+
17
+ def self.new(params)
18
+ super **params.transform_keys(&:to_sym)
11
19
  end
12
20
  end
13
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aladtec
2
4
  class Error < StandardError; end
3
5
  end
@@ -1,10 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
4
+
1
5
  module Aladtec
6
+ # Member
2
7
  class Member
3
- attr_accessor :id, :name
8
+ extend Dry::Initializer
9
+
10
+ option :member_id, as: :id
11
+ option :name
12
+ option :is_active
13
+ option :attributes, [] do
14
+ option :attribute_id, as: :id
15
+ option :value
16
+ end
4
17
 
5
- def initialize(args = {})
6
- @id = args["id"].to_i
7
- @name = args["name"]
18
+ def self.new(params)
19
+ super **params.transform_keys(&:to_sym)
8
20
  end
9
21
  end
10
22
  end
@@ -1,13 +1,18 @@
1
- require 'aladtec/member'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
2
4
 
3
5
  module Aladtec
6
+ # Position
4
7
  class Position
5
- attr_accessor :id, :name, :member
8
+ extend Dry::Initializer
9
+
10
+ option :position_id, as: :id
11
+ option :label
12
+ option :member_id, optional: true
6
13
 
7
- def initialize(args = {})
8
- @id = args["id"].to_i
9
- @name = args["name"]
10
- @member = Member.new(args["member"]) if args["member"]
14
+ def self.new(params)
15
+ super **params.transform_keys(&:to_sym)
11
16
  end
12
17
  end
13
18
  end
data/lib/aladtec/range.rb CHANGED
@@ -1,18 +1,22 @@
1
- require 'aladtec/schedule'
2
- require 'aladtec/position'
3
- require 'aladtec/member'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
4
4
 
5
5
  module Aladtec
6
+ # Range
6
7
  class Range
7
- attr_accessor :schedule, :position, :member, :begin, :end
8
+ extend Dry::Initializer
8
9
 
9
- def initialize(args = {})
10
- @member = Member.new(args["member"])
11
- @position = Position.new(args["position"])
12
- @schedule = Schedule.new(args["schedule"])
13
- @begin = Time.parse(args["begin"]) if args["begin"]
14
- @end = Time.parse(args["end"]) if args["end"]
15
- end
10
+ option :schedule_id
11
+ option :position_id
12
+ option :member_id
13
+ option :start_datetime, Time.method(:parse), as: :starts_at
14
+ option :stop_datetime, Time.method(:parse), as: :ends_at
15
+ option :extends_before
16
+ option :extends_after
16
17
 
18
+ def self.new(params)
19
+ super **params.transform_keys(&:to_sym)
20
+ end
17
21
  end
18
22
  end
@@ -1,22 +1,21 @@
1
- require 'aladtec/position'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
2
4
 
3
5
  module Aladtec
6
+ # Schedule
4
7
  class Schedule
5
- attr_accessor :id, :name, :positions
8
+ extend Dry::Initializer
6
9
 
7
- def initialize(args = {})
8
- @id = args["id"].to_i
9
- @name = args["name"]
10
- @positions = parse_positions(args["positions"]["position"]) if args["positions"]
10
+ option :schedule_id, as: :id
11
+ option :name
12
+ option :positions, [] do
13
+ option :position_id, proc(&:to_i), as: :id
14
+ option :label
11
15
  end
12
16
 
13
- private
14
-
15
- def parse_positions(positions)
16
- return [] unless positions
17
- return [Position.new(positions)] if positions.include? "id"
18
- positions.map{|p| Position.new(p)}
17
+ def self.new(params)
18
+ super **params.transform_keys(&:to_sym)
19
19
  end
20
-
21
20
  end
22
21
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
4
+
5
+ module Aladtec
6
+ # ScheduledNow
7
+ class ScheduledNow
8
+ extend Dry::Initializer
9
+
10
+ option :schedule_id, as: :id
11
+ option :positions, [] do
12
+ option :position_id, proc(&:to_i), as: :id
13
+ option :member_id, proc(&:to_i)
14
+ end
15
+
16
+ def self.new(params)
17
+ super **params.transform_keys(&:to_sym)
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aladtec
2
- VERSION = "0.2.0"
4
+ VERSION = '0.3.2'
3
5
  end
data/lib/aladtec.rb CHANGED
@@ -1,7 +1,15 @@
1
- require "aladtec/version"
2
- require "aladtec/configuration"
3
- require "aladtec/client"
1
+ # frozen_string_literal: true
4
2
 
3
+ require 'dry/configurable'
4
+ require 'aladtec/version'
5
+ require 'aladtec/client'
6
+
7
+ # Aladtec API Wrapper
5
8
  module Aladtec
6
- extend Configuration
9
+ extend Dry::Configurable
10
+
11
+ setting :endpoint, default: 'https://secure.aladtec.com/example/api/'
12
+ setting :user_agent, default: "Aladtec API Ruby Gem #{Aladtec::VERSION}"
13
+ setting :client_id
14
+ setting :client_secret
7
15
  end
@@ -1,7 +1,24 @@
1
- require_relative File.join('..','spec_helper')
1
+ # frozen_string_literal: true
2
+
3
+ require_relative File.join('..', 'spec_helper')
2
4
 
3
5
  describe Aladtec do
4
6
  it 'should have a version' do
5
7
  expect(Aladtec::VERSION).not_to be_nil
6
8
  end
9
+
10
+ describe '.configure' do
11
+ Aladtec.settings.each do |key|
12
+ it "should set the #{key}" do
13
+ Aladtec.configure do |config|
14
+ config.public_send("#{key}=", key)
15
+ end
16
+ expect(Aladtec.config.public_send(key)).to eq(key)
17
+ end
18
+ end
19
+
20
+ after(:each) do
21
+ Aladtec.reset_config
22
+ end
23
+ end
7
24
  end