qismo 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82fdfb3850c268372f3bb9a626923296a277365a2e418e39e77faee771a17f01
4
- data.tar.gz: 5e8e7248607487caab854fc59b45a4db35140b8f48392c3a52ef46fcb7d3b02f
3
+ metadata.gz: 37a8b45b1fb3c7a407f44bb9490f47f101a6c617347fa81519195d408aadd7d8
4
+ data.tar.gz: 48ff80a20004cd10e970fd07276e61aa9e1edffe575e7803fe8e10c2bdba1bdb
5
5
  SHA512:
6
- metadata.gz: 6f18f2d1e90c3892e8793f8e48af1643a8c91fa94307197cab6660df6130aa7e9e6d28d9e8942e51fb9c538d0531e9532275bb728c2ae3cb05c57f539e4f6d0c
7
- data.tar.gz: 804e77fe38f0bb3e5da3859e2560bb74586960d8a7c96a9b723c0026ee2cfdf16ceca7531d05bc3ce7f9e3c748092daad112e458f66800b18776073e6711125e
6
+ metadata.gz: 0dd79f84b481f832e7d6433d20af7e9b6cb729d3e2d19255024ec673eacfb8bde3441c253f6fadc240014d2d8be6c1bd839e09c3defdf3fd1784b66d4751dbc1
7
+ data.tar.gz: f6893f2b12eb14a3770bfd28ed1bf96b286d46ae6fb7b4d4426e954c637e8a5fe842f45e54817abd3ee86156650cfb49003142fe4ba1196791d0c00859da4d64
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
@@ -2,8 +2,6 @@
2
2
 
3
3
  module Qismo
4
4
  module Api
5
- include Qismo::Model
6
-
7
5
  def rooms(**opt)
8
6
  body = post("/api/v2/customer_rooms", opt)
9
7
  Collection.new(
@@ -167,7 +165,11 @@ module Qismo
167
165
  end
168
166
 
169
167
  def office_hours
170
- get("/api/v1/admin/office_hours").data
168
+ data = get("/api/v1/admin/office_hours").data
169
+ data_hash = data.as_json
170
+ data_hash["is_in_office_hour"] = Util.in_office_hour?(data)
171
+
172
+ DataObject.new(data_hash)
171
173
  end
172
174
 
173
175
  def wa_broadcast_templates(**opt)
data/lib/qismo/client.rb CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  module Qismo
4
4
  class Client
5
- include Qismo::Model
6
5
  include Qismo::Api
7
6
 
8
7
  VALID_OPTIONS = [:app_id, :secret_key, :url, :logger, :instrumentation, :timeout, :proxy]
@@ -34,7 +33,7 @@ module Qismo
34
33
  def request(method, path, **opt)
35
34
  res = connection.request(method, @url + path, opt.compact)
36
35
  if res.status.success?
37
- return JSON.parse(res.to_s, object_class: Data)
36
+ return DataObject.new(JSON.parse(res.to_s))
38
37
  end
39
38
 
40
39
  if res.status.server_error?
@@ -42,9 +41,10 @@ module Qismo
42
41
  end
43
42
 
44
43
  if res.status.client_error?
45
- body = JSON.parse(res.to_s, object_class: Data)
44
+ body = DataObject.new(JSON.parse(res.to_s))
45
+
46
46
  error = body.errors
47
- error = error.message if error.is_a?(Hash)
47
+ error = error.message if error.is_a?(DataObject)
48
48
 
49
49
  error_klass_map = {
50
50
  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.1"
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.1
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: