xpensify-sdk 0.0.1

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: 5954814ad4593baa0ef39565b18b6d50fdd4a7661d995b67acccdd2a8bc1f759
4
+ data.tar.gz: 297a199973fa552b0aaf072fd3b89345bc3f39e57be50ae765401b7285f28e8f
5
+ SHA512:
6
+ metadata.gz: 21371daec0bf8daaaac79a33255589deaacc36f47de711b28c3d0b92d2115d80f6eeb4b668acc33127bf9c029f06aec7e8c3ec7caac2f4084b7c10e4d5fcde19
7
+ data.tar.gz: 84cb4848dd2ceaf37a12521099842300871e1b6c3b114e38f75749c31729dfba21cf702680aadc69d7f31586c0139f85c030b362ce806d3351ac807ebaf0adb7
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
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/xpensify_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,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xpensify_sdk (0.0.1)
5
+ http (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.0)
11
+ public_suffix (>= 2.0.2, < 5.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.4)
19
+ ffi-compiler (1.0.1)
20
+ ffi (>= 1.0.0)
21
+ rake
22
+ http (5.0.4)
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.4)
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 (4.0.6)
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)
80
+ unicode-display_width (2.1.0)
81
+
82
+ PLATFORMS
83
+ universal-darwin-20
84
+
85
+ DEPENDENCIES
86
+ bundler (~> 2.2)
87
+ byebug (~> 11.1)
88
+ rake (~> 13.0)
89
+ rspec (~> 3.10)
90
+ rubocop (~> 1.23)
91
+ rubocop-performance (~> 1.12)
92
+ rubocop-rspec (~> 2.6)
93
+ simplecov (~> 0.21)
94
+ xpensify_sdk!
95
+
96
+ BUNDLED WITH
97
+ 2.2.31
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # XpensifySDK
2
+
3
+ This gem is the official library provided by Xpensify to interact with our API.
4
+
5
+ You can read the API docs directly from [here](https://xpensifyapi.docs.apiary.io).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'xpensify_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 xpensify_sdk
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ First you need to configure your api key obtained from the platform:
30
+
31
+ ```ruby
32
+ XpensifySDK.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 = XpensifySDK::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 = XpensifySDK::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 = XpensifySDK::Client.new
83
+ invoice = client.invoice(ID)
84
+
85
+ puts invoice.id
86
+ puts invoice.products
87
+ ```
88
+
89
+ ### Errors
90
+
91
+ You can check [here](https://github.com/Xpensify/ruby-sdk/blob/master/lib/xpensify_sdk/error.rb) all the error classes that the library raises.
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Xpensify/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 'xpensify_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,20 @@
1
+ require_relative 'invoices/request_builder'
2
+
3
+ module XpensifySDK
4
+ class Client
5
+ def initialize(api_key: nil)
6
+ @api_key = api_key || XpensifySDK.config[:api_key]
7
+ end
8
+
9
+ def invoices(params = {})
10
+ XpensifySDK::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
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module XpensifySDK
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,30 @@
1
+ require 'http'
2
+
3
+ module XpensifySDK
4
+ class HTTPClient
5
+ API_URL = 'https://api.xpensify.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
+ private
24
+
25
+ def build_headers(api_key)
26
+ { 'Authorization' => api_key }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'result'
2
+
3
+ module XpensifySDK
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 call
38
+ XpensifySDK::Invoices::Result.new(@options, @api_key, HTTPClient)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,69 @@
1
+ require 'ostruct'
2
+
3
+ module XpensifySDK
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 XpensifySDK
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'xpensify_sdk/error'
2
+ require 'xpensify_sdk/client'
3
+ require 'xpensify_sdk/http_client'
4
+ require 'xpensify_sdk/version'
5
+
6
+ module XpensifySDK
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 'xpensify_sdk/version'
4
+ require 'date'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'xpensify-sdk'
8
+ spec.version = XpensifySDK::VERSION
9
+ spec.authors = ['Alejandro Bezdjian']
10
+ spec.email = ['alejandro.bezdjian@klarna.com']
11
+ spec.summary = "SDK to connect with Xpensify's API"
12
+ spec.description = "SDK to connect with Xpensify's API"
13
+ spec.homepage = 'https://github.com/Xpensify/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,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xpensify-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alejandro Bezdjian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-18 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 Xpensify'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
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/xpensify_sdk.rb
75
+ - lib/xpensify_sdk/client.rb
76
+ - lib/xpensify_sdk/error.rb
77
+ - lib/xpensify_sdk/http_client.rb
78
+ - lib/xpensify_sdk/invoices/request_builder.rb
79
+ - lib/xpensify_sdk/invoices/result.rb
80
+ - lib/xpensify_sdk/version.rb
81
+ - xpensify_sdk.gemspec
82
+ homepage: https://github.com/Xpensify/ruby-sdk
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: SDK to connect with Xpensify's API
105
+ test_files: []