aladtec 0.2.0 → 0.3.0

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: 1e687432d0c3fdf944414f149f28dbddd226255191cf192240a6f5ba244f9077
4
+ data.tar.gz: a8bd333f2c39f0ed0d88fcabe7127488f80b09f5d349a4118412936c145bdfac
5
5
  SHA512:
6
- metadata.gz: afdf0a35e2d5e5960a4958b41044ef0f5a7eb1c6484ccac93d8786ac7c7b6f2b910f3b8cf52f49e73b7d687de4c86b73522911748d000b3a1d2e767966385203
7
- data.tar.gz: 648d57e16f2ded50b0d62f966a3c59464032cf3d93d6502699b476c99607ccb8639e9d6d45ff8bdef6d79e24605c59edb510fb91c5b22e28d12f65bffd3fba63
6
+ metadata.gz: '011149c9cf0f94c581135fbb1c2f0e1aa2e4ea05fcb97c03e4eb7008c9ffe68b7f80750618a71b7503599e9543483ff6ffc431e19a0360a5e21b93f7ee6305fa'
7
+ data.tar.gz: '0293445a0e145bbd54ca1db4a138feafb3d68ebf2e19559de880988f2afc7fded9a7ca66284090c79795ad3fb0d17f7894a9897e6f9f05cb8b6ad610c8f46730'
@@ -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
+
@@ -1,3 +1,7 @@
1
+ # 0.3.0
2
+ - Updated to JSON api
3
+ - Added http dependency
4
+
1
5
  # 0.2.0
2
6
  - Removed dependencies on happy_mapper and rest_client.
3
7
  - 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
 
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
@@ -1,28 +1,32 @@
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'
15
17
 
16
18
  spec.files = `git ls-files -z`.split("\x0")
17
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
21
+ spec.require_paths = ['lib']
20
22
 
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"
23
+ spec.add_development_dependency 'bundler', '~> 2.0'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'webmock'
26
28
 
27
- spec.add_dependency "multi_xml"
29
+ spec.add_dependency 'dry-configurable'
30
+ spec.add_dependency 'dry-initializer'
31
+ spec.add_dependency 'http'
28
32
  end
@@ -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, 'https://secure.aladtec.com/example/api/'
12
+ setting :user_agent, "Aladtec API Ruby Gem #{Aladtec::VERSION}"
13
+ setting :client_id
14
+ setting :client_secret
7
15
  end
@@ -1,67 +1,70 @@
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.update(args)
17
+ end
12
18
 
19
+ def configure
20
+ yield config
21
+ end
13
22
 
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])
21
- end
23
+ def authenticate
24
+ body = { grant_type: 'client_credentials', client_id: config.client_id,
25
+ client_secret: config.client_secret }
26
+ response = HTTP.post(URI.join(config.endpoint, 'oauth/token'), json: body)
27
+ body = response.parse
28
+ @auth_token = body.fetch('token')
29
+ @auth_expiration = Time.at body.fetch('expires')
30
+ response.status.success?
22
31
  end
23
32
 
24
33
  # Public: Get a list of events for a date or range of dates
25
34
  #
26
35
  # 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).
36
+ # :begin_time - The begin date to return events for (required).
37
+ # :end_time - The end date to return events for (required).
29
38
  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
39
+ bd = options.fetch(:begin_time) do
40
+ raise ArgumentError, 'You must supply a :begin_time option!'
41
+ end
42
+ ed = options.fetch(:end_time) do
43
+ raise ArgumentError, 'You must supply a :end_time option!'
35
44
  end
36
- body = request(:getEvents, bd: bd, ed: ed)
37
- fetch_map(body, "events", "event", Event)
45
+ events = request('events', range_start: format_time(bd),
46
+ range_stop: format_time(ed))
47
+ events.values.flatten.map { |event| Event.new(event) }
38
48
  end
39
49
 
40
50
  # Public: Get a list of members
41
51
  #
42
52
  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"])
53
+ res = request('members', include_attributes: true)
54
+ res.map { |member| Member.new(member) }
52
55
  end
53
56
 
54
57
  # Public: Get a list of schedules
55
58
  #
56
59
  def schedules
57
- body = request(:getSchedules, isp: 1)
58
- fetch_map(body, "schedules", "schedule", Schedule)
60
+ res = request('schedules')
61
+ res.map { |schedule| Schedule.new(schedule) }
59
62
  end
60
63
 
61
64
  # Public: Get list of members scheduled now
62
65
  def scheduled_now(options = {})
63
- body = request(:getScheduledTimeNow, {isp: 1}.merge(options))
64
- fetch_map(body, "schedules", "schedule", Schedule)
66
+ res = request('scheduled-time/members-scheduled-now', options)
67
+ res.map { |schedule| ScheduledNow.new(schedule) }
65
68
  end
66
69
 
67
70
  # Public: Get list of members scheduled in a time range
@@ -71,59 +74,31 @@ module Aladtec
71
74
  # :end_time - The end time to return events for (required).
72
75
  # :sch - Array of schedule ids to fetch
73
76
  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}
77
+ bt = options.fetch(:begin_time) do
78
+ raise ArgumentError, 'You must supply a :begin_time!'
79
+ end
80
+ et = options.fetch(:end_time) do
81
+ raise ArgumentError, 'You must supply an :end_time!'
82
+ end
83
+ # sch = Array(options.fetch(:sch, "all")).join(",")
84
+ scheduled_time = request('scheduled-time', range_start: format_time(bt),
85
+ range_stop: format_time(et))
86
+ scheduled_time.values.flatten.map { |range| Range.new(range) }
94
87
  end
95
- private :auth_params
96
88
 
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'
89
+ private
103
90
 
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)
107
- end
91
+ def request(path, options = {})
92
+ res = HTTP[user_agent: config.user_agent]
93
+ .auth("Bearer #{@auth_token}")
94
+ .get(URI.join(config.endpoint, path), params: options)
95
+ raise Aladtec::Error, res.status.reason unless res.status.success?
108
96
 
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
97
+ res.parse
120
98
  end
121
- private :request
122
99
 
123
- def uri
124
- @uri ||= URI(endpoint)
100
+ def format_time(time)
101
+ (time.is_a?(Time) ? time : Time.parse(time)).strftime('%FT%R')
125
102
  end
126
- private :uri
127
-
128
103
  end
129
104
  end
@@ -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
@@ -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