pdc 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.pronto.yml +13 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +13 -2
- data/Gemfile +16 -10
- data/Rakefile +4 -4
- data/bin/console +5 -5
- data/examples/logger.rb +14 -12
- data/examples/pdc_curb_access_token.rb +1 -1
- data/examples/pdc_release_rpm_mapping.rb +16 -0
- data/examples/pdc_resource_tests.rb +22 -0
- data/examples/pdc_test_cache.rb +9 -8
- data/examples/prod_failures.rb +1 -1
- data/examples/prod_pdc.rb +1 -1
- data/lib/pdc/base.rb +2 -2
- data/lib/pdc/config.rb +49 -49
- data/lib/pdc/http/request/append_slash.rb +1 -1
- data/lib/pdc/http/request/pdc_token.rb +5 -6
- data/lib/pdc/http/request/token_fetcher.rb +0 -1
- data/lib/pdc/http/response/pagination.rb +4 -5
- data/lib/pdc/http/response/parser.rb +6 -6
- data/lib/pdc/resource/attribute_store.rb +5 -9
- data/lib/pdc/resource/attributes.rb +22 -25
- data/lib/pdc/resource/identity.rb +7 -6
- data/lib/pdc/resource/path.rb +2 -4
- data/lib/pdc/resource/per_thread_registry.rb +10 -8
- data/lib/pdc/resource/relation/finder.rb +1 -1
- data/lib/pdc/resource/relation/pagination.rb +1 -1
- data/lib/pdc/resource/relation/query.rb +0 -1
- data/lib/pdc/resource/relation.rb +14 -13
- data/lib/pdc/resource/rest_api.rb +2 -3
- data/lib/pdc/resource/value_parser.rb +5 -6
- data/lib/pdc/v1/product.rb +0 -1
- data/lib/pdc/v1/release.rb +1 -1
- data/lib/pdc/v1/release_rpm_mapping.rb +9 -0
- data/lib/pdc/v1/release_variant.rb +1 -2
- data/lib/pdc/v1.rb +1 -0
- data/lib/pdc/version.rb +1 -1
- data/spec/fixtures/vcr/must_has_compose.yml +47 -0
- data/spec/pdc/config_spec.rb +2 -2
- data/spec/pdc/resource/attributes_spec.rb +46 -46
- data/spec/pdc/resource/cache_spec.rb +0 -1
- data/spec/pdc/resource/count_spec.rb +2 -2
- data/spec/pdc/resource/identity_spec.rb +0 -1
- data/spec/pdc/resource/pagination_spec.rb +3 -3
- data/spec/pdc/resource/path_spec.rb +10 -5
- data/spec/pdc/resource/relation_spec.rb +2 -3
- data/spec/pdc/v1/arch_spec.rb +1 -1
- data/spec/pdc/v1/release_rpm_mapping_spec.rb +16 -0
- data/spec/pdc/v1/release_spec.rb +2 -2
- data/spec/spec_helper.rb +4 -4
- data/spec/support/fixtures.rb +2 -4
- data/spec/support/vcr.rb +1 -1
- data/spec/support/webmock.rb +1 -3
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a63a931b11dc86a0902d4632e3a6cdfc8ac3812
|
4
|
+
data.tar.gz: 18be45c56ab85aeb2ebced876758df49052fdb32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28df5bd5f63bef0af4d6122df37f3552df06ccb4bf108283f5396224fb1746e3c62a500d145bcfc3b38a16371e9394043e55a788f5af9be3b77daa23953d4ff1
|
7
|
+
data.tar.gz: 0e316110d183aab7b67357816124daa0b825b5b9d35fd56abf43093972607b4e1c066c85c89d5acd5afff6f7d8c09a108cf2d462a769c90fe5908cfd43e31aa8
|
data/.pronto.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
all:
|
2
|
+
exclude:
|
3
|
+
- 'spec/**/*'
|
4
|
+
# exclude files for single runner
|
5
|
+
eslint:
|
6
|
+
exclude:
|
7
|
+
- 'app/assets/**/*'
|
8
|
+
github:
|
9
|
+
slug: product-definition-center/pdc-ruby-gem
|
10
|
+
api_endpoint: https://api.github.com/
|
11
|
+
web_endpoint: https://github.com/
|
12
|
+
max_warnings: 150
|
13
|
+
verbose: false
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
DisplayStyleGuide: true
|
4
|
+
|
5
|
+
Exclude:
|
6
|
+
- spec/**/*
|
7
|
+
|
8
|
+
Style/Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/ClassAndModuleChildren:
|
12
|
+
Enabled: false
|
13
|
+
EnforcedStyle: compact
|
14
|
+
|
15
|
+
Metrics/LineLength:
|
16
|
+
Max: 100
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Max: 25
|
20
|
+
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 25
|
23
|
+
|
data/.travis.yml
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
language: ruby
|
2
|
+
cache: bundler
|
2
3
|
rvm:
|
3
4
|
- 2.2.2
|
4
5
|
addons:
|
@@ -8,14 +9,24 @@ addons:
|
|
8
9
|
before_install: gem install bundler -v 1.11.2
|
9
10
|
install:
|
10
11
|
- bundle install --jobs=3 --retry=4
|
11
|
-
- gem install coveralls
|
12
12
|
- git config --global user.name "Travis CI"
|
13
13
|
- git config --global user.email "pdc@product-definition-center.com"
|
14
14
|
- export REPO_URL_GITHUB="https://$GH_TOKEN@github.com/$GH_REPO.git"
|
15
|
+
- export GITHUB_ACCESS_TOKEN=$GH_TOKEN
|
15
16
|
script:
|
16
17
|
- bundle exec rake test
|
17
|
-
-
|
18
|
+
- >
|
19
|
+
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
|
20
|
+
bundle exec pronto run -f github_pr -c origin/${TRAVIS_BRANCH}
|
21
|
+
fi
|
22
|
+
|
23
|
+
after_success:
|
24
|
+
- >
|
25
|
+
[ "$TRAVIS_BRANCH" = "master" ] &&
|
26
|
+
[ "$TRAVIS_PULL_REQUEST" = "false" ] &&
|
27
|
+
make deploy_doc
|
18
28
|
env:
|
19
29
|
global:
|
20
30
|
- GH_REPO="product-definition-center/pdc-ruby-gem"
|
31
|
+
- PULL_REQUEST_ID=${TRAVIS_PULL_REQUEST}
|
21
32
|
- secure: Mf6k7ZwdsOOzP4woIo9z3X8e8hYZUOitBYPVOBeWpXPY8rruet3DhRYAEMNqf8iH9YAVqssE4Q0j76PQrQ6WyX+TXnayl62VAGeRjwQt4rs3orDjN+lZSP1tMt8w5fN906XeV2pcbuahRU1cFEEJ6iWhTbvhvgugsmuo8dzoS5uLRtv+VopyZjJaOqvsk5wCPKMKQLAqCvYDf2ugCm2qI3+FdjQb24WaTMLmF+vVXQSGvOpXx8Z6gLZRk0/bZeBLxFLFJcAFnRdbC09YI8Jt5nmwg7Lc+mX19qGUmracsYz2KKTDg3XC2b4n4O63woQFdwwvj0LQIoLmc/6YoxIPuCDQ92oXVCDGHB9kTh3GPxHPVwWsveg8Auvj0cPMSOTZId46Jox8sOXlIxZD0xXHiq8XQ1WSYJm0LJheO9mZHV4DIHp0o0Zm7ii7cxkb2It0c2oACRpiyBmXPfGKwNoMgfAzPo4dbbmbqoRD6XxxzLxKNwhKOAtcBMs6j/dKlKyo12h79+yqlkcYmvOLp+Bz/NqRRN63j1h5wR2Irl95uLv2PN26k2sod+Kmk22IcmDJP80ha/IodcxmPIDTZKnqLd7bcaK3mHvZMQ1DsKeuktaqKvnK8fdaZHAhVSfUlu0yCi5PfiH28+HbdpSK6VHQhH7c9/YgwrS9sgcE0Kh1Mw0=
|
data/Gemfile
CHANGED
@@ -1,31 +1,37 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
group :tools do
|
4
|
-
gem 'rubocop'
|
5
4
|
gem 'guard', '<= 2.12.0'
|
6
5
|
gem 'guard-minitest', '~> 2.4.4'
|
7
6
|
gem 'guard-shell'
|
8
7
|
end
|
9
8
|
|
10
9
|
group :development do
|
11
|
-
gem 'rake', '~> 10.0'
|
12
|
-
gem 'rake-notes'
|
13
|
-
gem 'colorize'
|
14
10
|
gem 'awesome_print'
|
11
|
+
gem 'colorize'
|
15
12
|
gem 'pry'
|
16
13
|
gem 'pry-byebug'
|
14
|
+
gem 'rake', '~> 10.0'
|
15
|
+
gem 'rake-notes'
|
17
16
|
end
|
18
17
|
|
19
18
|
group :test do
|
20
|
-
gem 'mocha'
|
21
|
-
gem 'webmock', '~> 1.18.0'
|
22
|
-
gem 'minitest-reporters', '~> 1.1.9'
|
23
|
-
gem 'minitest-focus'
|
24
|
-
gem 'simplecov'
|
25
19
|
gem 'coveralls'
|
20
|
+
gem 'minitest-focus'
|
21
|
+
gem 'minitest-reporters', '~> 1.1.9'
|
22
|
+
gem 'mocha'
|
26
23
|
gem 'rack', '~> 1.4.7'
|
27
|
-
gem '
|
24
|
+
gem 'simplecov'
|
28
25
|
gem 'timecop'
|
26
|
+
gem 'vcr'
|
27
|
+
gem 'webmock', '~> 1.18.0'
|
28
|
+
end
|
29
|
+
|
30
|
+
group :development, :test do
|
31
|
+
# Pronto
|
32
|
+
gem 'pronto'
|
33
|
+
gem 'pronto-flay', require: false
|
34
|
+
gem 'pronto-rubocop', require: false
|
29
35
|
end
|
30
36
|
|
31
37
|
# Specify your gem's dependencies in pdc-ruby.gemspec
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
3
|
require 'rake/notes/rake_task'
|
4
4
|
|
5
5
|
Rake::TestTask.new(:test) do |t|
|
6
|
-
t.libs <<
|
7
|
-
t.libs <<
|
6
|
+
t.libs << 'spec'
|
7
|
+
t.libs << 'lib'
|
8
8
|
t.test_files = FileList['spec/**/*_spec.rb']
|
9
9
|
end
|
10
10
|
|
data/bin/console
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pry'
|
5
|
+
require 'ap'
|
6
6
|
|
7
|
-
require
|
8
|
-
require_relative
|
7
|
+
require 'pdc'
|
8
|
+
require_relative '../spec/support/fixtures'
|
9
9
|
|
10
10
|
include Fixtures
|
11
11
|
Pry.start
|
data/examples/logger.rb
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
require_relative '../lib/pdc'
|
2
2
|
require 'ap'
|
3
3
|
|
4
|
+
# :nodoc:
|
4
5
|
class FakeLogger
|
5
6
|
attr_accessor :level
|
6
7
|
|
7
8
|
def debug(*args)
|
8
9
|
end
|
10
|
+
|
9
11
|
def info(*args)
|
10
12
|
end
|
13
|
+
|
11
14
|
def warn(*args)
|
12
15
|
end
|
16
|
+
|
13
17
|
def fatal(*args)
|
14
18
|
end
|
15
19
|
|
@@ -17,22 +21,21 @@ class FakeLogger
|
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
20
|
-
#PDC.logger = FakeLogger.new
|
21
|
-
|
24
|
+
# PDC.logger = FakeLogger.new
|
22
25
|
|
23
26
|
logger = PDC.logger
|
24
27
|
|
25
|
-
logger.debug(
|
26
|
-
logger.info(
|
28
|
+
logger.debug('Debug will be logged')
|
29
|
+
logger.info('Info will be logged as well')
|
27
30
|
|
28
31
|
logger.level = Logger::WARN
|
29
32
|
|
30
|
-
logger.debug(
|
31
|
-
logger.info(
|
32
|
-
logger.warn(
|
33
|
-
logger.error(
|
33
|
+
logger.debug('Created logger') # won't be logged
|
34
|
+
logger.info('Program started') # won't be logged
|
35
|
+
logger.warn('Nothing to do!')
|
36
|
+
logger.error('Error something really went wrong')
|
34
37
|
|
35
|
-
path =
|
38
|
+
path = 'a_non_existent_file'
|
36
39
|
|
37
40
|
begin
|
38
41
|
File.foreach(path) do |line|
|
@@ -41,10 +44,9 @@ begin
|
|
41
44
|
end
|
42
45
|
end
|
43
46
|
rescue => err
|
44
|
-
logger.fatal(
|
47
|
+
logger.fatal('Caught exception; exiting')
|
45
48
|
logger.fatal(err)
|
46
49
|
end
|
47
50
|
|
48
51
|
logger.level = Logger::INFO
|
49
|
-
logger.info(
|
50
|
-
|
52
|
+
logger.info('will continue here')
|
@@ -7,7 +7,7 @@ def obtain_token
|
|
7
7
|
url = PDC.config.site + PDC.config.rest_api_path + '/' + PDC.config.token_obtain_path
|
8
8
|
|
9
9
|
c = Curl::Easy.new(url) do |request|
|
10
|
-
request.headers[
|
10
|
+
request.headers['Accept'] = 'application/json'
|
11
11
|
request.http_auth_types = :gssnegotiate
|
12
12
|
|
13
13
|
# The curl man page (http://curl.haxx.se/docs/manpage.html) specifes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require './lib/pdc'
|
2
|
+
|
3
|
+
def main
|
4
|
+
PDC.configure do |config|
|
5
|
+
config.site = 'https://pdc.engineering.redhat.com'
|
6
|
+
config.log_level = :debug
|
7
|
+
config.disable_caching = true
|
8
|
+
end
|
9
|
+
|
10
|
+
mapping = PDC::V1::ReleaseRpmMapping.where(
|
11
|
+
release_id: 'ceph-2.1-updates@rhel-7', package: 'ceph'
|
12
|
+
).first
|
13
|
+
puts mapping.mapping
|
14
|
+
end
|
15
|
+
|
16
|
+
main if __FILE__ == $PROGRAM_NAME
|
@@ -171,3 +171,25 @@ describe PDC::V1::ReleaseVariant do
|
|
171
171
|
end
|
172
172
|
end
|
173
173
|
end
|
174
|
+
|
175
|
+
# using prod server to test PDC::V1::ReleaseRpmMapping
|
176
|
+
pdc = server[:prod]
|
177
|
+
|
178
|
+
PDC.configure do |config|
|
179
|
+
config.site = pdc[:site]
|
180
|
+
auth_token = pdc[:token]
|
181
|
+
config.token = pdc[:token].call if auth_token
|
182
|
+
config.requires_token = auth_token.present? # to_bool
|
183
|
+
end
|
184
|
+
|
185
|
+
describe PDC::V1::ReleaseRpmMapping do
|
186
|
+
let(:mapping) do
|
187
|
+
PDC::V1::ReleaseRpmMapping.where(
|
188
|
+
release_id: 'ceph-2.1-updates@rhel-7',
|
189
|
+
package: 'ceph'
|
190
|
+
).first
|
191
|
+
end
|
192
|
+
it 'must has compose' do
|
193
|
+
mapping.mapping.must_be_instance_of OpenStruct
|
194
|
+
end
|
195
|
+
end
|
data/examples/pdc_test_cache.rb
CHANGED
@@ -7,17 +7,18 @@ PDC.configure do |config|
|
|
7
7
|
# dev server
|
8
8
|
config.site = 'https://pdc.host.dev.eng.pek2.redhat.com/'
|
9
9
|
config.cache_store = ActiveSupport::Cache.lookup_store(
|
10
|
-
:file_store, [File.join(ENV['TMPDIR'] || '/tmp', 'cache')]
|
10
|
+
:file_store, [File.join(ENV['TMPDIR'] || '/tmp', 'cache')]
|
11
|
+
)
|
11
12
|
config.log_level = :info
|
12
13
|
end
|
13
14
|
|
14
|
-
ActiveSupport::Notifications.subscribe
|
15
|
+
ActiveSupport::Notifications.subscribe 'http_cache.faraday' do |*args|
|
15
16
|
event = ActiveSupport::Notifications::Event.new(*args)
|
16
|
-
puts
|
17
|
+
puts ' >>> ' + "cache: #{event.payload[:cache_status]}"
|
17
18
|
ap event.payload
|
18
19
|
end
|
19
20
|
|
20
|
-
def benchmark(description,
|
21
|
+
def benchmark(description, _opts = {}, &block)
|
21
22
|
puts "Running: #{description}"
|
22
23
|
|
23
24
|
initial = Benchmark.measure(&block)
|
@@ -28,19 +29,19 @@ def benchmark(description, opts = {}, &block)
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def main
|
31
|
-
benchmark
|
32
|
+
benchmark 'fetch all' do
|
32
33
|
PDC::V1::Release.all
|
33
34
|
end
|
34
35
|
|
35
|
-
benchmark
|
36
|
+
benchmark 'fetch with page number' do
|
36
37
|
PDC::V1::Release.page(2).contents!
|
37
38
|
end
|
38
39
|
|
39
|
-
benchmark
|
40
|
+
benchmark 'fetch with page number and size' do
|
40
41
|
PDC::V1::Release.page(2).page_size(30).contents!
|
41
42
|
end
|
42
43
|
|
43
|
-
benchmark
|
44
|
+
benchmark 'fetch with page number and size and query condition' do
|
44
45
|
PDC::V1::Release.page(2).page_size(30).where(active: true).contents!
|
45
46
|
end
|
46
47
|
end
|
data/examples/prod_failures.rb
CHANGED
data/examples/prod_pdc.rb
CHANGED
data/lib/pdc/base.rb
CHANGED
@@ -8,7 +8,7 @@ module PDC
|
|
8
8
|
include PDC::Resource::Scopes
|
9
9
|
include PDC::Resource::RestApi
|
10
10
|
|
11
|
-
scope :page, ->(value) { where(:
|
12
|
-
scope :page_size, ->(value) { where(:
|
11
|
+
scope :page, ->(value) { where(page: value) }
|
12
|
+
scope :page_size, ->(value) { where(page_size: value) }
|
13
13
|
end
|
14
14
|
end
|
data/lib/pdc/config.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'faraday-http-cache'
|
2
2
|
require 'pdc/http/request/token_fetcher'
|
3
3
|
|
4
|
+
# rubocop:disable ModuleLength
|
4
5
|
module PDC
|
5
6
|
# This class is the main access point for all PDC::Resource instances.
|
6
7
|
#
|
@@ -34,7 +35,7 @@ module PDC
|
|
34
35
|
:logger,
|
35
36
|
|
36
37
|
:cache_store,
|
37
|
-
:disable_caching
|
38
|
+
:disable_caching
|
38
39
|
) do
|
39
40
|
def initialize
|
40
41
|
# site config
|
@@ -93,61 +94,60 @@ module PDC
|
|
93
94
|
|
94
95
|
private
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
97
|
+
def apply_config
|
98
|
+
reset_logger
|
99
|
+
reset_token_fetcher
|
100
|
+
reset_base_connection
|
101
|
+
end
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
103
|
+
def reset_logger
|
104
|
+
PDC.logger = Logger.new(nil) unless config.enable_logging
|
105
|
+
logger.level = Logger.const_get(config.log_level.upcase)
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
108
|
+
def reset_token_fetcher
|
109
|
+
Request::TokenFetcher.configure do |c|
|
110
|
+
c.url = token_url
|
111
|
+
c.ssl_verify_mode = config.ssl_verify_mode
|
112
112
|
end
|
113
|
+
end
|
113
114
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
140
|
-
c.adapter Faraday.default_adapter
|
115
|
+
# resets and returns the +Faraday+ +connection+ object
|
116
|
+
# rubocop:disable AbcSize
|
117
|
+
def reset_base_connection
|
118
|
+
faraday_config = {
|
119
|
+
url: api_url,
|
120
|
+
headers: PDC::Request.default_headers,
|
121
|
+
ssl: ssl_config
|
122
|
+
}
|
123
|
+
|
124
|
+
PDC::Base.connection = Faraday.new(faraday_config) do |c|
|
125
|
+
c.request :append_slash_to_path
|
126
|
+
c.request :pdc_token, token: config.token if config.requires_token
|
127
|
+
|
128
|
+
c.response :logger, config.logger
|
129
|
+
c.response :pdc_paginator
|
130
|
+
c.response :pdc_json_parser
|
131
|
+
c.response :raise_error
|
132
|
+
c.response :pdc_raise_error
|
133
|
+
|
134
|
+
c.use FaradayMiddleware::FollowRedirects
|
135
|
+
|
136
|
+
unless config.disable_caching
|
137
|
+
c.use Faraday::HttpCache, store: cache_store,
|
138
|
+
logger: PDC.logger,
|
139
|
+
instrumenter: ActiveSupport::Notifications
|
141
140
|
end
|
141
|
+
c.adapter Faraday.default_adapter
|
142
142
|
end
|
143
|
+
end
|
143
144
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
def cache_store
|
149
|
-
config.cache_store || ActiveSupport::Cache.lookup_store(:memory_store)
|
150
|
-
end
|
145
|
+
def ssl_config
|
146
|
+
{ verify: config.ssl_verify_mode == OpenSSL::SSL::VERIFY_PEER }
|
147
|
+
end
|
151
148
|
|
149
|
+
def cache_store
|
150
|
+
config.cache_store || ActiveSupport::Cache.lookup_store(:memory_store)
|
151
|
+
end
|
152
152
|
end
|
153
153
|
end
|
@@ -2,7 +2,7 @@ module PDC
|
|
2
2
|
class Request::AppendSlashToPath < Faraday::Middleware
|
3
3
|
include PDC::Logging
|
4
4
|
|
5
|
-
Faraday::Request.register_middleware :
|
5
|
+
Faraday::Request.register_middleware append_slash_to_path: self
|
6
6
|
|
7
7
|
def call(env)
|
8
8
|
logger.debug "\n..... append slash .........................................."
|
@@ -1,5 +1,4 @@
|
|
1
1
|
module PDC::Request
|
2
|
-
|
3
2
|
# Adds TokenAuthentication to request header. Uses the token if passed
|
4
3
|
# else fetches token using the TokenFetcher to get the token once
|
5
4
|
class Token < Faraday::Middleware
|
@@ -20,11 +19,11 @@ module PDC::Request
|
|
20
19
|
|
21
20
|
private
|
22
21
|
|
23
|
-
|
22
|
+
attr_reader :options
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
# uses the token passed or fetches one only once
|
25
|
+
def token
|
26
|
+
@token ||= options[:token] || TokenFetcher.fetch
|
27
|
+
end
|
29
28
|
end
|
30
29
|
end
|
@@ -2,7 +2,7 @@ module PDC::Response
|
|
2
2
|
class Paginator < Faraday::Response::Middleware
|
3
3
|
include PDC::Logging
|
4
4
|
|
5
|
-
Faraday::Response.register_middleware :
|
5
|
+
Faraday::Response.register_middleware pdc_paginator: self
|
6
6
|
|
7
7
|
def parse(json)
|
8
8
|
logger.debug "\n.....paginate json ....................................."
|
@@ -18,7 +18,7 @@ module PDC::Response
|
|
18
18
|
# schema://host:port/ of the next and previous
|
19
19
|
|
20
20
|
next_page: request_uri(metadata.delete(:next)),
|
21
|
-
previous_page: request_uri(metadata.delete(:previous))
|
21
|
+
previous_page: request_uri(metadata.delete(:previous))
|
22
22
|
}
|
23
23
|
|
24
24
|
logger.debug '... after parsing pagination data:'
|
@@ -35,9 +35,8 @@ module PDC::Response
|
|
35
35
|
|
36
36
|
def paginated?(metadata)
|
37
37
|
metadata[:count].is_a?(Numeric) &&
|
38
|
-
|
39
|
-
|
38
|
+
metadata.key?(:next) &&
|
39
|
+
metadata.key?(:previous)
|
40
40
|
end
|
41
|
-
|
42
41
|
end
|
43
42
|
end
|
@@ -5,20 +5,20 @@ module PDC::Response
|
|
5
5
|
class Parser < Faraday::Response::Middleware
|
6
6
|
include PDC::Logging
|
7
7
|
|
8
|
-
Faraday::Response.register_middleware :
|
8
|
+
Faraday::Response.register_middleware pdc_json_parser: self
|
9
9
|
|
10
10
|
def parse(body)
|
11
11
|
logger.debug "\n.....parse to json ....................................."
|
12
12
|
logger.debug self.class
|
13
13
|
|
14
|
-
logger.debug '... parsing' +
|
14
|
+
logger.debug '... parsing' + body.to_s.truncate(55)
|
15
15
|
begin
|
16
16
|
json = MultiJson.load(body, symbolize_keys: true)
|
17
17
|
rescue MultiJson::ParseError => e
|
18
18
|
raise PDC::JsonParseError, e
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
{
|
22
22
|
data: extract_data(json), # Always an Array
|
23
23
|
errors: extract_errors(json), #
|
24
24
|
metadata: extract_metadata(json) # a hash
|
@@ -29,7 +29,7 @@ module PDC::Response
|
|
29
29
|
|
30
30
|
def extract_data(json)
|
31
31
|
return [] if error?(json)
|
32
|
-
return json[:results] if
|
32
|
+
return json[:results] if metadata_present?(json)
|
33
33
|
Array.wrap(json)
|
34
34
|
end
|
35
35
|
|
@@ -38,11 +38,11 @@ module PDC::Response
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def extract_metadata(json)
|
41
|
-
return json.except(:details, :results) if
|
41
|
+
return json.except(:details, :results) if metadata_present?(json)
|
42
42
|
data_only?(json) ? { count: json.length, next: nil, previous: nil } : {}
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def metadata_present?(json)
|
46
46
|
return false if data_only?(json) || error?(json)
|
47
47
|
|
48
48
|
json[:results].is_a?(Array) &&
|
@@ -8,15 +8,11 @@ module PDC::Resource
|
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
when value.is_a?(Array) then value.map { |v| parse_value(v) }
|
16
|
-
else value
|
17
|
-
end
|
11
|
+
def parse_value(value)
|
12
|
+
if value.is_a?(PDC::Base) then value.attributes.to_params
|
13
|
+
elsif value.is_a?(Array) then value.map { |v| parse_value(v) }
|
14
|
+
else value
|
18
15
|
end
|
19
|
-
|
16
|
+
end
|
20
17
|
end
|
21
18
|
end
|
22
|
-
|