pdc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -7
- data/examples/http_failures.rb +1 -1
- data/examples/pdc_resource_tests.rb +2 -2
- data/examples/pdc_test_cache.rb +3 -3
- data/examples/prod_failures.rb +1 -1
- data/lib/pdc/config.rb +15 -25
- data/lib/pdc/http/request/append_slash.rb +2 -2
- data/lib/pdc/http/request/pdc_token.rb +30 -0
- data/lib/pdc/http/request/token_fetcher.rb +32 -0
- data/lib/pdc/http/request.rb +2 -0
- data/lib/pdc/http/response/pagination.rb +2 -2
- data/lib/pdc/http/response/parser.rb +2 -2
- data/lib/pdc/resource/rest_api.rb +1 -1
- data/lib/pdc/version.rb +1 -1
- data/lib/pdc.rb +0 -5
- data/pdc.gemspec +2 -2
- data/spec/pdc/config_spec.rb +26 -7
- data/spec/support/webmock.rb +2 -2
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 234203e23bbe2f53796496c2048352e4029c6821
|
4
|
+
data.tar.gz: cd1c02de0fe5905a7312dc5f843b7ca7cc2986a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a85cc7e23ec88086c115e4071de94266f327ffdb05044205cf469b1c46e9aced707bfc21ab64c642136d59fd5b617e20ff2a6e2569c4e6262eaf8c0816982d0b
|
7
|
+
data.tar.gz: 1f31b025816cebc99da89a81dd768a7fd9741ebcac826f69ced6b2a0a6f27093b0215fd81ff3be743321ea56bdf4fc96b162562e57b0cc89cf60cefbd84a491d
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# PDC
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Ruby `PDC` library that maps `PDC` json api to ActiveRecord like objects.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -26,16 +24,37 @@ TODO: Write usage instructions here
|
|
26
24
|
|
27
25
|
## Development
|
28
26
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
28
|
+
Then, run `rake test` to run the tests. You can also run `bin/console` for
|
29
|
+
an interactive prompt that will allow you to experiment.
|
30
30
|
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
32
|
+
To release a new version, update the version number in `version.rb`, and
|
33
|
+
then run `bundle exec rake release`, which will create a git tag for the
|
34
|
+
version, push git commits and tags, and push the `.gem` file to [rubygems][]
|
32
35
|
|
33
36
|
## Contributing
|
34
37
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at
|
38
|
+
Bug reports and pull requests are welcome on GitHub at
|
39
|
+
[product-definition-center/pdc-ruby-gem][pdc-github] . This project is intended to be a safe,
|
40
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
41
|
+
the [Contributor Covenant][coc] code of conduct.
|
36
42
|
|
37
43
|
|
38
44
|
## License
|
39
45
|
|
40
|
-
The gem is available as open source under the terms of the [MIT License]
|
46
|
+
The gem is available as open source under the terms of the [MIT License][mit-license]
|
47
|
+
|
48
|
+
## Credits
|
49
|
+
|
50
|
+
This gem is heavily influenced by the [spyke][] gem which requires
|
51
|
+
`activesupport 4.0` or above where as the `pdc` gem needs to run against
|
52
|
+
`activesupport 3.2.22`. If you are using `activesupport 4` (rails 4) or above
|
53
|
+
and is looking for json api to ORM framework please consider using [spyke][] instead.
|
54
|
+
|
41
55
|
|
56
|
+
[rubygems]: https://rubygems.org
|
57
|
+
[mit-license]: http://opensource.org/licenses/MIT
|
58
|
+
[coc]: http://contributor-covenant.org
|
59
|
+
[pdc-github]: https://github.com/product-definition-center/pdc-ruby-gem
|
60
|
+
[spyke]: https://github.com/balvig/spyke
|
data/examples/http_failures.rb
CHANGED
@@ -7,9 +7,9 @@ WebMock.disable! # this uses real server
|
|
7
7
|
def token
|
8
8
|
script_path = File.expand_path(File.dirname(__FILE__))
|
9
9
|
token_path = File.join(script_path, '.token', 'pdc.prod')
|
10
|
-
File.read(token_path).chomp.tap { |x| puts "Using token :#{x
|
10
|
+
File.read(token_path).chomp.tap { |x| puts "Using token :#{x}" }
|
11
11
|
rescue Errno::ENOENT => e
|
12
|
-
puts "Hey! did you forget to create #{token_path
|
12
|
+
puts "Hey! did you forget to create #{token_path} \n"
|
13
13
|
raise e
|
14
14
|
end
|
15
15
|
|
data/examples/pdc_test_cache.rb
CHANGED
@@ -13,7 +13,7 @@ end
|
|
13
13
|
|
14
14
|
ActiveSupport::Notifications.subscribe "http_cache.faraday" do |*args|
|
15
15
|
event = ActiveSupport::Notifications::Event.new(*args)
|
16
|
-
puts " >>> "
|
16
|
+
puts " >>> " + "cache: #{event.payload[:cache_status]}"
|
17
17
|
ap event.payload
|
18
18
|
end
|
19
19
|
|
@@ -23,8 +23,8 @@ def benchmark(description, opts = {}, &block)
|
|
23
23
|
initial = Benchmark.measure(&block)
|
24
24
|
repeat = Benchmark.measure(&block)
|
25
25
|
|
26
|
-
puts "Initial : #{initial.to_s.chomp} >> #{initial.real.round(2)
|
27
|
-
puts "Repeat : #{repeat.to_s.chomp} >> #{repeat.real.round(2)
|
26
|
+
puts "Initial : #{initial.to_s.chomp} >> #{initial.real.round(2)}"
|
27
|
+
puts "Repeat : #{repeat.to_s.chomp} >> #{repeat.real.round(2)} \n"
|
28
28
|
end
|
29
29
|
|
30
30
|
def main
|
data/examples/prod_failures.rb
CHANGED
data/lib/pdc/config.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'faraday-http-cache'
|
2
|
-
require '
|
2
|
+
require 'pdc/http/request/token_fetcher'
|
3
3
|
|
4
4
|
module PDC
|
5
5
|
# This class is the main access point for all PDC::Resource instances.
|
@@ -90,7 +90,7 @@ module PDC
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def token
|
93
|
-
config.token ||
|
93
|
+
config.token || Request::TokenFetcher.fetch(token_url)
|
94
94
|
end
|
95
95
|
|
96
96
|
private
|
@@ -107,10 +107,16 @@ module PDC
|
|
107
107
|
|
108
108
|
# resets and returns the +Faraday+ +connection+ object
|
109
109
|
def reset_base_connection
|
110
|
-
|
111
|
-
|
110
|
+
|
111
|
+
faraday_config = {
|
112
|
+
url: api_url,
|
113
|
+
headers: PDC::Request.default_headers,
|
114
|
+
ssl: ssl_config
|
115
|
+
}
|
116
|
+
|
117
|
+
PDC::Base.connection = Faraday.new(faraday_config) do |c|
|
112
118
|
c.request :append_slash_to_path
|
113
|
-
c.request :
|
119
|
+
c.request :pdc_token, token: config.token, token_url: token_url if config.requires_token
|
114
120
|
|
115
121
|
c.response :logger, config.logger
|
116
122
|
c.response :pdc_paginator
|
@@ -129,29 +135,13 @@ module PDC
|
|
129
135
|
end
|
130
136
|
end
|
131
137
|
|
138
|
+
def ssl_config
|
139
|
+
{ verify: config.ssl_verify_mode == OpenSSL::SSL::VERIFY_PEER }
|
140
|
+
end
|
141
|
+
|
132
142
|
def cache_store
|
133
143
|
config.cache_store || ActiveSupport::Cache.lookup_store(:memory_store)
|
134
144
|
end
|
135
145
|
|
136
|
-
def fetch_token
|
137
|
-
curl = Curl::Easy.new(token_url.to_s) do |request|
|
138
|
-
request.headers['Accept'] = 'application/json'
|
139
|
-
request.http_auth_types = :gssnegotiate
|
140
|
-
|
141
|
-
# The curl man page (http://curl.haxx.se/docs/manpage.html)
|
142
|
-
# specifes setting a fake username when using Negotiate auth,
|
143
|
-
# and use ':' in their example.
|
144
|
-
request.username = ':'
|
145
|
-
end
|
146
|
-
curl.perform
|
147
|
-
if curl.response_code != 200
|
148
|
-
logger.info "Obtain token from #{token_url} failed: #{curl.body_str}"
|
149
|
-
error = { token_url: token_url, body: curl.body, code: curl.response_code }
|
150
|
-
raise PDC::TokenFetchFailed, error
|
151
|
-
end
|
152
|
-
result = JSON.parse(curl.body_str)
|
153
|
-
curl.close
|
154
|
-
result['token']
|
155
|
-
end
|
156
146
|
end
|
157
147
|
end
|
@@ -5,14 +5,14 @@ module PDC
|
|
5
5
|
Faraday::Request.register_middleware :append_slash_to_path => self
|
6
6
|
|
7
7
|
def call(env)
|
8
|
-
logger.debug "\n..... append slash .........................................."
|
8
|
+
logger.debug "\n..... append slash .........................................."
|
9
9
|
logger.debug self.class
|
10
10
|
logger.debug env.url
|
11
11
|
|
12
12
|
path = env.url.path
|
13
13
|
env.url.path = path + '/' unless path.ends_with?('/')
|
14
14
|
|
15
|
-
logger.debug "... after adding / #{env.url.path}: #{env.url
|
15
|
+
logger.debug "... after adding / #{env.url.path}: #{env.url}"
|
16
16
|
@app.call(env)
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module PDC::Request
|
2
|
+
|
3
|
+
# Adds TokenAuthentication to request header. Uses the token if passed
|
4
|
+
# else fetches token using the TokenFetcher to get the token once
|
5
|
+
class Token < Faraday::Middleware
|
6
|
+
include PDC::Logging
|
7
|
+
|
8
|
+
Faraday::Request.register_middleware pdc_token: self
|
9
|
+
|
10
|
+
def initialize(app, options = {})
|
11
|
+
@options = options
|
12
|
+
super(app)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
env.request_headers['Token'] = token
|
17
|
+
logger.debug "Token set, headers: #{env.request_headers}"
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :options
|
24
|
+
|
25
|
+
# uses the token passed or fetches one only once
|
26
|
+
def token
|
27
|
+
@token ||= options[:token] || TokenFetcher.fetch(options[:token_url])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module PDC::Request
|
5
|
+
module TokenFetcher
|
6
|
+
|
7
|
+
# uses kerberos token to obtain token from pdc
|
8
|
+
def self.fetch(token_url)
|
9
|
+
PDC.logger.debug "Fetch token from: #{token_url}"
|
10
|
+
curl = Curl::Easy.new(token_url.to_s) do |request|
|
11
|
+
request.headers['Accept'] = 'application/json'
|
12
|
+
request.http_auth_types = :gssnegotiate
|
13
|
+
|
14
|
+
# The curl man page (http://curl.haxx.se/docs/manpage.html)
|
15
|
+
# specifes setting a fake username when using Negotiate auth,
|
16
|
+
# and use ':' in their example.
|
17
|
+
request.username = ':'
|
18
|
+
end
|
19
|
+
|
20
|
+
curl.perform
|
21
|
+
if curl.response_code != 200
|
22
|
+
PDC.logger.info "Obtain token from #{token_url} failed: #{curl.body_str}"
|
23
|
+
error = { token_url: token_url, body: curl.body, code: curl.response_code }
|
24
|
+
raise PDC::TokenFetchFailed, error
|
25
|
+
end
|
26
|
+
result = JSON.parse(curl.body_str)
|
27
|
+
curl.close
|
28
|
+
result['token']
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/pdc/http/request.rb
CHANGED
@@ -5,7 +5,7 @@ module PDC::Response
|
|
5
5
|
Faraday::Response.register_middleware :pdc_paginator => self
|
6
6
|
|
7
7
|
def parse(json)
|
8
|
-
logger.debug "\n.....paginate json ....................................."
|
8
|
+
logger.debug "\n.....paginate json ....................................."
|
9
9
|
|
10
10
|
metadata = json[:metadata]
|
11
11
|
logger.debug metadata
|
@@ -21,7 +21,7 @@ module PDC::Response
|
|
21
21
|
previous_page: request_uri(metadata.delete(:previous)),
|
22
22
|
}
|
23
23
|
|
24
|
-
logger.debug '... after parsing pagination data:'
|
24
|
+
logger.debug '... after parsing pagination data:'
|
25
25
|
logger.debug metadata
|
26
26
|
json
|
27
27
|
end
|
@@ -8,10 +8,10 @@ module PDC::Response
|
|
8
8
|
Faraday::Response.register_middleware :pdc_json_parser => self
|
9
9
|
|
10
10
|
def parse(body)
|
11
|
-
logger.debug "\n.....parse to json ....................................."
|
11
|
+
logger.debug "\n.....parse to json ....................................."
|
12
12
|
logger.debug self.class
|
13
13
|
|
14
|
-
logger.debug
|
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
|
@@ -8,7 +8,7 @@ module PDC::Resource
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def request(method, path, query = {})
|
11
|
-
PDC.logger.debug ' >>>'
|
11
|
+
PDC.logger.debug ' >>>' + " : #{path} #{query}"
|
12
12
|
ActiveSupport::Notifications.instrument('request.pdc', method: method) do |payload|
|
13
13
|
response = connection.send(method) do |request|
|
14
14
|
request.url path, query
|
data/lib/pdc/version.rb
CHANGED
data/lib/pdc.rb
CHANGED
data/pdc.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'pdc/version'
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'pdc'
|
8
8
|
s.version = PDC::VERSION
|
9
|
-
s.authors = ['Sunil Thaha']
|
10
|
-
s.email = ['sthaha@redhat.com']
|
9
|
+
s.authors = ['Sunil Thaha', 'Cheng Yu']
|
10
|
+
s.email = ['sthaha@redhat.com', 'ycheng@redhat.com']
|
11
11
|
|
12
12
|
s.summary = 'Ruby gem for use with Product-Definition-Center'
|
13
13
|
s.description = 'API for PDC'
|
data/spec/pdc/config_spec.rb
CHANGED
@@ -10,11 +10,16 @@ describe PDC do
|
|
10
10
|
|
11
11
|
describe '##configure' do
|
12
12
|
let(:site) { 'https://pdc.production.site.org' }
|
13
|
+
|
13
14
|
let(:token_url) do
|
14
15
|
pdc = PDC.config
|
15
16
|
URI.join(site, pdc.api_root, pdc.token_obtain_path)
|
16
17
|
end
|
17
18
|
|
19
|
+
let(:releases_url) do
|
20
|
+
URI.join(site, PDC.config.api_root, 'v1/releases/')
|
21
|
+
end
|
22
|
+
|
18
23
|
it 'wont accept invalid config' do
|
19
24
|
assert_raises PDC::ConfigError do
|
20
25
|
PDC.configure do |config|
|
@@ -31,24 +36,38 @@ describe PDC do
|
|
31
36
|
ret.must_be_instance_of Faraday::Connection
|
32
37
|
end
|
33
38
|
|
34
|
-
it '
|
35
|
-
|
39
|
+
it 'must not fetch token during configure step' do
|
40
|
+
token = stub_request(:get, token_url).to_return_json(token: 'foobar')
|
41
|
+
PDC.configure { |pdc| pdc.site = site }
|
42
|
+
|
43
|
+
assert_not_requested token
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'fetches token on first request' do
|
47
|
+
token = stub_request(:get, token_url).to_return_json(token: 'foobar')
|
48
|
+
|
36
49
|
PDC.configure { |pdc| pdc.site = site }
|
50
|
+
assert_not_requested token
|
37
51
|
|
38
|
-
|
39
|
-
PDC.
|
52
|
+
releases = stub_request(:get, releases_url).to_return_json(count: 0)
|
53
|
+
3.times { PDC::V1::Release.count }
|
54
|
+
|
55
|
+
# the releases will be requested n times but the token will be reused
|
56
|
+
assert_requested token, times: 1
|
57
|
+
assert_requested releases, times: 3
|
40
58
|
end
|
41
59
|
|
42
60
|
it 'raises TokenFetchFailed when fails' do
|
43
|
-
|
61
|
+
token = stub_request(:get, token_url).to_return_json(
|
44
62
|
{ detail: 'Not found' },
|
45
63
|
status: [404, 'NOT FOUND']
|
46
64
|
)
|
47
65
|
|
66
|
+
PDC.configure { |pdc| pdc.site = site }
|
48
67
|
assert_raises PDC::TokenFetchFailed do
|
49
|
-
PDC.
|
68
|
+
PDC.token
|
50
69
|
end
|
51
|
-
assert_requested
|
70
|
+
assert_requested token
|
52
71
|
end
|
53
72
|
end
|
54
73
|
|
data/spec/support/webmock.rb
CHANGED
@@ -13,7 +13,7 @@ end
|
|
13
13
|
WebMock.allow_net_connect!
|
14
14
|
|
15
15
|
WebMock.stub_request(:any, /.*/).to_return do |request|
|
16
|
-
puts "UNSTUBBED REQUEST:"
|
16
|
+
puts "UNSTUBBED REQUEST:" + " #{request.method.upcase} #{request.uri}"
|
17
17
|
{ body: nil }
|
18
18
|
end
|
19
19
|
|
@@ -22,7 +22,7 @@ module PDC
|
|
22
22
|
module WebMockExtentions
|
23
23
|
def stub_get(path)
|
24
24
|
uri = URI.join(Fixtures::Base::SITE, 'fixtures/', path).to_s
|
25
|
-
puts " stubbing: #{uri
|
25
|
+
puts " stubbing: #{uri}"
|
26
26
|
stub_request(:get, uri)
|
27
27
|
end
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sunil Thaha
|
8
|
+
- Cheng Yu
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2016-08
|
12
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: activesupport
|
@@ -125,6 +126,7 @@ dependencies:
|
|
125
126
|
description: API for PDC
|
126
127
|
email:
|
127
128
|
- sthaha@redhat.com
|
129
|
+
- ycheng@redhat.com
|
128
130
|
executables:
|
129
131
|
- console
|
130
132
|
- setup
|
@@ -169,6 +171,8 @@ files:
|
|
169
171
|
- lib/pdc/http/errors.rb
|
170
172
|
- lib/pdc/http/request.rb
|
171
173
|
- lib/pdc/http/request/append_slash.rb
|
174
|
+
- lib/pdc/http/request/pdc_token.rb
|
175
|
+
- lib/pdc/http/request/token_fetcher.rb
|
172
176
|
- lib/pdc/http/response.rb
|
173
177
|
- lib/pdc/http/response/pagination.rb
|
174
178
|
- lib/pdc/http/response/parser.rb
|
@@ -254,7 +258,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
254
258
|
version: '0'
|
255
259
|
requirements: []
|
256
260
|
rubyforge_project:
|
257
|
-
rubygems_version: 2.
|
261
|
+
rubygems_version: 2.4.8
|
258
262
|
signing_key:
|
259
263
|
specification_version: 4
|
260
264
|
summary: Ruby gem for use with Product-Definition-Center
|