qismo 0.12.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/qismo/client.rb CHANGED
@@ -25,6 +25,7 @@ module Qismo
25
25
  }
26
26
 
27
27
  attr_accessor(*DEFAULT_OPTIONS.keys)
28
+ attr_reader :admin_email
28
29
 
29
30
  # Initialize client
30
31
  #
@@ -33,15 +34,18 @@ module Qismo
33
34
  DEFAULT_OPTIONS.merge(options).each do |key, value|
34
35
  instance_variable_set("@#{key}", value)
35
36
  end
37
+
38
+ @admin_email = "#{@app_id}_admin@qismo.com"
36
39
  end
37
40
 
38
41
  # Send http request with post method
39
42
  #
40
43
  # @param path [String]
41
44
  # @param body [Hash]
42
- # @return [Qismo::SingleObject]
45
+ # @return [Qismo::ObjectifiedHash]
43
46
  def post(path, body = {})
44
- request(:post, path, json: body)
47
+ puts body.to_json
48
+ request(:post, path, json: body.compact)
45
49
  end
46
50
 
47
51
  # Send HTTP request with post method to upload file
@@ -50,25 +54,25 @@ module Qismo
50
54
  # @param body [Hash]
51
55
  # @return [Qismo::SingleObject]
52
56
  def post_upload(path, body = {})
53
- request(:post, form: body)
57
+ request(:post, path, form: body.compact)
54
58
  end
55
59
 
56
60
  # Send HTTP request with get method
57
61
  #
58
62
  # @param path [String]
59
63
  # @param params [Hash]
60
- # @return [Qismo::SingleObject]
64
+ # @return [Qismo::ObjectifiedHash]
61
65
  def get(path, params = {})
62
- request(:get, path, params: params)
66
+ request(:get, path, params: params.compact)
63
67
  end
64
68
 
65
69
  # Send HTTP request with delete method
66
70
  #
67
71
  # @param path [String]
68
72
  # @param params [Hash]
69
- # @return [Qismo::SingleObject]
73
+ # @return [Qismo::ObjectifiedHash]
70
74
  def delete(path, params = {})
71
- request(:delete, path, params: params)
75
+ request(:delete, path, params: params.compact)
72
76
  end
73
77
 
74
78
  # Send HTTP request
@@ -78,16 +82,19 @@ module Qismo
78
82
  # @param headers [Hash]
79
83
  # @param params [Hash]
80
84
  # @param json [Hash]
81
- # @return [Qismo::SingleObject]
82
- def request(method, path, headers: {}, params: {}, json: {})
83
- res = connection.request(method, @url + path, {
84
- headers: headers,
85
- params: params,
86
- json: json
87
- })
85
+ # @return [Qismo::ObjectifiedHash]
86
+ def request(method, path, options = {})
87
+ res = connection.request(method, @url + path, options.compact)
88
+
89
+ # require "byebug"
90
+ # byebug
88
91
 
89
92
  if res.status.success?
90
- return SingleObject.new(JSON.parse(res.to_s))
93
+ begin
94
+ return JSON.parse(res.to_s, object_class: Qismo::ObjectifiedHash)
95
+ rescue JSON::ParserError
96
+ return res.to_s
97
+ end
91
98
  end
92
99
 
93
100
  if res.status.server_error?
@@ -95,10 +102,10 @@ module Qismo
95
102
  end
96
103
 
97
104
  if res.status.client_error?
98
- body = SingleObject.new(JSON.parse(res.to_s))
105
+ body = JSON.parse(res.to_s, object_class: Qismo::ObjectifiedHash)
99
106
 
100
107
  error = body.errors
101
- error = error.message if error.is_a?(SingleObject)
108
+ error = error.message if error.is_a?(Hash)
102
109
 
