pennylane 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +54 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +16 -0
- data/lib/pennylane/client.rb +73 -0
- data/lib/pennylane/configuration.rb +6 -0
- data/lib/pennylane/list_object.rb +26 -0
- data/lib/pennylane/object.rb +101 -0
- data/lib/pennylane/resources/base.rb +52 -0
- data/lib/pennylane/resources/customer.rb +21 -0
- data/lib/pennylane/util.rb +95 -0
- data/lib/pennylane/version.rb +5 -0
- data/lib/pennylane.rb +34 -0
- data/pennylane.gemspec +38 -0
- data/sig/pennylane.rbs +4 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da1842f4d3dee5f1bd7c9bccd793eb1c185cb5fece2178e967da0cbec3b752be
|
4
|
+
data.tar.gz: 1f050f189dae434bdf4febe2c7715858b1f422273a129aa6654677c95ba94921
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c245562b9cdcfc4754348d1a43d2029d5b6d025cd61191c0ceeb3e7b814ed81bae299cc7e86b76799a11275fc296a4e5fd8080ab133f262981ccf9091fad665f
|
7
|
+
data.tar.gz: 0404a595eedc597bc04c2dafa9db4796e1ab9d6a12a0bfc9f28132195b50f0ebe8516552c5de04326ed1d79cfef97a2d07de0e7b30ce6fd188abc991e07a221c
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 159814+sbounmy@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pennylane (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
json (2.7.2)
|
11
|
+
language_server-protocol (3.17.0.3)
|
12
|
+
minitest (5.22.3)
|
13
|
+
parallel (1.24.0)
|
14
|
+
parser (3.3.0.5)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
racc
|
17
|
+
power_assert (2.0.3)
|
18
|
+
racc (1.7.3)
|
19
|
+
rainbow (3.1.1)
|
20
|
+
rake (13.2.0)
|
21
|
+
regexp_parser (2.9.0)
|
22
|
+
rexml (3.2.6)
|
23
|
+
rubocop (1.62.1)
|
24
|
+
json (~> 2.3)
|
25
|
+
language_server-protocol (>= 3.17.0)
|
26
|
+
parallel (~> 1.10)
|
27
|
+
parser (>= 3.3.0.2)
|
28
|
+
rainbow (>= 2.2.2, < 4.0)
|
29
|
+
regexp_parser (>= 1.8, < 3.0)
|
30
|
+
rexml (>= 3.2.5, < 4.0)
|
31
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
32
|
+
ruby-progressbar (~> 1.7)
|
33
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
34
|
+
rubocop-ast (1.31.2)
|
35
|
+
parser (>= 3.3.0.4)
|
36
|
+
ruby-progressbar (1.13.0)
|
37
|
+
test-unit (3.6.2)
|
38
|
+
power_assert
|
39
|
+
unicode-display_width (2.5.0)
|
40
|
+
vcr (6.2.0)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
arm64-darwin-22
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
minitest (~> 5.22)
|
47
|
+
pennylane!
|
48
|
+
rake (~> 13.0)
|
49
|
+
rubocop (~> 1.21)
|
50
|
+
test-unit (~> 3.0)
|
51
|
+
vcr (~> 6.2)
|
52
|
+
|
53
|
+
BUNDLED WITH
|
54
|
+
2.4.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Stephane Bounmy
|
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,63 @@
|
|
1
|
+
# Pennylane Ruby Library
|
2
|
+
|
3
|
+
The Pennylane Ruby library provides convenient access to the Pennylane API from applications written in the Ruby language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the Pennylane API.
|
4
|
+
It only works with the [buys and sales API](https://pennylane.readme.io/reference/versioning).
|
5
|
+
|
6
|
+
It was inspired by the [Stripe Ruby library](https://github.com/stripe/stripe-ruby).
|
7
|
+
|
8
|
+
|
9
|
+
## Documentation
|
10
|
+
See the [Pennylane API](https://pennylane.readme.io/reference/versioning) docs.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Install the gem and add to the application's Gemfile by executing:
|
15
|
+
|
16
|
+
$ bundle add pennylane
|
17
|
+
|
18
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
19
|
+
|
20
|
+
$ gem install pennylane
|
21
|
+
|
22
|
+
## Requirements
|
23
|
+
Ruby 2.3+.
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
The library needs to be configured with your account's token api which is available in your Pennylane Settings. Set `Pennylane.api_key` to its value:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'pennylane'
|
31
|
+
Pennylane.api_key = 'x0fd....'
|
32
|
+
|
33
|
+
# list customers
|
34
|
+
Pennylane::Customer.list
|
35
|
+
|
36
|
+
# filter and paginate customers
|
37
|
+
Pennylane::Customer.list(filter: [{field: 'name', operator: 'eq', value: 'Apple'}], page: 2)
|
38
|
+
|
39
|
+
# Retrieve single customer
|
40
|
+
Pennylane::Customer.retrieve('38a1f19a-256d-4692-a8fe-0a16403f59ff')
|
41
|
+
```
|
42
|
+
|
43
|
+
## Test mode
|
44
|
+
Pennylane provide a [test environment](https://help.pennylane.com/fr/articles/18773-creer-un-environnement-de-test). You can use the library with your test token api by setting the `Pennylane.api_key` to its value.
|
45
|
+
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sbounmy/pennylane. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/pennylane/blob/main/CODE_OF_CONDUCT.md).
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
60
|
+
|
61
|
+
## Code of Conduct
|
62
|
+
|
63
|
+
Everyone interacting in the Pennylane project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pennylane/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Pennylane
|
2
|
+
class Client
|
3
|
+
BASE_URI = 'app.pennylane.com/api/external'.freeze
|
4
|
+
VERSION = 'v1'.freeze
|
5
|
+
|
6
|
+
attr_accessor :key, :version
|
7
|
+
|
8
|
+
def initialize(key, version: 'v1')
|
9
|
+
@key = key
|
10
|
+
@version = version
|
11
|
+
end
|
12
|
+
|
13
|
+
def url(path, query={})
|
14
|
+
URI("https://#{BASE_URI}/#{VERSION}#{path}").tap do |uri|
|
15
|
+
uri.query = URI.encode_www_form(query) if query
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def base_uri
|
20
|
+
URI("https://#{BASE_URI}")
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def authorization
|
25
|
+
"Bearer #{key}"
|
26
|
+
end
|
27
|
+
def http
|
28
|
+
Net::HTTP.new(base_uri.host, base_uri.port).tap do |http|
|
29
|
+
http.use_ssl = true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def klass(method)
|
34
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
35
|
+
end
|
36
|
+
|
37
|
+
def request method, path, params:, opts: {}
|
38
|
+
req = initialize_request(method, path, params[:query]).tap do |req|
|
39
|
+
req.body = params[:body].to_json if params[:body] && method != :get
|
40
|
+
# req.query = params if !params.empty? && method == :get
|
41
|
+
end
|
42
|
+
|
43
|
+
http.request(req).tap do |resp|
|
44
|
+
handle_error_response(resp) if should_handle_as_error?(resp.code)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def handle_error_response(resp)
|
51
|
+
case resp.code.to_i
|
52
|
+
when 401
|
53
|
+
raise Pennylane::AuthenticationError, resp.body
|
54
|
+
when 404
|
55
|
+
raise Pennylane::NotFoundError
|
56
|
+
else
|
57
|
+
error = JSON.parse(resp.read_body)
|
58
|
+
raise Pennylane::Error, "#{resp.code} - #{error['message'] || error['error']}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def should_handle_as_error?(code)
|
63
|
+
code.to_i >= 400
|
64
|
+
end
|
65
|
+
def initialize_request method=nil, path=nil, params={}
|
66
|
+
klass(method).new(url(path, params)).tap do |request|
|
67
|
+
request["content-type"] = 'application/json'
|
68
|
+
request["authorization"] = authorization
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Pennylane
|
2
|
+
class ListObject < Pennylane::Object
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def self.object_name
|
6
|
+
'list'
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.build_from(response, params = {}, opts = {})
|
10
|
+
new.initialize_from_response(response)
|
11
|
+
end
|
12
|
+
|
13
|
+
def each(&blk)
|
14
|
+
data.each(&blk)
|
15
|
+
end
|
16
|
+
|
17
|
+
def data
|
18
|
+
@values[key_for(@values)]
|
19
|
+
end
|
20
|
+
|
21
|
+
def key_for(resp)
|
22
|
+
resp.keys.find { |k| Pennylane::API_RESOURCES.keys.include?(Util.singularize(k.to_s)) } || resp.keys.find { |k| resp[k].is_a? Array }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Pennylane
|
2
|
+
class Object
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(id = nil)
|
6
|
+
@id = id
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize_from_response(response, params = {}, opts = {})
|
10
|
+
values = Util.symbolize_names(response)
|
11
|
+
@values = self.class.send :deep_copy, values
|
12
|
+
add_accessors(@values.keys, @values)
|
13
|
+
@opts = opts
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.protected_fields
|
18
|
+
[]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.build_from(response, params = {}, opts = {})
|
22
|
+
new(response['id']).initialize_from_response(response, params, opts)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.objects
|
26
|
+
{}.tap do |h|
|
27
|
+
descendants.each do |klass|
|
28
|
+
h[klass.object_name] = klass
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.descendants
|
34
|
+
ObjectSpace.each_object(Class).select { |klass| klass < self && respond_to?(:object_name) }
|
35
|
+
end
|
36
|
+
|
37
|
+
# Eigenclass to define methods on the specific class
|
38
|
+
protected def metaclass
|
39
|
+
class << self; self; end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected def add_accessors(keys, values)
|
43
|
+
# not available in the #instance_eval below
|
44
|
+
protected_fields = self.class.protected_fields
|
45
|
+
|
46
|
+
metaclass.instance_eval do
|
47
|
+
keys.each do |k|
|
48
|
+
# next if RESERVED_FIELD_NAMES.include?(k)
|
49
|
+
# next if protected_fields.include?(k)
|
50
|
+
# next if @@permanent_attributes.include?(k)
|
51
|
+
|
52
|
+
if k == :method
|
53
|
+
# Object#method is a built-in Ruby method that accepts a symbol
|
54
|
+
# and returns the corresponding Method object. Because the API may
|
55
|
+
# also use `method` as a field name, we check the arity of *args
|
56
|
+
# to decide whether to act as a getter or call the parent method.
|
57
|
+
define_method(k) { |*args| args.empty? ? @values[k] : super(*args) }
|
58
|
+
else
|
59
|
+
define_method(k) { @values[k] }
|
60
|
+
end
|
61
|
+
|
62
|
+
define_method(:"#{k}=") do |v|
|
63
|
+
if v == ""
|
64
|
+
raise ArgumentError, "You cannot set #{k} to an empty string. " \
|
65
|
+
"We interpret empty strings as nil in requests. " \
|
66
|
+
"You may set (object).#{k} = nil to delete the property."
|
67
|
+
end
|
68
|
+
@values[k] = Util.convert_to_pennylane_object(v, @opts)
|
69
|
+
dirty_value!(@values[k])
|
70
|
+
@unsaved_values.add(k)
|
71
|
+
end
|
72
|
+
|
73
|
+
define_method(:"#{k}?") { @values[k] } if [FalseClass, TrueClass].include?(values[k].class)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Produces a deep copy of the given object including support for arrays,
|
79
|
+
# hashes, and Pennylane:Object.
|
80
|
+
private_class_method def self.deep_copy(obj)
|
81
|
+
case obj
|
82
|
+
when Array
|
83
|
+
obj.map { |e| deep_copy(e) }
|
84
|
+
when Hash
|
85
|
+
obj.each_with_object({}) do |(k, v), copy|
|
86
|
+
copy[k] = deep_copy(v)
|
87
|
+
copy
|
88
|
+
end
|
89
|
+
when Pennylane::Object
|
90
|
+
obj.class.build_from(
|
91
|
+
deep_copy(obj.instance_variable_get(:@values)),
|
92
|
+
obj.instance_variable_get(:@opts).select do |k, _v|
|
93
|
+
Util::OPTS_COPYABLE.include?(k)
|
94
|
+
end
|
95
|
+
)
|
96
|
+
else
|
97
|
+
obj
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Pennylane
|
2
|
+
module Resources
|
3
|
+
class Base < Pennylane::Object
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def object_name
|
7
|
+
name&.split('::')&.last&.downcase
|
8
|
+
end
|
9
|
+
|
10
|
+
def object_name_plural
|
11
|
+
"#{object_name}s"
|
12
|
+
end
|
13
|
+
|
14
|
+
def request_pennylane_object(method:, path:, params: {}, opts: {}, usage: [])
|
15
|
+
resp, opts = execute_resource_request(method, path, params, opts, usage)
|
16
|
+
Util.convert_to_pennylane_object(Util.normalize_response(resp), params, opts)
|
17
|
+
end
|
18
|
+
|
19
|
+
def execute_resource_request(method, path, params = {}, opts = {}, usage = [])
|
20
|
+
api_key = opts.delete(:api_key) || Pennylane.api_key
|
21
|
+
|
22
|
+
|
23
|
+
resp = client.request(
|
24
|
+
method,
|
25
|
+
path,
|
26
|
+
params: params,
|
27
|
+
opts: opts
|
28
|
+
)
|
29
|
+
|
30
|
+
[JSON.parse(resp.read_body), opts]
|
31
|
+
end
|
32
|
+
|
33
|
+
def client
|
34
|
+
@client ||= Pennylane::Client.new(Pennylane.api_key)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# So we can call directly method on the object rather than going through his key
|
39
|
+
# Pennylane::Customer.retrieve('any-id').name == Pennylane::Customer.retrieve('any-id').customer.name
|
40
|
+
def method_missing(method_name, *args, &block)
|
41
|
+
obj = @values[self.class.object_name.to_sym]
|
42
|
+
raise NoMethodError, "undefined method `#{method_name}` for #{self.class}" unless obj
|
43
|
+
obj.send(method_name, *args, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def respond_to_missing?(method_name, include_private = false)
|
47
|
+
@values[self.class.object_name.to_sym].respond_to?(method_name) || super
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Pennylane
|
2
|
+
class Customer < Resources::Base
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def list filters = {}, opts = {}
|
7
|
+
request_pennylane_object(method: :get, path: "/customers", params: { query: filters }, opts: opts)
|
8
|
+
end
|
9
|
+
|
10
|
+
def retrieve customer_id, opts = {}
|
11
|
+
request_pennylane_object(method: :get, path: "/customers/#{customer_id}", params: {}, opts: opts)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create params, opts = {}
|
15
|
+
request_pennylane_object(method: :post, path: "/customers", params: { body: { customer: params } }, opts: opts)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Pennylane
|
2
|
+
class Util
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def convert_to_pennylane_object(resp, params={}, opts={})
|
6
|
+
case resp
|
7
|
+
when Array
|
8
|
+
resp.map { |value| convert_to_pennylane_object(value, params, opts) }
|
9
|
+
when Hash
|
10
|
+
resp.each do |key, value|
|
11
|
+
resp[key] = convert_to_pennylane_object(value, params, opts)
|
12
|
+
end
|
13
|
+
klass_for(resp['_object']).build_from(resp, params, opts)
|
14
|
+
else
|
15
|
+
resp
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# This method is used to normalize the response from the API
|
20
|
+
# It will add an _object key to each hash in the response
|
21
|
+
# This key will contain the name of the object
|
22
|
+
# It will also add an _object key to the root of the response
|
23
|
+
def normalize_response(object)
|
24
|
+
# puts object.inspect
|
25
|
+
case object
|
26
|
+
when Hash
|
27
|
+
new_hash = {}
|
28
|
+
new_hash['_object'] = object.has_key?('total_pages') ? 'list' : singularize(object.keys.first)
|
29
|
+
|
30
|
+
object.each do |key, value|
|
31
|
+
if value.is_a? Array
|
32
|
+
new_hash[key] = value.map do |h|
|
33
|
+
h['_object'] = singularize(key) if h.is_a? Hash
|
34
|
+
normalize_response(h)
|
35
|
+
end
|
36
|
+
elsif value.is_a? Hash
|
37
|
+
value['_object'] = singularize(key)
|
38
|
+
end
|
39
|
+
new_hash[key] = normalize_response(value)
|
40
|
+
end
|
41
|
+
new_hash
|
42
|
+
when Array
|
43
|
+
object.map do |value|
|
44
|
+
normalize_response(value)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
object
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# This method is used to convert the keys of a hash from strings to symbols
|
52
|
+
def symbolize_names(object)
|
53
|
+
case object
|
54
|
+
when Hash
|
55
|
+
new_hash = {}
|
56
|
+
object.each do |key, value|
|
57
|
+
key = (begin
|
58
|
+
key.to_sym
|
59
|
+
rescue StandardError
|
60
|
+
key
|
61
|
+
end) || key
|
62
|
+
new_hash[key] = symbolize_names(value)
|
63
|
+
end
|
64
|
+
new_hash
|
65
|
+
when Array
|
66
|
+
object.map { |value| symbolize_names(value) }
|
67
|
+
else
|
68
|
+
object
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def klass_for(object)
|
73
|
+
Pennylane::API_RESOURCES[singularize(object)] || Pennylane::Object
|
74
|
+
rescue
|
75
|
+
Pennylane::Object
|
76
|
+
end
|
77
|
+
|
78
|
+
# We define our own singularize method because the ActiveSupport one is too heavy for this use case
|
79
|
+
def singularize(word)
|
80
|
+
return word if word.empty?
|
81
|
+
|
82
|
+
# Basic rules: this is far from comprehensive and handles only simple and common cases
|
83
|
+
if word.end_with?('ies')
|
84
|
+
word.sub(/ies$/, 'y')
|
85
|
+
elsif word.end_with?('ss') # so `address` dont become `addres`
|
86
|
+
word
|
87
|
+
elsif word.end_with?('s')
|
88
|
+
word.chomp('s')
|
89
|
+
else
|
90
|
+
word
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/pennylane.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "pennylane/version"
|
4
|
+
require 'pennylane/configuration'
|
5
|
+
require 'pennylane/object'
|
6
|
+
require 'pennylane/list_object'
|
7
|
+
require 'pennylane/util'
|
8
|
+
require 'pennylane/client'
|
9
|
+
require 'forwardable'
|
10
|
+
# require 'ostruct'
|
11
|
+
require 'uri'
|
12
|
+
require 'net/http'
|
13
|
+
|
14
|
+
Dir["./lib/pennylane/resources/*.rb"].each {|file| require file }
|
15
|
+
|
16
|
+
|
17
|
+
module Pennylane
|
18
|
+
class Error < StandardError; end
|
19
|
+
class AuthenticationError < Error; end
|
20
|
+
class ConfigurationError < Error; end
|
21
|
+
class NotFoundError < Error; end
|
22
|
+
|
23
|
+
API_RESOURCES = {
|
24
|
+
ListObject.object_name => ListObject,
|
25
|
+
Customer.object_name => Customer
|
26
|
+
}.freeze
|
27
|
+
|
28
|
+
@config = Pennylane::Configuration.new
|
29
|
+
# So we can have a module Pennylane that can be a class as well Pennylane.api_key = '1234'
|
30
|
+
class << self
|
31
|
+
extend Forwardable
|
32
|
+
def_delegators :@config, :api_key, :api_key=
|
33
|
+
end
|
34
|
+
end
|
data/pennylane.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/pennylane/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "pennylane"
|
7
|
+
spec.version = Pennylane::VERSION
|
8
|
+
spec.authors = ["Stephane Bounmy"]
|
9
|
+
spec.email = ["159814+sbounmy@users.noreply.github.com"]
|
10
|
+
|
11
|
+
spec.summary = "Ruby bindings for the Pennylane API"
|
12
|
+
spec.description = "Pennylane offers integrated financial management and accounting tools for businesses. See https://pennylane.com for details."
|
13
|
+
spec.homepage = "https://rubygems.org/gems/pennylane"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/sbounmy/pennylane"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/sbounmy/pennylane/blob/master/CHANGELOG.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
spec.add_development_dependency "vcr", "~> 6.2"
|
35
|
+
spec.add_development_dependency "test-unit", "~> 3.6"
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.22"
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
data/sig/pennylane.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pennylane
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephane Bounmy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: vcr
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test-unit
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.22'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.22'
|
55
|
+
description: Pennylane offers integrated financial management and accounting tools
|
56
|
+
for businesses. See https://pennylane.com for details.
|
57
|
+
email:
|
58
|
+
- 159814+sbounmy@users.noreply.github.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- lib/pennylane.rb
|
72
|
+
- lib/pennylane/client.rb
|
73
|
+
- lib/pennylane/configuration.rb
|
74
|
+
- lib/pennylane/list_object.rb
|
75
|
+
- lib/pennylane/object.rb
|
76
|
+
- lib/pennylane/resources/base.rb
|
77
|
+
- lib/pennylane/resources/customer.rb
|
78
|
+
- lib/pennylane/util.rb
|
79
|
+
- lib/pennylane/version.rb
|
80
|
+
- pennylane.gemspec
|
81
|
+
- sig/pennylane.rbs
|
82
|
+
homepage: https://rubygems.org/gems/pennylane
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata:
|
86
|
+
homepage_uri: https://rubygems.org/gems/pennylane
|
87
|
+
source_code_uri: https://github.com/sbounmy/pennylane
|
88
|
+
changelog_uri: https://github.com/sbounmy/pennylane/blob/master/CHANGELOG.md
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 2.6.0
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.4.10
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Ruby bindings for the Pennylane API
|
108
|
+
test_files: []
|