qismo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
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? || resp.status.informational? || resp.status.redirect?
78
+
79
+ if resp.status.server_error?
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.status.client_error?
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
+ klass.message = errors if klass.message.is_a?(String)
128
+ else
129
+ klass.message = klass.http_body
130
+ end
131
+
132
+ klass
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qismo
4
+ # @return [String] gem version constant
5
+ VERSION = "0.1.0"
6
+ end
data/lib/qismo.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helpers
4
+ require "qismo/helpers/base_helper"
5
+
6
+ # Base
7
+ require "qismo/version"
8
+ require "qismo/errors"
9
+ require "qismo/response"
10
+ require "qismo/client"
11
+
12
+ # Resources
13
+ require "qismo/resources/agent_resource"
14
+ require "qismo/resources/room_resource"
15
+ require "qismo/resources/office_setting_resource"
16
+ require "qismo/resources/user_resource"
17
+ require "qismo/resources/bot_resource"
18
+
19
+ # Models
20
+ require "qismo/models/base"
21
+ require "qismo/models/agent"
22
+ require "qismo/models/room"
23
+ require "qismo/models/office_setting"
24
+ require "qismo/models/office_hour"
25
+ require "qismo/models/user"
26
+ require "qismo/models/bot"
27
+
28
+ #
29
+ # Base module of Qismo ruby
30
+ #
31
+ module Qismo
32
+ class << self
33
+ attr_writer :client
34
+
35
+ #
36
+ # Configure client using block
37
+ #
38
+ # @yieldparam [Client]
39
+ #
40
+ # @yieldreturn [Client]
41
+ #
42
+ def init
43
+ yield(client)
44
+
45
+ client
46
+ end
47
+
48
+ alias_method :configure, :init
49
+
50
+ #
51
+ # Configure client
52
+ #
53
+ # @param app_id [String]
54
+ # @param secret_key [String]
55
+ # @param base_url [String]
56
+ #
57
+ # @return [Client]
58
+ #
59
+ def new(app_id:, secret_key:, base_url: Client::DEFAULT_BASE_URL)
60
+ @client = Client.new(app_id: app_id, secret_key: secret_key, base_url: base_url)
61
+ end
62
+
63
+ #
64
+ # Qismo ruby client
65
+ #
66
+ # @return [Qismo::Client]
67
+ #
68
+ def client
69
+ @client ||= Client.new
70
+ end
71
+ end
72
+ end
data/qismo.gemspec ADDED
@@ -0,0 +1,30 @@
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.4.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", "~> 4.4")
28
+
29
+ spec.add_development_dependency("rubocop-shopify", "~> 2.3")
30
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qismo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nurcholis Art
8
+ - Qiscus
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-02-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: http
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rubocop-shopify
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.3'
42
+ description:
43
+ email:
44
+ - nurcholis@qiscus.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".editorconfig"
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - ".ruby-version"
54
+ - CODE_OF_CONDUCT.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/console
61
+ - bin/setup
62
+ - lib/qismo.rb
63
+ - lib/qismo/client.rb
64
+ - lib/qismo/errors.rb
65
+ - lib/qismo/helpers/base_helper.rb
66
+ - lib/qismo/models/agent.rb
67
+ - lib/qismo/models/base.rb
68
+ - lib/qismo/models/bot.rb
69
+ - lib/qismo/models/null_object.rb
70
+ - lib/qismo/models/office_hour.rb
71
+ - lib/qismo/models/office_setting.rb
72
+ - lib/qismo/models/room.rb
73
+ - lib/qismo/models/user.rb
74
+ - lib/qismo/operations.rb
75
+ - lib/qismo/resources/agent_resource.rb
76
+ - lib/qismo/resources/bot_resource.rb
77
+ - lib/qismo/resources/office_setting_resource.rb
78
+ - lib/qismo/resources/room_resource.rb
79
+ - lib/qismo/resources/user_resource.rb
80
+ - lib/qismo/response.rb
81
+ - lib/qismo/version.rb
82
+ - qismo.gemspec
83
+ homepage: https://multichannel.qiscus.com
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.4.0
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.1.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Qiscus Multichannel Ruby Client
106
+ test_files: []