flexipass 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c9c91b169d3c20a41871c20a3983a969974ff3d25097f518542837a49d88eada
4
+ data.tar.gz: ec6209b051dd70dfc730bdcec81e877d64c339c1d317b8b05f61be2263e9fc64
5
+ SHA512:
6
+ metadata.gz: 25431ca3666a7063cedfd9c50e665a6c837a6e6ad4e0d1043c58acf6a025a19ff47d51f5f665d0a87c8b1957f347023bcd0d1400bfeec04ccbe355aa602debc9
7
+ data.tar.gz: 6f02dfe29466cd84129ca3548cd2447cc98b1fb5456a1f90464fe21a597ba4786da585cf1630b6585b5902cf993f563fff829c78008a0f29d4865dd119e0fec5
data/.byebug_history ADDED
@@ -0,0 +1,14 @@
1
+ c
2
+ DateTime.parse("hello #{time}")
3
+ DateTime.parse("#{date} #{time}")
4
+ date
5
+ DateTime.parse("#{date} #{time}")
6
+ c
7
+ date
8
+ c
9
+ label
10
+ time
11
+ date
12
+ c
13
+ validate_date_time(params[:Checkin_date], params[:Checkin_time], "Check-in")
14
+ params[:Checkin_date]
@@ -0,0 +1,3 @@
1
+ module Flexipass
2
+ class ApiError < StandardError; end
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'faraday'
2
+ require 'base64'
3
+
4
+ module Flexipass
5
+ class Client
6
+ def initialize
7
+ Flexipass.configuration.validate!
8
+ @conn = Faraday.new(url: Flexipass.configuration.server_address) do |faraday|
9
+ if Flexipass.configuration.enable_logging
10
+ # Create a logger instance
11
+ logger = Logger.new(STDOUT)
12
+ logger.level = Logger::DEBUG
13
+ faraday.response :logger, logger, bodies: true # Set bodies: true to log request and response bodies
14
+ end
15
+ faraday.request :url_encoded
16
+ faraday.adapter Faraday.default_adapter
17
+ faraday.headers['Content-Type'] = 'application/json'
18
+ faraday.headers['Authorization'] = basic_auth_header
19
+ end
20
+ end
21
+
22
+ def mobile_key
23
+ @mobile_key ||= MobileKey.new(self)
24
+ end
25
+
26
+ def door
27
+ @door ||= Door.new(self)
28
+ end
29
+
30
+ def company
31
+ @company ||= Company.new(self)
32
+ end
33
+
34
+ def send_request(method, endpoint, params = {})
35
+ response = @conn.send(method.downcase) do |req|
36
+ req.url endpoint
37
+ req.params['companyToken'] = Flexipass.configuration.company_token
38
+ req.body = params.to_json if ['POST', 'PUT'].include?(method.upcase)
39
+ end
40
+
41
+ handle_response(response)
42
+ end
43
+
44
+ private
45
+
46
+ def basic_auth_header
47
+ credentials = "#{Flexipass.configuration.username}:#{Flexipass.configuration.password}"
48
+ encoded_credentials = Base64.strict_encode64(credentials)
49
+ "Basic #{encoded_credentials}"
50
+ end
51
+
52
+ def handle_response(response)
53
+ case response.status
54
+ when 200..299
55
+ JSON.parse(response.body)
56
+ else
57
+ raise ApiError.new("API request failed with status #{response.status}: #{response.body}")
58
+ end
59
+ rescue JSON::ParserError
60
+ response.body
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ module Flexipass
2
+ class Company
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def details
8
+ @client.send_request('GET', '/Flexipass/rest/webapi/getCompanyDetails')
9
+ end
10
+
11
+ def permissions
12
+ @client.send_request('GET', '/Flexipass/rest/webapi/getPermissionList')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ # lib/flexipass/configuration.rb
2
+
3
+ module Flexipass
4
+ class Configuration
5
+ attr_accessor :username, :password, :company_token, :environment, :enable_logging
6
+ attr_reader :server_address
7
+
8
+ DEV_SERVER = 'https://dev.flexipass.it'
9
+ PROD_SERVER = 'https://flexipass.it'
10
+
11
+ def initialize
12
+ @environment = :development
13
+ set_server_address
14
+ end
15
+
16
+ def environment=(env)
17
+ @environment = env.to_sym
18
+ set_server_address
19
+ end
20
+
21
+ def validate!
22
+ raise ConfigurationError, 'Username must be set' if @username.nil?
23
+ raise ConfigurationError, 'Password must be set' if @password.nil?
24
+ raise ConfigurationError, 'Company token must be set' if @company_token.nil?
25
+ raise ConfigurationError, "Invalid environment: #{@environment}. Must be :development or :production" unless [:development, :production].include?(@environment)
26
+ end
27
+
28
+ private
29
+
30
+ def set_server_address
31
+ @server_address = if @environment == :development
32
+ DEV_SERVER
33
+ elsif @environment == :production
34
+ PROD_SERVER
35
+ end
36
+ end
37
+
38
+
39
+ end
40
+
41
+ class ConfigurationError < StandardError; end
42
+ end
@@ -0,0 +1,11 @@
1
+ module Flexipass
2
+ class Door
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def list
8
+ @client.send_request('GET', '/Flexipass/rest/webapi/getDoorList')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,84 @@
1
+ module Flexipass
2
+ class MobileKey
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def create(params)
8
+ validate_params(params)
9
+ @client.send_request('POST', '/Flexipass/rest/webapi/createMobileKey', params)
10
+ end
11
+
12
+ def update(mobile_key_id, params)
13
+ validate_params(params)
14
+ @client.send_request('PUT', "/Flexipass/rest/webapi/updateMobileKey?mkID=#{mobile_key_id}", params)
15
+ end
16
+
17
+ def delete(mobile_key_id)
18
+ @client.send_request('DELETE', "/Flexipass/rest/webapi/deleteMobileKey?mkID=#{mobile_key_id}")
19
+ end
20
+
21
+ def list(door, params)
22
+ validate_list_params(params)
23
+ params[:Door] = door
24
+ @client.send_request('POST', "/Flexipass/rest/webapi/getMobileKeyList", params)
25
+ end
26
+
27
+ def details(mobile_key_id)
28
+ @client.send_request('GET', "/Flexipass/rest/webapi/getMobileKeyDetails?mkID=#{mobile_key_id}")
29
+ end
30
+
31
+ def send_mail(mobile_key_id)
32
+ @client.send_request('GET', "/Flexipass/rest/webapi/sendMail?mkID=#{mobile_key_id}")
33
+ end
34
+
35
+ def send_sms(mobile_key_id)
36
+ @client.send_request('GET', "/Flexipass/rest/webapi/sendSMS?mkID=#{mobile_key_id}")
37
+ end
38
+
39
+ private
40
+
41
+ def validate_params(params)
42
+ required_params = [:Name, :Surname, :Mail, :Door, :Checkin_date, :Checkin_time, :Checkout_date, :Checkout_time, :MobileKeyType]
43
+ missing_params = required_params - params.keys.map(&:to_sym)
44
+ raise ArgumentError, "Missing required parameters: #{missing_params.join(', ')}" if missing_params.any?
45
+
46
+ validate_date_time(params[:Checkin_date], params[:Checkin_time], "Check-in")
47
+ validate_date_time(params[:Checkout_date], params[:Checkout_time], "Check-out")
48
+
49
+ validate_mobile_key_type(params[:MobileKeyType])
50
+ end
51
+
52
+ def validate_date_time(date, time, label)
53
+ date_format = /^(\d{4}-\d{2}-\d{2})$/
54
+ time_format = /^(\d{2}:\d{2}:\d{2})$/
55
+
56
+ unless date.match?(date_format) && time.match?(time_format)
57
+ raise ArgumentError, "Invalid #{label} date or time format"
58
+ end
59
+ begin
60
+ DateTime.parse("#{date} #{time}")
61
+ rescue ArgumentError
62
+ raise ArgumentError, "Invalid #{label} date or time format"
63
+ end
64
+ end
65
+
66
+ def validate_list_params(params)
67
+ date_time_format = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})$/
68
+
69
+ unless params[:StartDate].match?(date_time_format)
70
+ raise ArgumentError, "Invalid StartDate format"
71
+ end
72
+
73
+ unless params[:EndDate].match?(date_time_format)
74
+ raise ArgumentError, "Invalid EndDate format"
75
+ end
76
+ end
77
+
78
+ def validate_mobile_key_type(type)
79
+ unless [0, 1].include?(type)
80
+ raise ArgumentError, "Invalid MobileKeyType. Must be 0 or 1"
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flexipass
4
+ VERSION = "0.1.1"
5
+ end
data/lib/flexipass.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'flexipass/version'
4
+ require 'flexipass/configuration'
5
+ require 'flexipass/client'
6
+ require 'flexipass/mobile_key'
7
+ require 'flexipass/door'
8
+ require 'flexipass/company'
9
+ require 'flexipass/api_error'
10
+
11
+ module Flexipass
12
+ class Error < StandardError; end
13
+
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configure
19
+ self.configuration ||= Configuration.new
20
+ yield(configuration)
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexipass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Kellen Kyros
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.7.11
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.7'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.11
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.7.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.7'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.13'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '3.13'
67
+ - !ruby/object:Gem::Dependency
68
+ name: webmock
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '3.23'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.23.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.23'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.23.1
87
+ - !ruby/object:Gem::Dependency
88
+ name: debug
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.8'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '1.8'
101
+ description: A Ruby gem allows you to easily interact with Flexipass APIs
102
+ email:
103
+ - kellenkyros@gmail.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".byebug_history"
109
+ - lib/flexipass.rb
110
+ - lib/flexipass/api_error.rb
111
+ - lib/flexipass/client.rb
112
+ - lib/flexipass/company.rb
113
+ - lib/flexipass/configuration.rb
114
+ - lib/flexipass/door.rb
115
+ - lib/flexipass/mobile_key.rb
116
+ - lib/flexipass/version.rb
117
+ homepage: https://github.com/kellenkyros/flexipass
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ homepage_uri: https://github.com/kellenkyros/flexipass
122
+ source_code_uri: https://github.com/kellenkyros/flexipass
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.6.0
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.3.26
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Flexipass mobile key integration gem.
142
+ test_files: []