103
110
  error_klass_map = {
104
111
  400 => BadRequestError,
@@ -143,7 +150,7 @@ module Qismo
143
150
  http.headers({
144
151
  "Qiscus-App-Id": @app_id,
145
152
  "Qiscus-Secret-Key": @secret_key,
146
- "User-Agent": user_agent.to_json
153
+ "User-Agent": user_agent
147
154
  })
148
155
  end
149
156
 
@@ -151,13 +158,20 @@ module Qismo
151
158
  #
152
159
  # @return [Hash]
153
160
  def user_agent
154
- {
161
+ format = {
162
+ lib_name: "Qismo Ruby",
155
163
  lib_version: Qismo::VERSION,
156
164
  lang: "ruby",
157
165
  lang_version: RUBY_VERSION,
158
166
  platform: RUBY_PLATFORM,
159
167
  engine: defined?(RUBY_ENGINE) ? RUBY_ENGINE : ""
160
168
  }
169
+
170
+ "#{format[:lib_name]}/#{format[:lib_version]} (#{format[:platform]}; #{format[:lang]}; v#{format[:lang_version]}) app_id:#{@app_id}"
171
+ end
172
+
173
+ def default_broadcast_name
174
+ Time.current.strftime("%d/%m/%Y, %H:%I %z")
161
175
  end
162
176
  end
163
177
  end
@@ -0,0 +1,19 @@
1
+ module Qismo
2
+ class Collection < Array
3
+ # Collection object
4
+ #
5
+ # @param data [Array]
6
+ # @param prev_page [String,Integer]
7
+ # @param next_page [String,Integer]
8
+ # @param pagination_type [String]
9
+ def initialize(data, prev_page: nil, next_page: nil)
10
+ super(data)
11
+
12
+ @prev_page = prev_page
13
+ @next_page = next_page
14
+ end
15
+
16
+ attr_reader :next_page
17
+ attr_reader :prev_page
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require "dry-struct"
2
+
3
+ module Qismo
4
+ class Object < Dry::Struct
5
+ include Qismo::Types
6
+
7
+ transform_keys(&:to_sym)
8
+
9
+ class << self
10
+ # Create object from array of hash
11
+ #
12
+ # @param array [Array<Hash>]
13
+ # @return [Array<Object>]
14
+ def from_array(array)
15
+ array.map { |a| new(a) }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module Qismo
2
+ class ObjectifiedHash < Hash
3
+ def method_missing(method, *args, &block)
4
+ return super unless respond_to?(method)
5
+
6
+ fetch(method.to_s)
7
+ end
8
+
9
+ def respond_to?(method_name, include_private = false)
10
+ fetch(method_name.to_s)
11
+
12
+ true
13
+ rescue KeyError
14
+ super(method_name, include_private)
15
+ end
16
+
17
+ def respond_to_missing?(method_name, include_private = false)
18
+ fetch(method_name.to_s)
19
+
20
+ true
21
+ rescue KeyError
22
+ super(method_name, include_private)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ module Qismo
2
+ module Objects
3
+ class AgentService < Qismo::Object
4
+ # @!attribute [r] created_at
5
+ # @return [String]
6
+ attribute? :created_at, Types::String.optional
7
+
8
+ # @!attribute [r] first_comment_id
9
+ # @return [String]
10
+ attribute? :first_comment_id, Types::String.optional
11
+
12
+ # @!attribute [r] first_comment_timestamp
13
+ # @return [String]
14
+ attribute? :first_comment_timestamp, Types::String.optional
15
+
16
+ # @!attribute [r] is_resolved
17
+ # @return [TrueClass,FalseClass]
18
+ attribute? :is_resolved, Types::Bool.optional
19
+
20
+ # @!attribute [r] last_comment_id
21
+ # @return [String]
22
+ attribute? :last_comment_id, Types::String.optional
23
+
24
+ # @!attribute [r] notes
25
+ # @return [String]
26
+ attribute? :notes, Types::String.optional
27
+
28
+ # @!attribute [r] resolved_at
29
+ # @return [String]
30
+ attribute? :resolved_at, Types::String.optional
31
+
32
+ # @!attribute [r] retrieved_at
33
+ # @return [String]
34
+ attribute? :retrieved_at, Types::String.optional
35
+
36
+ # @!attribute [r] room_id
37
+ # @return [Integer]
38
+ attribute? :room_id, Types::String.optional
39
+
40
+ # @!attribute [r] updated_at
41
+ # @return [String]
42
+ attribute? :updated_at, Types::String.optional
43
+
44
+ # @!attribute [r] user_id
45
+ # @return [Integer]
46
+ attribute? :user_id, Types::Int.optional
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,95 @@
1
+ module Qismo
2
+ module Objects
3
+ class BroadcastJob < Qismo::Object
4
+ class User < Qismo::Object
5
+ # @!attribute [r] name
6
+ # @return [String]
7
+ attribute? :name, Types::String.optional
8
+ end
9
+
10
+ # @!attribute [r] user
11
+ # @return [Qismo::Objects::BroadcastJob::User]
12
+ attribute? :user, User.optional
13
+
14
+ # @!attribute [r] broadcast_job_id
15
+ # @return [Integer]
16
+ attribute? :broadcast_job_id, Types::Int.optional
17
+
18
+ # @!attribute [r] broadcast_logs
19
+ # @return [Array<Qismo::Objects::BroadcastLog>]
20
+ attribute? :broadcast_logs, Types.Array(Qismo::Objects::BroadcastLog.optional).optional
21
+
22
+ # @!attribute [r] language
23
+ # @return [String]
24
+ attribute? :language, Types::String.optional
25
+
26
+ # @!attribute [r] name
27
+ # @return [String]
28
+ attribute? :name, Types::String.optional
29
+
30
+ # @!attribute [r] namespace
31
+ # @return [String]
32
+ attribute? :namespace, Types::String.optional
33
+
34
+ # @!attribute [r] canceled_at
35
+ # @return [String]
36
+ attribute? :canceled_at, Types::String.optional
37
+
38
+ # @!attribute [r] canceled_by
39
+ # @return [Integer]
40
+ attribute? :canceled_by, Types::Int.optional
41
+
42
+ # @!attribute [r] channel_id
43
+ # @return [Integer]
44
+ attribute? :channel_id, Types::Int.optional
45
+
46
+ # @!attribute [r] channel_name
47
+ # @return [String]
48
+ attribute? :channel_name, Types::String.optional
49
+
50
+ # @!attribute [r] created_at
51
+ # @return [String]
52
+ attribute? :created_at, Types::String.optional
53
+
54
+ # @!attribute [r] id
55
+ # @return [Integer]
56
+ attribute? :id, Types::Int | Types::String
57
+
58
+ # @!attribute [r] started_at
59
+ # @return [String]
60
+ attribute? :started_at, Types::String.optional
61
+
62
+ # @!attribute [r] status
63
+ # @return [String]
64
+ attribute? :status, Types::String.optional
65
+
66
+ # @!attribute [r] template_detail_id
67
+ # @return [Integer]
68
+ attribute? :template_detail_id, Types::Int.optional
69
+
70
+ # @!attribute [r] template_id
71
+ # @return [Integer]
72
+ attribute? :template_id, Types::Int.optional
73
+
74
+ # @!attribute [r] template_name
75
+ # @return [String]
76
+ attribute? :template_name, Types::String.optional
77
+
78
+ # @!attribute [r] total_failed
79
+ # @return [Integer]
80
+ attribute? :total_failed, Types::Int.optional
81
+
82
+ # @!attribute [r] total_read
83
+ # @return [Integer]
84
+ attribute? :total_read, Types::Int.optional
85
+
86
+ # @!attribute [r] total_received
87
+ # @return [Integer]
88
+ attribute? :total_received, Types::Int.optional
89
+
90
+ # @!attribute [r] total_recipient
91
+ # @return [Integer]
92
+ attribute? :total_recipient, Types::Int.optional
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,57 @@
1
+ module Qismo
2
+ module Objects
3
+ class BroadcastLog < Qismo::Object
4
+ # @!attribute [r] button_params
5
+ # @return [String]
6
+ attribute? :button_params, Types::String.optional
7
+
8
+ # @!attribute [r] customer_name
9
+ # @return [String]
10
+ attribute? :customer_name, Types::String.optional
11
+
12
+ # @!attribute [r] header_value
13
+ # @return [String]
14
+ attribute? :header_value, Types::String.optional
15
+
16
+ # @!attribute [r] id
17
+ # @return [Integer]
18
+ attribute? :id, Types::Int.optional
19
+
20
+ # @!attribute [r] message
21
+ # @return [String]
22
+ attribute? :message, Types::String.optional
23
+
24
+ # @!attribute [r] message_id
25
+ # @return [String]
26
+ attribute? :message_id, Types::String.optional
27
+
28
+ # @!attribute [r] notes
29
+ # @return [String]
30
+ attribute? :notes, Types::String.optional
31
+
32
+ # @!attribute [r] params
33
+ # @return [String]
34
+ attribute? :params, Types::String.optional
35
+
36
+ # @!attribute [r] phone_number
37
+ # @return [String]
38
+ attribute? :phone_number, Types::String.optional
39
+
40
+ # @!attribute [r] sent_at
41
+ # @return [String]
42
+ attribute? :sent_at, Types::String.optional
43
+
44
+ # @!attribute [r] status
45
+ # @return [String]
46
+ attribute? :status, Types::String.optional
47
+
48
+ # @!attribute [r] template_name
49
+ # @return [String]
50
+ attribute? :template_name, Types::String.optional
51
+
52
+ # @!attribute [r] variables
53
+ # @return [String]
54
+ attribute? :variables, Types::String.optional
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module Qismo
2
+ module Objects
3
+ class CustomChannelMessageResponse < Qismo::Object
4
+ # @!attribute [r] agent
5
+ # @return [Qismo::Objects::User]
6
+ attribute? :agent, Qismo::Objects::User.optional
7
+
8
+ # @!attribute [r] agent_service
9
+ # @return [Qismo::Objects::AgentService]
10
+ attribute? :agent_service, Qismo::Objects::AgentService.optional
11
+
12
+ # @!attribute [r] room_log
13
+ # @return [Qismo::Objects::CustomerRoom]
14
+ attribute? :room_log, Qismo::Objects::CustomerRoom
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,83 @@
1
+ require "qismo/object"
2
+
3
+ module Qismo
4
+ module Objects
5
+ class CustomerRoom < Qismo::Object
6
+ # @!attribute [r] channel_id
7
+ # @return [Integer]
8
+ attribute? :channel_id, Types::Int.optional
9
+
10
+ # @!attribute [r] contact_id
11
+ # @return [Integer]
12
+ attribute? :contact_id, Types::Int.optional
13
+
14
+ # @!attribute [r] id
15
+ # @return [Integer]
16
+ attribute? :id, Types::Int.optional
17
+
18
+ # @!attribute [r] is_handled_by_bot
19
+ # @return [TrueClass,FalseClass]
20
+ attribute? :is_handled_by_bot, Types::Bool.optional
21
+
22
+ # @!attribute [r] is_resolved
23
+ # @return [String]
24
+ attribute? :is_resolved, Types::Bool
25
+
26
+ # @!attribute [r] is_waiting
27
+ # @return [TrueClass,FalseClass]
28
+ attribute? :is_waiting, Types::Bool
29
+
30
+ # @!attribute [r] last_comment_sender
31
+ # @return [String]
32
+ attribute? :last_comment_sender, String
33
+
34
+ # @!attribute [r] last_comment_sender_type
35
+ # @return [String]
36
+ attribute? :last_comment_sender_type, Types::String.optional
37
+
38
+ # @!attribute [r] last_comment_text
39
+ # @return [String]
40
+ attribute? :last_comment_text, Types::String.optional
41
+
42
+ # @!attribute [r] last_comment_timestamp
43
+ # @return [String]
44
+ attribute? :last_comment_timestamp, Types::String.optional
45
+
46
+ # @!attribute [r] last_customer_comment_text
47
+ # @return [String]
48
+ attribute? :last_customer_comment_text, Types::String.optional
49
+
50
+ # @!attribute [r] last_customer_timestamp
51
+ # @return [String]
52
+ attribute? :last_customer_timestamp, Types::String.optional
53
+
54
+ # @!attribute [r] name
55
+ # @return [String]
56
+ attribute? :name, Types::String.optional
57
+
58
+ # @!attribute [r] room_badge
59
+ # @return [String]
60
+ attribute? :room_badge, Types::String.optional
61
+
62
+ # @!attribute [r] room_id
63
+ # @return [String]
64
+ attribute? :room_id, Types::Int.optional
65
+
66
+ # @!attribute [r] room_type
67
+ # @return [String]
68
+ attribute? :room_type, Types::String.optional
69
+
70
+ # @!attribute [r] source
71
+ # @return [String]
72
+ attribute? :source, Types::String.optional
73
+
74
+ # @!attribute [r] user_avatar_url
75
+ # @return [String]
76
+ attribute? :user_avatar_url, Types::String.optional
77
+
78
+ # @!attribute [r] user_id
79
+ # @return [String]
80
+ attribute? :user_id, Types::String.optional
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,105 @@
1
+ module Qismo
2
+ module Objects
3
+ class HsmTemplate < Qismo::Object
4
+ class HsmDetail < Qismo::Object
5
+ class Button < Qismo::Object
6
+ # @!attribute [r] text
7
+ # @return [String]
8
+ attribute? :text, Types::String.optional
9
+
10
+ # @!attribute [r] type
11
+ # @return [String]
12
+ attribute? :type, Types::String.optional
13
+
14
+ # @!attribute [r] url
15
+ # @return [String]
16
+ attribute? :url, Types::String.optional
17
+
18
+ # @!attribute [r] phone_number
19
+ # @return [String]
20
+ attribute? :phone_number, Types::String.optional
21
+ end
22
+
23
+ # @!attribute [r] approval_status
24
+ # @return [Integer]
25
+ attribute? :approval_status, Types::Int.optional
26
+
27
+ # @!attribute [r] buttons
28
+ # @return [Array<Qismo::Objects::HsmInfo::Button>]
29
+ attribute? :buttons, Types.Array(Button.optional).optional
30
+
31
+ # @!attribute [r] content
32
+ # @return [String]
33
+ attribute? :content, Types::String.optional
34
+
35
+ # @!attribute [r] footer
36
+ # @return [String]
37
+ attribute? :footer, Types::String.optional
38
+
39
+ # @!attribute [r] header_content
40
+ # @return [String]
41
+ attribute? :header_content, Types::String.optional
42
+
43
+ # @!attribute [r] header_default_value
44
+ # @return [String]
45
+ attribute? :header_default_value, Types::String.optional
46
+
47
+ # @!attribute [r] header_type
48
+ # @return [String]
49
+ attribute? :header_type, Types::String.optional
50
+
51
+ # @!attribute [r] id
52
+ # @return [Integer]
53
+ attribute? :id, Types::Int.optional
54
+
55
+ # @!attribute [r] language
56
+ # @return [String]
57
+ attribute? :language, Types::String.optional
58
+
59
+ # @!attribute [r] number_of_arguments
60
+ # @return [Integer]
61
+ attribute? :number_of_arguments, Types::Int.optional
62
+
63
+ # @!attribute [r] rejection_reason
64
+ # @return [String]
65
+ attribute? :rejection_reason, Types::String.optional
66
+
67
+ # @!attribute [r] tested
68
+ # @return [TrueClass,FalseClass]
69
+ attribute? :tested, Types::Bool.optional
70
+ end
71
+
72
+ # @!attribute [r] category
73
+ # @return [String]
74
+ attribute? :category, Types::String.optional
75
+
76
+ # @!attribute [r] channel_id
77
+ # @return [Integer]
78
+ attribute? :channel_id, Types::Int.optional
79
+
80
+ # @!attribute [r] channel_name
81
+ # @return [String]
82
+ attribute? :channel_name, Types::String.optional
83
+
84
+ # @!attribute [r] hsm_details
85
+ # @return [Array<Qismo::Objects::HsmInfo::HsmDetail>]
86
+ attribute? :hsm_details, Types.Array(HsmDetail.optional).optional
87
+
88
+ # @!attribute [r] id
89
+ # @return [Integer]
90
+ attribute? :id, Types::Int.optional
91
+
92
+ # @!attribute [r] name
93
+ # @return [String]
94
+ attribute? :name, Types::String.optional
95
+
96
+ # @!attribute [r] namespace
97
+ # @return [String]
98
+ attribute? :namespace, Types::String.optional
99
+
100
+ # @!attribute [r] type
101
+ # @return [Integer]
102
+ attribute? :type, Types::Int.optional
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,63 @@
1
+ module Qismo
2
+ module Objects
3
+ class OfficeHour < Qismo::Object
4
+ class OfficeHourElement < Qismo::Object
5
+ # @!attribute [r] day
6
+ # @return [Integer]
7
+ attribute? :day, Types::Int.optional
8
+
9
+ # @!attribute [r] starttime
10
+ # @return [String]
11
+ attribute? :starttime, Types::String.optional
12
+
13
+ # @!attribute [r] endtime
14
+ # @return [String]
15
+ attribute? :endtime, Types::String.optional
16
+ end
17
+
18
+ # @!attribute [r] online_message
19
+ # @return [String]
20
+ attribute? :online_message, Types::String.optional
21
+
22
+ # @!attribute [r] offline_message
23
+ # @return [String]
24
+ attribute? :offline_message, Types::String.optional
25
+
26
+ # @!attribute [r] timezone
27
+ # @return [String]
28
+ attribute? :timezone, Types::String.optional
29
+
30
+ # @!attribute [r] send_online_if_resolved
31
+ # @return [TrueClass,FalseClass]
32
+ attribute? :send_online_if_resolved, Types::Bool.optional
33
+
34
+ # @!attribute [r] send_offline_each_message
35
+ # @return [TrueClass,FalseClass]
36
+ attribute? :send_offline_each_message, Types::Bool.optional
37
+
38
+ # @!attribute [r] office_hours
39
+ # @return [Array<Qismo::Objects::OfficeHour::OfficeHourElement>]
40
+ attribute? :office_hours, Types.Array(OfficeHourElement.optional).optional
41
+
42
+ def in_office_hour?
43
+ return false if timezone.blank?
44
+
45
+ offset = ::Time.zone_offset(timezone)
46
+ tz_current = ::Time.current.getlocal(offset)
47
+
48
+ today_office_hour = office_hours.find { |oh| oh.day == tz_current.strftime("%u").to_i }
49
+ return false if today_office_hour.nil?
50
+
51
+ start_hour = today_office_hour.starttime
52
+ end_hour = today_office_hour.endtime
53
+
54
+ # rubocop:disable Layout/LineLength
55
+ start_time = ::Time.parse("#{tz_current.year}-#{tz_current.month}-#{tz_current.day} #{start_hour} #{timezone}")
56
+ end_time = ::Time.parse("#{tz_current.year}-#{tz_current.month}-#{tz_current.day} #{end_hour} #{timezone}")
57
+ # rubocop:enable Layout/LineLength
58
+
59
+ tz_current.between?(start_time, end_time)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,13 @@
1
+ module Qismo
2
+ module Objects
3
+ class RoomAdditionalInfo < Qismo::Object
4
+ # @!attribute [r] key
5
+ # @return [String]
6
+ attribute? :key, Types::String.optional
7
+
8
+ # @!attribute [r] value
9
+ # @return [String,Integer,TrueClass,FalseClass]
10
+ attribute? :value, (Types::String.optional | Types::Int.optional | Types::Params::Bool.optional)
11
+ end
12
+ end
13
+ end