qismo 0.6.0 → 0.7.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
2
  SHA256:
3
- metadata.gz: 82fdfb3850c268372f3bb9a626923296a277365a2e418e39e77faee771a17f01
4
- data.tar.gz: 5e8e7248607487caab854fc59b45a4db35140b8f48392c3a52ef46fcb7d3b02f
3
+ metadata.gz: be7d855a466237baededa8d61cbf42a88a52ff245c82b3a3ee2ecce2dd5ded47
4
+ data.tar.gz: 190f309eed38161f7718ce842e02a6e1e3d2c0e961096263b2ac003ca7216a29
5
5
  SHA512:
6
- metadata.gz: 6f18f2d1e90c3892e8793f8e48af1643a8c91fa94307197cab6660df6130aa7e9e6d28d9e8942e51fb9c538d0531e9532275bb728c2ae3cb05c57f539e4f6d0c
7
- data.tar.gz: 804e77fe38f0bb3e5da3859e2560bb74586960d8a7c96a9b723c0026ee2cfdf16ceca7531d05bc3ce7f9e3c748092daad112e458f66800b18776073e6711125e
6
+ metadata.gz: d9ac3a4baa95ae52792801be684cac5d6d9de3c6bc271e50d2788fab62f243dafe9c8e05721d04d1004a02de22f89406b67e1bed8414315506d331a900738e95
7
+ data.tar.gz: 7ec9a32d43b4025809ada7d61af3e7dedcb9439c5ef074961e31e7714d1122c1a854e45126b8171af9d45c2b77ca364b4cd1032cd71ed1bfd43fc323b1d764ac
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qismo (0.5.0)
4
+ qismo (0.6.0)
5
5
  activesupport
6
6
  http
7
7
 
data/lib/qismo/api.rb CHANGED
@@ -167,7 +167,11 @@ module Qismo
167
167
  end
168
168
 
169
169
  def office_hours
170
- get("/api/v1/admin/office_hours").data
170
+ data = get("/api/v1/admin/office_hours").data
171
+ data_hash = data.as_json
172
+ data_hash["is_in_office_hour"] = Util.in_office_hour?(data)
173
+
174
+ DataObject.new(data_hash)
171
175
  end
172
176
 
173
177
  def wa_broadcast_templates(**opt)
data/lib/qismo/client.rb CHANGED
@@ -34,7 +34,7 @@ module Qismo
34
34
  def request(method, path, **opt)
35
35
  res = connection.request(method, @url + path, opt.compact)
36
36
  if res.status.success?
37
- return JSON.parse(res.to_s, object_class: Data)
37
+ return DataObject.new(JSON.parse(res.to_s))
38
38
  end
39
39
 
40
40
  if res.status.server_error?
@@ -42,9 +42,10 @@ module Qismo
42
42
  end
43
43
 
44
44
  if res.status.client_error?
45
- body = JSON.parse(res.to_s, object_class: Data)
45
+ body = DataObject.new(JSON.parse(res.to_s))
46
+
46
47
  error = body.errors
47
- error = error.message if error.is_a?(Hash)
48
+ error = error.message if error.is_a?(DataObject)
48
49
 
