xendit-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf2037101e13b2b57ec12d52edb1ca9d1b88384518a9e4a6bac7e227345b951e
4
+ data.tar.gz: 106453aecae96cc27b0325d387f0730c28ffcb153eda880865169786dc722a5f
5
+ SHA512:
6
+ metadata.gz: 5e2a0942541f002d3289018edc5ae4538aa1d2dff50b41c7b1b5acaa659f05bf4ac357b82f098b3de9a0d6df154d8fb3a77f62b6f6309f321f54981f3784871c
7
+ data.tar.gz: 29e8b22d1dd593667f5eb0137650c8ae8947fff5ee0de22d2bb9a668c3b01a727db2a26ee61757f1622b23e96330444f96cdf67ecdf6cfe6572e2677b638df8e
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### Version 0.1.0 (date: 15 Jan 2023)
2
+
3
+ * Support invoice API
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in xendit.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xendit-client (0.1.0)
5
+ faraday (~> 2.7.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ faraday (2.7.2)
11
+ faraday-net_http (>= 2.0, < 3.1)
12
+ ruby2_keywords (>= 0.0.4)
13
+ faraday-net_http (3.0.2)
14
+ rake (13.0.6)
15
+ ruby2_keywords (0.0.5)
16
+
17
+ PLATFORMS
18
+ arm64-darwin-21
19
+ ruby
20
+ universal-darwin-22
21
+
22
+ DEPENDENCIES
23
+ rake (~> 13.0)
24
+ xendit-client!
25
+
26
+ BUNDLED WITH
27
+ 2.3.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Nasrul Gunawan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Xendit Ruby library
2
+
3
+ This is the Unofficial Ruby API client/library for Xendit API.
4
+
5
+ <!-- toc -->
6
+
7
+ - [API Documentation](#api-documentation)
8
+ - [Requirements](#requirements)
9
+ - [Installation](#installation)
10
+ - [Usage](#usage)
11
+ * [Invoice Service](#invoice-service)
12
+
13
+ <!-- tocstop -->
14
+
15
+ ## API Documentation
16
+
17
+ Please check [Xendit API Reference](https://xendit.github.io/apireference/).
18
+
19
+ ## Requirements
20
+ - Ruby 2.5+
21
+
22
+ ## Installation
23
+
24
+ ### Using Gemfile
25
+ Install the gem and add to the application's Gemfile by executing:
26
+ ```ruby
27
+ bundle add xendit-client
28
+ ```
29
+ Or add gem xendit-client to Gemfile
30
+ ```ruby
31
+ gem 'xendit-client'
32
+ ```
33
+ Run this command in your terminal
34
+
35
+ ```ruby
36
+ bundle install
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ Get your API key from [Xendit Dashboard](https://dashboard.xendit.co/settings/developers#api-keys).
42
+
43
+ Add your API key to the initializer.
44
+
45
+ ```ruby
46
+ require 'xendit'
47
+
48
+ Xendit.api_key = 'xnd_...'
49
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,56 @@
1
+ require 'faraday'
2
+ require_relative 'json_serializer'
3
+ require_relative 'response'
4
+ require_relative 'errors'
5
+
6
+ module Xendit
7
+ class ApiClient
8
+ class << self
9
+ def get(url, params = nil)
10
+ conn = create_connection()
11
+ response = conn.get(url, params)
12
+ handle_response(response)
13
+ end
14
+
15
+ def post(url, body)
16
+ conn = create_connection()
17
+ response = conn.post(url, JSONSerializer.encode(body))
18
+ handle_response(response)
19
+ end
20
+
21
+ private
22
+
23
+ def create_connection(additional_headers = {})
24
+ if Xendit.api_key.nil? || Xendit.api_key == ""
25
+ raise "Please configure your API key"
26
+ end
27
+
28
+ default_headers = {
29
+ "Accept" => "application/json",
30
+ "Content-Type" => "application/json",
31
+ "User-Agent" => "Xendit ruby client library #{Xendit::VERSION}"
32
+ }
33
+
34
+ default_headers = default_headers.merge!(additional_headers) if additional_headers != {}
35
+
36
+ Faraday.new(
37
+ url: Xendit.base_url,
38
+ headers: default_headers
39
+ ) do |conn|
40
+ conn.request :authorization, :basic, Xendit.api_key, ''
41
+ end
42
+ end
43
+
44
+ def handle_response(response)
45
+ raise ArgumentError, "Invalid response" if response.nil?
46
+
47
+ if response.status >= 400
48
+ response_body = JSONSerializer.decode(response.body)
49
+ raise Xendit::Errors.new(response_body['error_code'], response_body['message'], response.status)
50
+ end
51
+
52
+ Xendit::Response.new(response)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,18 @@
1
+ module Xendit
2
+ class Errors < StandardError
3
+ attr_reader :error_code, :error_message, :http_status
4
+
5
+ def initialize(error_code, error_message, http_status)
6
+ @error_code = error_code
7
+ @error_message = error_message
8
+ @http_status = http_status
9
+ super(message_string)
10
+ end
11
+
12
+ private
13
+
14
+ def message_string
15
+ "Xendit API is returning API error. HTTP status #{@http_status}: #{@error_code}. Error message: #{@error_message}"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ module Xendit
2
+ class JSONSerializer
3
+ class << self
4
+ # If you using Rails then it will call ActiveSupport::JSON.encode
5
+ # Otherwise JSON.pretty_generate
6
+ def encode(params)
7
+ if defined?(ActiveSupport) && defined?(ActiveSupport::JSON)
8
+ ActiveSupport::JSON.encode(params)
9
+ else
10
+ require 'json' unless defined?(JSON)
11
+ JSON.pretty_generate(params)
12
+ end
13
+ end
14
+
15
+ # If you using Rails then it will call ActiveSupport::JSON.decode
16
+ # Otherwise JSON.parse
17
+ def decode(params)
18
+ if defined?(ActiveSupport) && defined?(ActiveSupport::JSON)
19
+ ActiveSupport::JSON.decode(params)
20
+ else
21
+ require 'json' unless defined?(JSON)
22
+ JSON.parse(params)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../api_client'
2
+
3
+ module Xendit
4
+ class Invoice
5
+ class << self
6
+ def get(invoice_id)
7
+ ApiClient.get "v2/invoices/#{invoice_id}"
8
+ end
9
+
10
+ def create(params)
11
+ ApiClient.post "v2/invoices/", params
12
+ end
13
+
14
+ def expire(invoice_id)
15
+ ApiClient.post "invoices/#{invoice_id}/expire!"
16
+ end
17
+
18
+ def get_all
19
+ ApiClient.get "v2/invoices"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ require_relative 'resources/invoice'
@@ -0,0 +1,20 @@
1
+ require_relative 'json_serializer'
2
+
3
+ module Xendit
4
+ class Response
5
+ attr_reader :status, :body
6
+
7
+ def initialize(response)
8
+ @status = response.status
9
+ @body = JSONSerializer.decode(response.body)
10
+ end
11
+
12
+ def success?
13
+ [200, 201].include?(@status)
14
+ end
15
+
16
+ def created?
17
+ @status == 201
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xendit
4
+ VERSION = "0.1.0"
5
+ end
data/lib/xendit.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "xendit/version"
4
+ require_relative 'xendit/resources'
5
+
6
+ module Xendit
7
+ class << self
8
+ attr_accessor :api_key
9
+ attr_writer :base_url
10
+
11
+ def base_url
12
+ @base_url ||= 'https://api.xendit.co/'
13
+ end
14
+ end
15
+ end
data/sig/xendit.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Xendit
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xendit-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nasrul Gunawan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.2
27
+ description:
28
+ email:
29
+ - search.nasrul@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/xendit.rb
41
+ - lib/xendit/api_client.rb
42
+ - lib/xendit/errors.rb
43
+ - lib/xendit/json_serializer.rb
44
+ - lib/xendit/resources.rb
45
+ - lib/xendit/resources/invoice.rb
46
+ - lib/xendit/response.rb
47
+ - lib/xendit/version.rb
48
+ - sig/xendit.rbs
49
+ homepage: https://github.com/nasrulgunawan/xendit-client
50
+ licenses: []
51
+ metadata:
52
+ homepage_uri: https://github.com/nasrulgunawan/xendit-client
53
+ source_code_uri: https://github.com/nasrulgunawan/xendit-client
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.5.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.4
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Xendit Ruby Client Library
73
+ test_files: []