keinaufwand 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/keinaufwand/api/client/error.rb +11 -8
- data/lib/keinaufwand/api/client/list.rb +8 -16
- data/lib/keinaufwand/api/client/record.rb +15 -18
- data/lib/keinaufwand/api/client.rb +26 -28
- data/lib/keinaufwand/api/configuration.rb +4 -1
- data/lib/keinaufwand/api/schema.rb +2 -2
- data/lib/keinaufwand/api/version.rb +1 -1
- data/lib/keinaufwand/api.rb +18 -18
- data/lib/keinaufwand.rb +1 -1
- data/schemas/2026-07-02.json +16 -1
- metadata +4 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 85fc27f6be51f097cb90f5b363623015b061cdc49cea25114db66df76535d51f
|
|
4
|
+
data.tar.gz: 96784cf520136fde81e42fbeb224d69938d786e9e23359779e3c39e3ad51763e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3921df5370f13c87fd5e1dd51e4b54e64eabcd7b2481c25adad9981dac8f8d6a3e6460d3815cde33f25fbfe71fdccff91e7abf95af38cf9f83a3f6e04ad9f72d
|
|
7
|
+
data.tar.gz: 5f9e5f1abe8a6e92c5f96c2c06c1ccca639b215a508e57c53fd9c4f055ef3b2d7e3b71a42590aaffb8a18d1f72643d636368f46bff459c9dbe45c732e74fab9c
|
data/README.md
CHANGED
|
@@ -6,17 +6,20 @@ module Keinaufwand
|
|
|
6
6
|
|
|
7
7
|
def initialize(response)
|
|
8
8
|
@response = response
|
|
9
|
-
|
|
9
|
+
super(response.is_a?(String) ? response : message_from(response))
|
|
10
|
+
end
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
private
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
end
|
|
14
|
+
def message_from(response)
|
|
15
|
+
fallback = response.body.presence || "#{response.code} #{response.message}"
|
|
16
|
+
parsed_error_message(response.body) || fallback
|
|
17
|
+
end
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
def parsed_error_message(body)
|
|
20
|
+
JSON.parse(body).dig('error', 'message')
|
|
21
|
+
rescue JSON::ParserError, TypeError
|
|
22
|
+
nil
|
|
20
23
|
end
|
|
21
24
|
end
|
|
22
25
|
end
|
|
@@ -13,35 +13,27 @@ module Keinaufwand
|
|
|
13
13
|
@request_context = request_context
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def each(&
|
|
17
|
-
data.each(&
|
|
16
|
+
def each(&)
|
|
17
|
+
data.each(&)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
data.empty?
|
|
22
|
-
end
|
|
20
|
+
delegate :empty?, to: :data
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
data.size
|
|
26
|
-
end
|
|
22
|
+
delegate :size, to: :data
|
|
27
23
|
|
|
28
24
|
alias count size
|
|
29
25
|
alias length size
|
|
30
26
|
|
|
31
|
-
|
|
32
|
-
data.last
|
|
33
|
-
end
|
|
27
|
+
delegate :last, to: :data
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
data[index]
|
|
37
|
-
end
|
|
29
|
+
delegate :[], to: :data
|
|
38
30
|
|
|
39
31
|
def next
|
|
40
|
-
next_page = meta.dig(
|
|
32
|
+
next_page = meta.dig('pagination', 'next')
|
|
41
33
|
return unless next_page
|
|
42
34
|
|
|
43
35
|
current_page = @request_context.dig(:params, :page)
|
|
44
|
-
raise Error,
|
|
36
|
+
raise Error, 'Infinite pagination loop detected' if current_page == next_page
|
|
45
37
|
|
|
46
38
|
params = @request_context[:params].merge(page: next_page)
|
|
47
39
|
@client.send(:perform_request, @request_context[:verb], @request_context[:path], params, model_name: @request_context[:model_name])
|
|
@@ -16,22 +16,16 @@ module Keinaufwand
|
|
|
16
16
|
raw_attributes[:id]
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
raw_attributes[key]
|
|
21
|
-
end
|
|
19
|
+
delegate :[], to: :raw_attributes
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
raw_attributes[key] = value
|
|
25
|
-
end
|
|
21
|
+
delegate :[]=, to: :raw_attributes
|
|
26
22
|
|
|
27
|
-
def method_missing(method, *args, &
|
|
23
|
+
def method_missing(method, *args, &)
|
|
28
24
|
method_name = method.to_s
|
|
29
25
|
|
|
30
|
-
if raw_attributes.key?(method_name)
|
|
31
|
-
return read_value(method_name)
|
|
32
|
-
end
|
|
26
|
+
return read_value(method_name) if raw_attributes.key?(method_name)
|
|
33
27
|
|
|
34
|
-
return nil if args.empty? && !method_name.end_with?(
|
|
28
|
+
return nil if args.empty? && !method_name.end_with?('=')
|
|
35
29
|
|
|
36
30
|
super
|
|
37
31
|
end
|
|
@@ -44,16 +38,13 @@ module Keinaufwand
|
|
|
44
38
|
|
|
45
39
|
def read_value(name)
|
|
46
40
|
value = raw_attributes[name]
|
|
47
|
-
type = schema&.dig(
|
|
41
|
+
type = schema&.dig('attributes', name)
|
|
48
42
|
|
|
49
|
-
if client.schema.association?(model_name, name)
|
|
50
|
-
return build_association(name, value)
|
|
51
|
-
end
|
|
43
|
+
return build_association(name, value) if client.schema.association?(model_name, name)
|
|
52
44
|
|
|
53
45
|
case type.to_s
|
|
54
|
-
when
|
|
55
|
-
when
|
|
56
|
-
when "amount" then value
|
|
46
|
+
when 'datetime' then parse_time(value) || value
|
|
47
|
+
when 'date' then parse_date(value) || value
|
|
57
48
|
else value
|
|
58
49
|
end
|
|
59
50
|
end
|
|
@@ -75,6 +66,12 @@ module Keinaufwand
|
|
|
75
66
|
rescue ArgumentError
|
|
76
67
|
nil
|
|
77
68
|
end
|
|
69
|
+
|
|
70
|
+
def parse_date(value)
|
|
71
|
+
Date.parse(value.to_s) if value.present?
|
|
72
|
+
rescue Date::Error
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
78
75
|
end
|
|
79
76
|
end
|
|
80
77
|
end
|
|
@@ -3,10 +3,10 @@ module Keinaufwand
|
|
|
3
3
|
class Client
|
|
4
4
|
attr_reader :access_token, :endpoint, :version, :schema, :host
|
|
5
5
|
|
|
6
|
-
def initialize(access_token:, endpoint:
|
|
6
|
+
def initialize(access_token:, endpoint: 'https://api.keinaufwand.com', version: nil, schema: nil, host: nil)
|
|
7
7
|
@access_token = access_token
|
|
8
|
-
@endpoint = endpoint.chomp(
|
|
9
|
-
@version = version || Keinaufwand::Api.configuration.version
|
|
8
|
+
@endpoint = endpoint.chomp('/')
|
|
9
|
+
@version = version || Keinaufwand::Api.configuration.version
|
|
10
10
|
@host = host
|
|
11
11
|
@schema = schema || load_schema
|
|
12
12
|
define_methods_from_schema
|
|
@@ -30,10 +30,10 @@ module Keinaufwand
|
|
|
30
30
|
def define_methods_from_schema
|
|
31
31
|
schema.endpoints.each do |resource, endpoints|
|
|
32
32
|
endpoints.each do |endpoint_config|
|
|
33
|
-
path = endpoint_config.fetch(
|
|
34
|
-
verb = endpoint_config.fetch(
|
|
33
|
+
path = endpoint_config.fetch('path')
|
|
34
|
+
verb = endpoint_config.fetch('verb')
|
|
35
35
|
model_name = resource.singularize.classify
|
|
36
|
-
method_name = method_name_for(endpoint_config.fetch(
|
|
36
|
+
method_name = method_name_for(endpoint_config.fetch('action'), resource)
|
|
37
37
|
|
|
38
38
|
define_singleton_method(method_name) do |*args|
|
|
39
39
|
params = args.last.is_a?(Hash) ? args.pop : {}
|
|
@@ -52,28 +52,28 @@ module Keinaufwand
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
def method_name_for(action, resource)
|
|
55
|
-
prefix =
|
|
56
|
-
noun = action ==
|
|
57
|
-
"#{prefix}_#{noun}"
|
|
55
|
+
prefix = ['show', 'index'].include?(action) ? 'retrieve' : action
|
|
56
|
+
noun = action == 'index' ? resource.pluralize : resource.singularize
|
|
57
|
+
:"#{prefix}_#{noun}"
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def perform_request(verb, path, params = {}, model_name:)
|
|
61
61
|
payload = perform_json(verb, path, params)
|
|
62
|
-
data = payload.is_a?(Hash) && payload.key?(
|
|
62
|
+
data = payload.is_a?(Hash) && payload.key?('data') ? payload['data'] : payload
|
|
63
63
|
|
|
64
64
|
records = if data.is_a?(Array)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
data.map { |attributes| build_record(model_name, attributes) }
|
|
66
|
+
elsif data.is_a?(Hash)
|
|
67
|
+
build_record(model_name, data)
|
|
68
|
+
else
|
|
69
|
+
data
|
|
70
|
+
end
|
|
71
71
|
|
|
72
72
|
return records unless records.is_a?(Array)
|
|
73
73
|
|
|
74
74
|
List.new(
|
|
75
75
|
data: records,
|
|
76
|
-
meta: payload[
|
|
76
|
+
meta: payload['meta'],
|
|
77
77
|
client: self,
|
|
78
78
|
request_context: { verb: verb, path: path, params: params, model_name: model_name }
|
|
79
79
|
)
|
|
@@ -81,19 +81,17 @@ module Keinaufwand
|
|
|
81
81
|
|
|
82
82
|
def perform_json(verb, path, params)
|
|
83
83
|
uri = URI("#{endpoint}#{path}")
|
|
84
|
-
if verb ==
|
|
85
|
-
uri.query = encode_query(params)
|
|
86
|
-
end
|
|
84
|
+
uri.query = encode_query(params) if verb == 'GET' && params.present?
|
|
87
85
|
|
|
88
86
|
request = request_for(verb, uri)
|
|
89
|
-
request[
|
|
90
|
-
request[
|
|
91
|
-
request[
|
|
92
|
-
request[
|
|
93
|
-
request.body = JSON.generate(params) if verb !=
|
|
87
|
+
request['Authorization'] = "Token #{access_token}"
|
|
88
|
+
request['Accept'] = 'application/json'
|
|
89
|
+
request['Content-Type'] = 'application/json'
|
|
90
|
+
request['Host'] = host if host
|
|
91
|
+
request.body = JSON.generate(params) if verb != 'GET' && params.present?
|
|
94
92
|
|
|
95
|
-
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme ==
|
|
96
|
-
raise Error
|
|
93
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(request) }
|
|
94
|
+
raise Error, response unless response.is_a?(Net::HTTPSuccess)
|
|
97
95
|
|
|
98
96
|
JSON.parse(response.body)
|
|
99
97
|
end
|
|
@@ -112,7 +110,7 @@ module Keinaufwand
|
|
|
112
110
|
else
|
|
113
111
|
"#{URI.encode_www_form_component(full_key)}=#{URI.encode_www_form_component(value.to_s)}"
|
|
114
112
|
end
|
|
115
|
-
end.join(
|
|
113
|
+
end.join('&')
|
|
116
114
|
end
|
|
117
115
|
end
|
|
118
116
|
end
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
module Keinaufwand
|
|
2
2
|
module Api
|
|
3
3
|
class Configuration
|
|
4
|
+
# The API version clients use unless the configuration names one.
|
|
5
|
+
DEFAULT_VERSION = '2026-07-02'.freeze
|
|
6
|
+
|
|
4
7
|
attr_accessor :path
|
|
5
8
|
|
|
6
9
|
def initialize(path = nil)
|
|
@@ -12,7 +15,7 @@ module Keinaufwand
|
|
|
12
15
|
end
|
|
13
16
|
|
|
14
17
|
def version
|
|
15
|
-
values.dig(:api, :version) || values[:version]
|
|
18
|
+
values.dig(:api, :version) || values[:version] || DEFAULT_VERSION
|
|
16
19
|
end
|
|
17
20
|
|
|
18
21
|
def schema_path(version = self.version)
|
|
@@ -29,7 +29,7 @@ module Keinaufwand
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def association_type?(type)
|
|
32
|
-
|
|
32
|
+
['association', 'has_one', 'has_many', 'belongs_to', ':has_one', ':has_many', ':belongs_to'].include?(type.to_s)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def attribute_type(model_name, attribute)
|
|
@@ -48,7 +48,7 @@ module Keinaufwand
|
|
|
48
48
|
|
|
49
49
|
def association_collection?(model_name, attribute)
|
|
50
50
|
type = association_metadata(model_name, attribute)[:type] || attribute_type(model_name, attribute)
|
|
51
|
-
type.to_s.include?(
|
|
51
|
+
type.to_s.include?('many')
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
end
|
data/lib/keinaufwand/api.rb
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require
|
|
7
|
-
require
|
|
8
|
-
require
|
|
9
|
-
require
|
|
10
|
-
require
|
|
1
|
+
require 'active_support'
|
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
|
3
|
+
require 'active_support/core_ext/object/blank'
|
|
4
|
+
require 'active_support/inflector'
|
|
5
|
+
require 'active_support/core_ext/module/delegation'
|
|
6
|
+
require 'json'
|
|
7
|
+
require 'net/http'
|
|
8
|
+
require 'time'
|
|
9
|
+
require 'uri'
|
|
10
|
+
require 'yaml'
|
|
11
11
|
|
|
12
|
-
require_relative
|
|
13
|
-
require_relative
|
|
14
|
-
require_relative
|
|
15
|
-
require_relative
|
|
16
|
-
require_relative
|
|
17
|
-
require_relative
|
|
18
|
-
require_relative
|
|
12
|
+
require_relative 'api/version'
|
|
13
|
+
require_relative 'api/configuration'
|
|
14
|
+
require_relative 'api/schema'
|
|
15
|
+
require_relative 'api/client/error'
|
|
16
|
+
require_relative 'api/client/list'
|
|
17
|
+
require_relative 'api/client/record'
|
|
18
|
+
require_relative 'api/client'
|
|
19
19
|
|
|
20
20
|
module Keinaufwand
|
|
21
21
|
module Api
|
|
@@ -31,7 +31,7 @@ module Keinaufwand
|
|
|
31
31
|
private
|
|
32
32
|
|
|
33
33
|
def default_config_path
|
|
34
|
-
Rails.root.join(
|
|
34
|
+
Rails.root.join('config/keinaufwand.yml') if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
end
|
data/lib/keinaufwand.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
require_relative
|
|
1
|
+
require_relative 'keinaufwand/api'
|
data/schemas/2026-07-02.json
CHANGED
|
@@ -66,6 +66,16 @@
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
|
+
"EventCategory": {
|
|
70
|
+
"resource_name": "event_categories",
|
|
71
|
+
"attributes": {
|
|
72
|
+
"id": "integer",
|
|
73
|
+
"name": "virtual",
|
|
74
|
+
"position": "virtual",
|
|
75
|
+
"created_at": "datetime",
|
|
76
|
+
"updated_at": "datetime"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
69
79
|
"EventDetail": {
|
|
70
80
|
"resource_name": "event_details",
|
|
71
81
|
"attributes": {
|
|
@@ -74,13 +84,18 @@
|
|
|
74
84
|
"body": "virtual",
|
|
75
85
|
"image_url": "virtual",
|
|
76
86
|
"slug": "virtual",
|
|
77
|
-
"
|
|
87
|
+
"event_category_id": "integer",
|
|
78
88
|
"product_ids": "integer_array",
|
|
79
89
|
"position": "virtual",
|
|
80
90
|
"created_at": "datetime",
|
|
81
91
|
"updated_at": "datetime"
|
|
82
92
|
},
|
|
83
93
|
"associations": {
|
|
94
|
+
"event_category": {
|
|
95
|
+
"type": "belongs_to",
|
|
96
|
+
"class_name": "EventCategory",
|
|
97
|
+
"inline": true
|
|
98
|
+
},
|
|
84
99
|
"products": {
|
|
85
100
|
"type": "has_and_belongs_to_many",
|
|
86
101
|
"class_name": "Product",
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: keinaufwand
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Codegestalt
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -24,8 +23,6 @@ dependencies:
|
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '7.2'
|
|
27
|
-
description:
|
|
28
|
-
email:
|
|
29
26
|
executables: []
|
|
30
27
|
extensions: []
|
|
31
28
|
extra_rdoc_files: []
|
|
@@ -45,7 +42,7 @@ homepage: https://keinaufwand.com
|
|
|
45
42
|
licenses: []
|
|
46
43
|
metadata:
|
|
47
44
|
allowed_push_host: https://rubygems.org
|
|
48
|
-
|
|
45
|
+
rubygems_mfa_required: 'true'
|
|
49
46
|
rdoc_options: []
|
|
50
47
|
require_paths:
|
|
51
48
|
- lib
|
|
@@ -60,8 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
60
57
|
- !ruby/object:Gem::Version
|
|
61
58
|
version: '0'
|
|
62
59
|
requirements: []
|
|
63
|
-
rubygems_version:
|
|
64
|
-
signing_key:
|
|
60
|
+
rubygems_version: 4.0.20
|
|
65
61
|
specification_version: 4
|
|
66
62
|
summary: Schema-aware Keinaufwand API client
|
|
67
63
|
test_files: []
|