qismo 0.1.8 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,182 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Qismo
4
- module Resources
5
- module RoomResource
6
- #
7
- # List rooms
8
- #
9
- # @param params [Hash]
10
- #
11
- # @return [Array<Room>]
12
- #
13
- def list(params = {})
14
- resp = Qismo.client.get("/api/v2/customer_rooms", params)
15
-
16
- body = resp.http_body
17
- customer_rooms = body.dig("data", "customer_rooms")
18
-
19
- rooms = []
20
- customer_rooms.each do |cr|
21
- rooms.append(Room.new(cr))
22
- end
23
-
24
- rooms
25
- end
26
-
27
- #
28
- # Get room by id
29
- #
30
- # @param id [Integer]
31
- #
32
- # @return [Room]
33
- #
34
- def get(id)
35
- resp = Qismo.client.get("/api/v2/customer_rooms/#{id}")
36
-
37
- body = resp.http_body
38
- room = body.dig("data", "customer_room")
39
- unless room
40
- raise NotFound.new(
41
- "Room with id #{id} is not found",
42
- http_code: 404,
43
- http_headers: resp.http_headers,
44
- http_body: body,
45
- http_raw_body: resp.http_raw_body
46
- )
47
- end
48
-
49
- Room.new(room)
50
- end
51
-
52
- #
53
- # Assign an agent to a room
54
- #
55
- # @param room_id [String]
56
- # @param agent_id [Integer]
57
- # @param options [Hash]
58
- #
59
- # @return [Agent]
60
- #
61
- def assign_agent(room_id, agent_id, options = {})
62
- options[:room_id] = room_id.to_s
63
- options[:agent_id] = agent_id
64
-
65
- resp = Qismo.client.post("/api/v1/admin/service/assign_agent", options)
66
-
67
- body = resp.http_body
68
- added_agent = body.dig("data", "added_agent")
69
-
70
- Agent.new(added_agent)
71
- end
72
-
73
- #
74
- # List agents who can be assigned to a room
75
- #
76
- # @param options [Hash]
77
- # @option options [Integer, String] :room_id Required
78
- # @option options [Integer, String] :limit Optional. Default: 10. Max: 25
79
- # @option options [String] :cursor_before
80
- # @option options [String] :cursor_after
81
- # @option options [String] :search Filter agents by name or email
82
- #
83
- # @return [Array<Agent>]
84
- #
85
- def other_agents(options = {})
86
- validate_existence_of!(:room_id)
87
-
88
- resp = Qismo.client.call(:get, "/api/v2/admin/service/other_agents", params: options)
89
-
90
- body = resp.http_body
91
- qismo_agents = body.dig("data", "agents")
92
-
93
- agents = []
94
- qismo_agents.each do |qismo_agent|
95
- agents.append(Agent.new(qismo_agent))
96
- end
97
-
98
- agents
99
- end
100
-
101
- alias_method :eligible_agents, :other_agents
102
-
103
- #
104
- # Resolve room
105
- #
106
- # @param options [Hash]
107
- # @option options room_id [String]
108
- # @option options notes [String]
109
- # @option options last_comment_id [Integer]
110
- # @option options is_send_email [TruClass, FalseClass]
111
- # @option options extras optional [Hash]
112
- #
113
- # @return [Response]
114
- #
115
- def resolve(options = {})
116
- validate_existence_of!(:room_id, on: options)
117
-
118
- fetch_last_comment_id = lambda do
119
- resp = Qismo.client.raw_request(:get, "https://api.qiscus.com/api/v2.1/rest/load_comments", {
120
- headers: { qiscus_sdk_app_id: Qismo.client.app_id, qiscus_sdk_secret: Qismo.client.secret_key },
121
- params: { room_id: options[:room_id], limit: 1 },
122
- })
123
-
124
- body = resp.http_body
125
- if body.empty?
126
- raise BadRequest.new(
127
- "There is no message in room #{options[:room_id]}",
128
- http_code: 200,
129
- http_headers: resp.http_headers,
130
- http_body: resp.http_body,
131
- http_raw_body: resp.http_raw_body
132
- )
133
- end
134
-
135
- id = body.dig("results", "comments", 0, "id")
136
- if id.nil?
137
- raise BadRequest.new(
138
- "There is no message in room #{options[:room_id]}",
139
- http_code: 200,
140
- http_headers: resp.http_headers,
141
- http_body: resp.http_body,
142
- http_raw_body: resp.http_raw_body
143
- )
144
- end
145
-
146
- id
147
- end
148
-
149
- if falsy?(options[:last_comment_id])
150
- options[:last_comment_id] = fetch_last_comment_id.call
151
- end
152
-
153
- validate_existence_of!(:last_comment_id, :notes, :is_send_email, :extras, on: options)
154
-
155
- options[:room_id] = options[:room_id].to_i
156
- Qismo.client.post(:post, "/api/v1/admin/service/mark_as_resolved", options)
157
- end
158
-
159
- #
160
- # Activate bot in a room
161
- #
162
- # @param id [Integer] Room id
163
- #
164
- # @return [Response]
165
- #
166
- def activate_bot(id)
167
- Qismo.client.post("/bot/#{id}/activate", is_active: true)
168
- end
169
-
170
- #
171
- # Deactivate bot in a room
172
- #
173
- # @param id [Integer] Room id
174
- #
175
- # @return [Response]
176
- #
177
- def deactivate_bot(id)
178
- Qismo.client.post("/bot/#{id}/activate", is_active: true)
179
- end
180
- end
181
- end
182
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Qismo
4
- module Resources
5
- module UserResource
6
- #
7
- # Login to Qismo
8
- #
9
- # @param email [String]
10
- # @param password [String]
11
- # @param role [String]
12
- #
13
- # @return [User]
14
- #
15
- def login(email, password, role: "admin")
16
- path = if role == "admin"
17
- "/api/v1/auth"
18
- else
19
- "/api/v1/#{Qismo.client.app_id}/agent_auth"
20
- end
21
-
22
- resp = Qismo.client.post(path, email: email, password: password)
23
-
24
- body = resp.http_body
25
-
26
- data = body.fetch("data", {})
27
- user = data.fetch("user", {})
28
- user[:long_lived_token] = data["long_lived_token"]
29
-
30
- User.new(user)
31
- end
32
-
33
- #
34
- # Logout from Qismo
35
- #
36
- # @param access_token [String]
37
- #
38
- # @return [Response]
39
- #
40
- def logout(access_token)
41
- Qismo.client.request(:post, "/api/v2/auth/logout", headers: { authorization: access_token })
42
- end
43
- end
44
- end
45
- end
@@ -1,135 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Qismo
4
- #
5
- # HTTP response handler
6
- #
7
- # @!attribute http_code [Integer]
8
- # @!attribute http_headers [HTTP::Headers]
9
- # @!attribute http_body [String]
10
- # @!attribute http_raw_body [String]
11
- # @!attribute error [Error]
12
- class Response
13
- include Qismo::Helpers::BaseHelper
14
-
15
- # @return [Integer] bad request constant
16
- BAD_REQUEST = 400
17
-
18
- # @return [Integer] unauthorized constant
19
- UNAUTHORIZED = 401
20
-
21
- # @return [Integer] payment required constant
22
- PAYMENT_REQUIRED = 402
23
-
24
- # @return [Integer] forbidden constant
25
- FORBIDDEN = 403
26
-
27
- # @return [Integer] not found constant
28
- NOT_FOUND = 404
29
-
30
- # @return [Integer] rate limit exceeded constant
31
- RATE_LIMIT_EXCEEDED = 429
32
-
33
- # @return [Integer] internal server error constant
34
- INTERNAL_SERVER_ERROR = 500
35
-
36
- attr_reader :http_code, :http_headers, :http_body, :http_raw_body, :error
37
-
38
- #
39
- # Response
40
- #
41
- # @param resp [HTTP::Response]
42
- #
43
- def initialize(resp)
44
- @http_code = resp.code
45
- @http_headers = parse_headers(resp)
46
- @http_body = parse_body(resp)
47
- @http_raw_body = resp.to_s
48
-
49
- @error = parse_error(resp)
50
- end
51
-
52
- def ok?
53
- (200..299).member?(@http_code)
54
- end
55
-
56
- alias_method :success?, :ok?
57
-
58
- def raise_for_error
59
- if @error.nil?
60
- return
61
- end
62
-
63
- raise @error
64
- end
65
-
66
- private
67
-
68
- def parse_body(resp)
69
- safe_parse_json(resp.to_s)
70
- end
71
-
72
- def parse_headers(resp)
73
- safe_parse_json(resp.headers.to_h)
74
- end
75
-
76
- def parse_error(resp)
77
- return nil if resp.status.ok?
78
-
79
- if resp.code >= 500
80
- return InternalServerError.new(
81
- "Qiscus internal server error",
82
- http_code: resp.code,
83
- http_headers: safe_parse_json(resp.headers.to_h),
84
- http_body: safe_parse_json(resp.to_s),
85
- http_raw_body: resp.to_s
86
- )
87
- end
88
-
89
- if resp.code >= 400
90
- code = resp.code
91
- headers = safe_parse_json(resp.headers.to_h)
92
- raw_body = resp.to_s
93
-
94
- klass = case code
95
- when BAD_REQUEST then BadRequest.new
96
- when UNAUTHORIZED then Unauthorized.new
97
- when PAYMENT_REQUIRED then PaymentRequired.new
98
- when FORBIDDEN then Forbidden.new
99
- when NOT_FOUND then NotFound.new
100
- when RATE_LIMIT_EXCEEDED then RateLimitExceeded.new
101
- else
102
- ClientError.new
103
- end
104
-
105
- if code == BAD_REQUEST && raw_body.downcase.include?("wrong email or password")
106
- klass = Unauthorized.new
107
- end
108
-
109
- klass.http_code = code
110
- klass.http_headers = headers
111
- klass.http_raw_body = raw_body
112
- klass.http_body = safe_parse_json(raw_body)
113
- klass.message = klass.class.to_s
114
-
115
- if klass.http_body.is_a?(Hash)
116
- http_body = klass.http_body
117
-
118
- error_message = if http_body["errors"].is_a?(String)
119
- http_body["errors"]
120
- elsif http_body["errors"].is_a?(Hash) && http_body.dig("errors", "message").is_a?(String)
121
- http_body["errors"]["message"]
122
- else
123
- klass.class.to_s
124
- end
125
-
126
- klass.message = error_message
127
- else
128
- klass.message = klass.http_raw_body
129
- end
130
-
131
- klass
132
- end
133
- end
134
- end
135
- end
data/qismo.gemspec DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/qismo/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "qismo"
7
- spec.version = Qismo::VERSION
8
- spec.authors = ["Nurcholis Art", "Qiscus"]
9
- spec.email = ["nurcholis@qiscus.com"]
10
-
11
- spec.summary = "Qiscus Multichannel Ruby Client"
12
- spec.homepage = "https://multichannel.qiscus.com"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
15
-
16
- # Specify which files should be added to the gem when it is released.
17
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
- %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
20
- end
21
-
22
- spec.bindir = "exe"
23
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
- spec.require_paths = ["lib"]
25
-
26
- # spec.add_runtime_dependency("activesupport", "~> 7.0")
27
- spec.add_runtime_dependency("http", "~> 5.0")
28
-
29
- spec.add_development_dependency("rubocop-shopify", "~> 2.3")
30
- end