49
50
  error_klass_map = {
50
51
  400 => BadRequestError,
data/lib/qismo/model.rb CHANGED
@@ -1,48 +1,80 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Qismo
4
- module Model
5
- class Data < OpenStruct # rubocop:disable Style/OpenStructUse
4
+ class DataObject
5
+ def initialize(opt = {})
6
+ opt.each do |key, value|
7
+ value = if value.is_a?(Array)
8
+ handle_array(value)
9
+ # instance_variable_set("@#{key}", handle_array(value))
10
+ elsif value.is_a?(Hash)
11
+ self.class.new(value)
12
+ # instance_variable_set("@#{key}", self.class.new(value))
13
+ else
14
+ value
15
+ # instance_variable_set("@#{key}", value)
16
+ end
17
+
18
+ self.class.attr_reader(key.to_sym)
19
+ instance_variable_set("@#{key}", value)
20
+ end
6
21
  end
7
22
 
8
- class Collection < Array
9
- def initialize(data, prev_page: nil, next_page: nil, prev_func: -> { nil }, next_func: -> { nil })
10
- super(data)
23
+ def inspect
24
+ inspected_func ||= -> do
25
+ attributes = instance_variables.map do |iv|
26
+ "#{iv.to_s.delete_prefix("@")}=#{instance_variable_get(iv).inspect}"
27
+ end
11
28
 
12
- @prev_page = prev_page
13
- @next_page = next_page
14
- @prev_func = prev_func
15
- @next_func = next_func
29
+ "#<#{self.class.name} #{attributes.join(", ")}>"
16
30
  end
17
31
 
18
- def has_next_page?
19
- @next_page != nil
20
- end
32
+ inspected_func.call
33
+ end
21
34
 
22
- alias_method :next_page?, :has_next_page?
35
+ alias_method :to_s, :inspect
23
36
 
24
- def has_prev_page?
25
- @prev_page != nil
37
+ private
38
+
39
+ def handle_array(values)
40
+ values.map do |value|
41
+ if value.is_a?(Hash)
42
+ self.class.new(value)
43
+ else
44
+ value
45
+ end
26
46
  end
47
+ end
48
+ end
27
49
 
28
- alias_method :prev_page?, :has_prev_page?
50
+ class Collection < Array
51
+ def initialize(data, prev_page: nil, next_page: nil, prev_func: -> { nil }, next_func: -> { nil })
52
+ super(data)
29
53
 
30
- def next_page
31
- @next_func.call if has_next_page?
32
- end
54
+ @prev_page = prev_page
55
+ @next_page = next_page
56
+ @prev_func = prev_func
57
+ @next_func = next_func
58
+ end
33
59
 
34
- def prev_page
35
- @prev_func.call if has_prev_page?
36
- end
60
+ def has_next_page?
61
+ @next_page != nil
37
62
  end
38
63
 
39
- class Response
40
- attr_reader :data, :meta
64
+ alias_method :next_page?, :has_next_page?
41
65
 
42
- def initialize(data, meta = {})
43
- @data = data
44
- @meta = meta
45
- end
66
+ def has_prev_page?
67
+ @prev_page != nil
68
+ end
69
+
70
+ alias_method :prev_page?, :has_prev_page?
71
+
72
+ def next_page
73
+ @next_func.call if has_next_page?
74
+ end
75
+
76
+ def prev_page
77
+ @prev_func.call if has_prev_page?
46
78
  end
47
79
  end
48
80
  end
data/lib/qismo/util.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qismo
4
+ class Util
5
+ class << self
6
+ def in_office_hour?(data)
7
+ return false if data.timezone.blank?
8
+
9
+ offset = Time.zone_offset(data.timezone)
10
+ tz_current = Time.current.getlocal(offset)
11
+
12
+ today_office_hour = data.office_hours.find { |oh| oh.day == tz_current.strftime("%u").to_i }
13
+ return false if today_office_hour.nil?
14
+
15
+ start_hour = today_office_hour.starttime
16
+ end_hour = today_office_hour.endtime
17
+
18
+ start_time = Time.parse("#{tz_current.year}-#{tz_current.month}-#{tz_current.day} #{start_hour} #{data.timezone}")
19
+ end_time = Time.parse("#{tz_current.year}-#{tz_current.month}-#{tz_current.day} #{end_hour} #{data.timezone}")
20
+
21
+ tz_current.between?(start_time, end_time)
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/qismo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Qismo
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/qismo.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/all"
3
4
  require "http"
4
5
 
6
+ require "qismo/util"
5
7
  require "qismo/version"
6
8
  require "qismo/error"
7
9
  require "qismo/model"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qismo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Qiscus Integration
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-04 00:00:00.000000000 Z
11
+ date: 2022-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -127,6 +127,7 @@ files:
127
127
  - lib/qismo/client.rb
128
128
  - lib/qismo/error.rb
129
129
  - lib/qismo/model.rb
130
+ - lib/qismo/util.rb
130
131
  - lib/qismo/version.rb
131
132
  homepage: https://qiscus.com
132
133
  licenses: