xtract-sdk 0.0.3

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: 8a4abd0dca5657fa1710bdbb1ee483fad535397269085070e1044cf462b5dca8
4
+ data.tar.gz: cb73e9fdf9a9f1464b48cf206c0dbb6f164b93258d6d4969c165e12817281bc3
5
+ SHA512:
6
+ metadata.gz: 6fd1d043cc044e72491a3f70f6190abcf89425fc1bb44fbf4cfa8a9d44f65f0fc9aafb8f06a4b7d5dc906aebbc6120616c508f1c53c70ffbe7a1a0d671b5db18
7
+ data.tar.gz: 95b10e7b32f99f912e7b5e4b9846fb36d7a28b66db81e203199a83bdb515fee76ed5aef2b0efce79e3da31a6532e41f5482f6677a7639209ebc3199f5ae532cb
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
13
+
14
+ .DS_Store
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-performance
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ Exclude:
8
+ - dictum.gemspec
9
+ - lib/tasks/dictum.rake
10
+ - spec/spec_helper.rb
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Layout/LineLength:
16
+ Max: 100
17
+
18
+ Style/FrozenStringLiteralComment:
19
+ Enabled: false
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - spec/**/*
24
+
25
+ RSpec/FilePath:
26
+ Exclude:
27
+ - spec/xtract_sdk/version_spec.rb
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+
7
+ before_install: gem install bundler -v 2.2.31
8
+
9
+ install:
10
+ - bundle install --retry=3
11
+
12
+ script:
13
+ - bundle exec rspec
14
+ - bundle exec rubocop -R lib spec --format simple
15
+
16
+ os:
17
+ - linux
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
6
+
7
+ group :development, :test do
8
+ gem 'byebug', '~> 11.1'
9
+ end
10
+
11
+ group :test do
12
+ gem 'rspec', '~> 3.10'
13
+ gem 'rubocop', '~> 1.23'
14
+ gem 'rubocop-performance', '~> 1.12'
15
+ gem 'rubocop-rspec', '~> 2.6'
16
+ gem 'simplecov', '~> 0.21'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,99 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xtract-sdk (0.0.3)
5
+ http (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.1)
11
+ public_suffix (>= 2.0.2, < 6.0)
12
+ ast (2.4.2)
13
+ byebug (11.1.3)
14
+ diff-lcs (1.4.4)
15
+ docile (1.4.0)
16
+ domain_name (0.5.20190701)
17
+ unf (>= 0.0.5, < 1.0.0)
18
+ ffi (1.15.5)
19
+ ffi-compiler (1.0.1)
20
+ ffi (>= 1.0.0)
21
+ rake
22
+ http (5.1.1)
23
+ addressable (~> 2.8)
24
+ http-cookie (~> 1.0)
25
+ http-form_data (~> 2.2)
26
+ llhttp-ffi (~> 0.4.0)
27
+ http-cookie (1.0.5)
28
+ domain_name (~> 0.5)
29
+ http-form_data (2.3.0)
30
+ llhttp-ffi (0.4.0)
31
+ ffi-compiler (~> 1.0)
32
+ rake (~> 13.0)
33
+ parallel (1.21.0)
34
+ parser (3.0.2.0)
35
+ ast (~> 2.4.1)
36
+ public_suffix (5.0.1)
37
+ rainbow (3.0.0)
38
+ rake (13.0.6)
39
+ regexp_parser (2.1.1)
40
+ rexml (3.2.5)
41
+ rspec (3.10.0)
42
+ rspec-core (~> 3.10.0)
43
+ rspec-expectations (~> 3.10.0)
44
+ rspec-mocks (~> 3.10.0)
45
+ rspec-core (3.10.1)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-expectations (3.10.1)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.10.0)
50
+ rspec-mocks (3.10.2)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.10.0)
53
+ rspec-support (3.10.3)
54
+ rubocop (1.23.0)
55
+ parallel (~> 1.10)
56
+ parser (>= 3.0.0.0)
57
+ rainbow (>= 2.2.2, < 4.0)
58
+ regexp_parser (>= 1.8, < 3.0)
59
+ rexml
60
+ rubocop-ast (>= 1.12.0, < 2.0)
61
+ ruby-progressbar (~> 1.7)
62
+ unicode-display_width (>= 1.4.0, < 3.0)
63
+ rubocop-ast (1.13.0)
64
+ parser (>= 3.0.1.1)
65
+ rubocop-performance (1.12.0)
66
+ rubocop (>= 1.7.0, < 2.0)
67
+ rubocop-ast (>= 0.4.0)
68
+ rubocop-rspec (2.6.0)
69
+ rubocop (~> 1.19)
70
+ ruby-progressbar (1.11.0)
71
+ simplecov (0.21.2)
72
+ docile (~> 1.1)
73
+ simplecov-html (~> 0.11)
74
+ simplecov_json_formatter (~> 0.1)
75
+ simplecov-html (0.12.3)
76
+ simplecov_json_formatter (0.1.3)
77
+ unf (0.1.4)
78
+ unf_ext
79
+ unf_ext (0.0.8.2)
80
+ unicode-display_width (2.1.0)
81
+
82
+ PLATFORMS
83
+ universal-darwin-20
84
+ x86_64-darwin-21
85
+ x86_64-linux
86
+
87
+ DEPENDENCIES
88
+ bundler (~> 2.2)
89
+ byebug (~> 11.1)
90
+ rake (~> 13.0)
91
+ rspec (~> 3.10)
92
+ rubocop (~> 1.23)
93
+ rubocop-performance (~> 1.12)
94
+ rubocop-rspec (~> 2.6)
95
+ simplecov (~> 0.21)
96
+ xtract-sdk!
97
+
98
+ BUNDLED WITH
99
+ 2.4.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Xtract
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # XtractSDK
2
+
3
+ This gem is the official library provided by Xtract to interact with our API.
4
+
5
+ You can read the API docs directly from [here](https://xtractapi.docs.apiary.io).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'xtract-sdk'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $> bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $> gem install xtract-sdk
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ First you need to configure your api key obtained from the platform:
30
+
31
+ ```ruby
32
+ XtractSDK.configure do |config|
33
+ config.api_key = 'API_KEY'
34
+ end
35
+ ```
36
+
37
+ Also you can pass the key when creating the client directly:
38
+
39
+ ```ruby
40
+ client = XtractSDK::Client.new(api_key: 'API_KEY')
41
+ ```
42
+
43
+ Then you can create a client and start making requests. We provide a convenient way to do this:
44
+
45
+ ### Get invoices
46
+
47
+ ```ruby
48
+ client = XtractSDK::Client.new
49
+ invoices = client
50
+ .invoices
51
+ .client_tax_id('SOME_TAX_ID')
52
+ .call
53
+
54
+ invoices.each do |invoice|
55
+ puts invoice.id
56
+ puts invoice.products
57
+ end
58
+ ```
59
+
60
+ These are all the filtering options:
61
+
62
+ ```ruby
63
+ client_business_name
64
+ client_tax_id
65
+ provider_business_name
66
+ provider_tax_id
67
+ date_from
68
+ date_to
69
+ expiration_date_from
70
+ expiration_date_to
71
+ number
72
+ electronic_authorization_id
73
+ created_at_from
74
+ created_at_to
75
+ accounted ('pending', 'in_progress', 'completed', 'with_errors')
76
+ imputed (true, false)
77
+ ```
78
+
79
+ ### Get an invoice
80
+
81
+ ```ruby
82
+ client = XtractSDK::Client.new
83
+ invoice = client.invoice(ID)
84
+
85
+ puts invoice.id
86
+ puts invoice.products
87
+ ```
88
+
89
+ ### Update an invoice
90
+
91
+ ```ruby
92
+ client = XtractSDK::Client.new
93
+ client.update_invoice(ID, { accounted: 'completed' }) #=> :ok
94
+ ```
95
+
96
+ ### Errors
97
+
98
+ You can check [here](https://github.com/xtractapp/ruby-sdk/blob/master/lib/xtract_sdk/error.rb) all the error classes that the library raises.
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xtractapp/ruby-sdk.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'xtract_sdk'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require_relative 'invoices/request_builder'
2
+
3
+ module XtractSDK
4
+ class Client
5
+ def initialize(api_key: nil)
6
+ @api_key = api_key || XtractSDK.config[:api_key]
7
+ end
8
+
9
+ def invoices(params = {})
10
+ XtractSDK::Invoices::RequestBuilder.new(@api_key, params)
11
+ end
12
+
13
+ def invoice(id)
14
+ response = HTTPClient.invoice(id, @api_key)
15
+ Error.raise_from_code!(response.code) if response.code != 200
16
+ # TODO: use dry-rb structs instead of OpenStruct
17
+ JSON.parse(response.body.to_s, object_class: OpenStruct) # rubocop:disable Style/OpenStructUse
18
+ end
19
+
20
+ def update_invoice(id, options)
21
+ response = HTTPClient.update_invoice(id, options, @api_key)
22
+ Error.raise_from_code!(response.code) if response.code != 200
23
+ :ok
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module XtractSDK
2
+ module Error
3
+ module_function
4
+
5
+ def raise_from_code!(code)
6
+ case code
7
+ when 404 then raise NotFound
8
+ else
9
+ raise "Response code #{code} from API"
10
+ end
11
+ end
12
+
13
+ class NotFound < StandardError; end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ require 'http'
2
+
3
+ module XtractSDK
4
+ class HTTPClient
5
+ API_URL = 'https://api.xtract.app/api/v1'.freeze
6
+
7
+ class << self
8
+ def invoices(api_key, params)
9
+ HTTP.get(
10
+ "#{API_URL}/invoices",
11
+ params: params,
12
+ headers: build_headers(api_key)
13
+ )
14
+ end
15
+
16
+ def invoice(id, api_key)
17
+ HTTP.get(
18
+ "#{API_URL}/invoices/#{id}",
19
+ headers: build_headers(api_key)
20
+ )
21
+ end
22
+
23
+ def update_invoice(id, params, api_key)
24
+ HTTP.put(
25
+ "#{API_URL}/invoices/#{id}",
26
+ body: params.to_json,
27
+ headers: build_headers(api_key)
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def build_headers(api_key)
34
+ { 'Authorization' => api_key, 'Content-type' => 'application/json' }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'result'
2
+
3
+ module XtractSDK
4
+ module Invoices
5
+ class RequestBuilder
6
+ VALID_PARAMS = %i[
7
+ client_business_name
8
+ client_tax_id
9
+ provider_business_name
10
+ provider_tax_id
11
+ date_from
12
+ date_to
13
+ expiration_date_from
14
+ expiration_date_to
15
+ number
16
+ electronic_authorization_id
17
+ created_at_from
18
+ created_at_to
19
+ accounted
20
+ imputed
21
+ ].freeze
22
+
23
+ def initialize(api_key, options = {})
24
+ @api_key = api_key
25
+ @options = options
26
+ end
27
+
28
+ VALID_PARAMS.each do |param|
29
+ define_method param do |value = nil|
30
+ return @options[param] if value.nil?
31
+
32
+ @options[param] = value
33
+ self
34
+ end
35
+ end
36
+
37
+ def with_market_rate(from = nil, to = nil)
38
+ return @options[:with_market_rate] if from.nil? && to.nil?
39
+
40
+ @options[:with_market_rate] = "#{from}-#{to}"
41
+ self
42
+ end
43
+
44
+ def call
45
+ XtractSDK::Invoices::Result.new(@options, @api_key, HTTPClient)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,69 @@
1
+ require 'ostruct'
2
+
3
+ module XtractSDK
4
+ module Invoices
5
+ class Result
6
+ include Enumerable
7
+
8
+ BATCH_SIZE = 40
9
+
10
+ def initialize(options, api_key, client)
11
+ @options = options
12
+ @api_key = api_key
13
+ @client = client
14
+ reset_cursors!
15
+ end
16
+
17
+ def each
18
+ if block_given?
19
+ while (element = read_from_page_or_fetch)
20
+ yield element
21
+ end
22
+ reset_cursors!
23
+ else
24
+ to_enum(:each)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def reset_cursors!
31
+ @page = 0
32
+ @total_pages = 1
33
+ @page_cursor = BATCH_SIZE
34
+ @buffer = []
35
+ end
36
+
37
+ def read_from_page_or_fetch
38
+ fetch_data if @page_cursor == BATCH_SIZE
39
+
40
+ element = @buffer[@page_cursor]
41
+ return nil unless element
42
+
43
+ @page_cursor += 1
44
+ element
45
+ end
46
+
47
+ def fetch_data
48
+ response = make_request if @page < @total_pages
49
+ @buffer = parse_response(response)
50
+ @page_cursor = 0
51
+ end
52
+
53
+ def parse_response(response)
54
+ return [] if response.nil?
55
+
56
+ Error.raise_from_code!(response.code) if response.code != 200
57
+ # TODO: use dry-rb structs instead of OpenStruct
58
+ parsed = JSON.parse(response.body.to_s, object_class: OpenStruct) # rubocop:disable Style/OpenStructUse
59
+ @total_pages = parsed.total_pages
60
+ parsed.data
61
+ end
62
+
63
+ def make_request
64
+ @page += 1
65
+ @client.invoices(@api_key, @options.merge(page: @page, per_page: BATCH_SIZE))
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module XtractSDK
2
+ VERSION = '0.0.3'.freeze
3
+ end
data/lib/xtract_sdk.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'xtract_sdk/error'
2
+ require 'xtract_sdk/client'
3
+ require 'xtract_sdk/http_client'
4
+ require 'xtract_sdk/version'
5
+
6
+ module XtractSDK
7
+ @config = {
8
+ api_key: nil
9
+ }
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+
15
+ def self.api_key=(api_key)
16
+ @config[:api_key] = api_key
17
+ end
18
+
19
+ def self.config
20
+ @config
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'xtract_sdk/version'
4
+ require 'date'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'xtract-sdk'
8
+ spec.version = XtractSDK::VERSION
9
+ spec.authors = ['Alejandro Bezdjian']
10
+ spec.email = ['alejandro.bezdjian@klarna.com']
11
+ spec.summary = "SDK to connect with Xtract's API"
12
+ spec.description = "SDK to connect with Xtract's API"
13
+ spec.homepage = 'https://github.com/xtractapp/ruby-sdk'
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec)/}) }
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec)/})
17
+ spec.require_paths = ['lib']
18
+ spec.license = 'MIT'
19
+
20
+ spec.add_dependency 'http', '~> 5.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 2.2'
23
+ spec.add_development_dependency 'rake', '~> 13.0'
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xtract-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Alejandro Bezdjian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ description: SDK to connect with Xtract's API
56
+ email:
57
+ - alejandro.bezdjian@klarna.com
58
+ executables:
59
+ - console
60
+ - setup
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".rubocop.yml"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/xtract_sdk.rb
76
+ - lib/xtract_sdk/client.rb
77
+ - lib/xtract_sdk/error.rb
78
+ - lib/xtract_sdk/http_client.rb
79
+ - lib/xtract_sdk/invoices/request_builder.rb
80
+ - lib/xtract_sdk/invoices/result.rb
81
+ - lib/xtract_sdk/version.rb
82
+ - xtract_sdk.gemspec
83
+ homepage: https://github.com/xtractapp/ruby-sdk
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: '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: SDK to connect with Xtract's API
106
+ test_files: []