pmp 0.2.0 → 0.2.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 +1 -0
- data/lib/pmp/client.rb +8 -0
- data/lib/pmp/collection_document.rb +12 -4
- data/lib/pmp/connection.rb +2 -1
- data/lib/pmp/parser.rb +1 -1
- data/lib/pmp/response.rb +12 -12
- data/lib/pmp/version.rb +1 -1
- data/pmp.gemspec +1 -0
- data/spec/client_spec.rb +11 -1
- data/spec/collection_document_spec.rb +28 -0
- data/spec/response_spec.rb +9 -5
- data/spec/spec_helper.rb +6 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 499ef7d634987ac1b12ec8ff9ac7c11032f3f29d
|
4
|
+
data.tar.gz: 731e197a1ba576417cc28d30c47ae21f9ece371b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c9da9f1703bc732ab9c5fe421a57d52a0d9fdcb57f1c3577b4aeab87164c7057867c9c94bff8224d5d6a7441ca94cc2b63f3bba698702b8027abaace538c056
|
7
|
+
data.tar.gz: b65cc1b02e347d3b87772cd82bd137f53b1b018a57c48bdb9ba11a28251aeb1604780e725db5a4c38d4051ba5fad1fc99eaf442200800a4f62ec2bb405dbe027
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# PMP gem
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/PRX/pmp.png)](https://travis-ci.org/PRX/pmp)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/PRX/pmp/badge.png)](https://coveralls.io/r/PRX/pmp)
|
4
5
|
|
5
6
|
Gem to make it easier to use the PMP API, which is a hypermedia API using the collection.doc+json format.
|
6
7
|
|
data/lib/pmp/client.rb
CHANGED
@@ -11,14 +11,17 @@ module PMP
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def credentials(opts={})
|
14
|
+
@credentials = nil if (opts != {})
|
14
15
|
@credentials ||= PMP::Credential.new(options.merge(opts))
|
15
16
|
end
|
16
17
|
|
17
18
|
def token(opts={})
|
19
|
+
@token = nil if (opts != {})
|
18
20
|
@token ||= PMP::Token.new(options.merge(opts)).get_token
|
19
21
|
end
|
20
22
|
|
21
23
|
def root(opts={})
|
24
|
+
@root = nil if (opts != {})
|
22
25
|
opts = options.merge(href: endpoint).merge(opts)
|
23
26
|
@root ||= PMP::CollectionDocument.new(opts)
|
24
27
|
end
|
@@ -35,5 +38,10 @@ module PMP
|
|
35
38
|
"#{endpoint}profiles/#{type}"
|
36
39
|
end
|
37
40
|
|
41
|
+
# assume you want to make a call on the root doc for stuff it can do
|
42
|
+
def method_missing(method, *args)
|
43
|
+
self.root.send(method, *args)
|
44
|
+
end
|
45
|
+
|
38
46
|
end
|
39
47
|
end
|
@@ -55,7 +55,7 @@ module PMP
|
|
55
55
|
|
56
56
|
def items
|
57
57
|
load
|
58
|
-
@items
|
58
|
+
@items ||= []
|
59
59
|
end
|
60
60
|
|
61
61
|
def attributes
|
@@ -126,9 +126,17 @@ module PMP
|
|
126
126
|
|
127
127
|
setup_oauth_token
|
128
128
|
|
129
|
-
|
130
|
-
|
131
|
-
|
129
|
+
begin
|
130
|
+
raw = connection(options.merge({url: url})).send(method) do |request|
|
131
|
+
if [:post, :put].include?(method.to_sym) && !body.blank?
|
132
|
+
request.body = body.is_a?(String) ? body : body.to_json
|
133
|
+
end
|
134
|
+
end
|
135
|
+
rescue Faraday::Error::ResourceNotFound=>not_found_ex
|
136
|
+
if (method.to_sym == :get)
|
137
|
+
raw = OpenStruct.new(body: nil, status: 404)
|
138
|
+
else
|
139
|
+
raise not_found_ex
|
132
140
|
end
|
133
141
|
end
|
134
142
|
|
data/lib/pmp/connection.rb
CHANGED
@@ -30,12 +30,13 @@ module PMP
|
|
30
30
|
faraday.request :authorization, 'Bearer', opts[:oauth_token]
|
31
31
|
end
|
32
32
|
|
33
|
-
faraday.request :url_encoded
|
34
33
|
faraday.request :multipart
|
34
|
+
faraday.request :url_encoded
|
35
35
|
|
36
36
|
faraday.response :mashify
|
37
37
|
faraday.response :logger if opts[:debug]
|
38
38
|
faraday.response :json
|
39
|
+
faraday.response :raise_error
|
39
40
|
|
40
41
|
faraday.adapter opts[:adapter]
|
41
42
|
end
|
data/lib/pmp/parser.rb
CHANGED
data/lib/pmp/response.rb
CHANGED
@@ -8,20 +8,20 @@ module PMP
|
|
8
8
|
@raw = raw
|
9
9
|
@request = request
|
10
10
|
|
11
|
-
check_for_error(raw)
|
11
|
+
# check_for_error(raw)
|
12
12
|
end
|
13
13
|
|
14
|
-
def check_for_error(response)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
14
|
+
# def check_for_error(response)
|
15
|
+
# status_code_type = response.status.to_s[0]
|
16
|
+
# case status_code_type
|
17
|
+
# when "2"
|
18
|
+
# # puts "all is well, status: #{response.status}"
|
19
|
+
# when "4", "5"
|
20
|
+
# raise "Whoops, error back from PMP: #{response.status}"
|
21
|
+
# else
|
22
|
+
# raise "Unrecongized status code: #{response.status}"
|
23
|
+
# end
|
24
|
+
# end
|
25
25
|
|
26
26
|
def body
|
27
27
|
self.raw.body
|
data/lib/pmp/version.rb
CHANGED
data/pmp.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |gem|
|
|
27
27
|
gem.add_development_dependency('guard-bundler')
|
28
28
|
gem.add_development_dependency('guard-minitest')
|
29
29
|
gem.add_development_dependency('simplecov')
|
30
|
+
gem.add_development_dependency('coveralls')
|
30
31
|
|
31
32
|
gem.add_runtime_dependency('faraday')
|
32
33
|
gem.add_runtime_dependency('faraday_middleware')
|
data/spec/client_spec.rb
CHANGED
@@ -21,7 +21,6 @@ describe PMP::Client do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "root doc can be loaded" do
|
24
|
-
|
25
24
|
root_doc = json_file(:collection_root)
|
26
25
|
|
27
26
|
stub_request(:get, "https://api.pmp.io/").
|
@@ -32,6 +31,17 @@ describe PMP::Client do
|
|
32
31
|
@root.creator.must_be_instance_of PMP::Link
|
33
32
|
end
|
34
33
|
|
34
|
+
it "calls the root doc when client does not have method" do
|
35
|
+
root_doc = json_file(:collection_root)
|
36
|
+
|
37
|
+
stub_request(:get, "https://api.pmp.io/").
|
38
|
+
with(:headers => {'Accept'=>'application/vnd.pmp.collection.doc+json', 'Content-Type'=>'application/vnd.pmp.collection.doc+json', 'Host'=>'api.pmp.io:443'}).
|
39
|
+
to_return(:status => 200, :body => root_doc, :headers => {})
|
40
|
+
|
41
|
+
@pmp.creator.must_be_instance_of PMP::Link
|
42
|
+
end
|
43
|
+
|
44
|
+
|
35
45
|
it "gets a credentials object" do
|
36
46
|
@pmp.credentials.wont_be_nil
|
37
47
|
end
|
@@ -100,6 +100,25 @@ describe PMP::CollectionDocument do
|
|
100
100
|
@doc.oauth_token.must_equal 'thisisatesttoken'
|
101
101
|
end
|
102
102
|
|
103
|
+
it "should should get oauth_token if missing" do
|
104
|
+
|
105
|
+
response_body = {
|
106
|
+
access_token: 'thisisalsoatesttoken',
|
107
|
+
token_type: "Bearer",
|
108
|
+
token_issue_date: DateTime.now,
|
109
|
+
token_expires_in: 24*60*60
|
110
|
+
}.to_json
|
111
|
+
|
112
|
+
stub_request(:post, "https://api.pmp.io/auth/access_token").
|
113
|
+
with(:body => {"grant_type"=>"client_credentials"},
|
114
|
+
:headers => {'Accept'=>'application/json', 'Authorization'=>'Basic Og==', 'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'api.pmp.io:443'}).
|
115
|
+
to_return(:status => 200, :body => response_body, :headers => {'Content-Type' => 'application/json; charset=utf-8'})
|
116
|
+
|
117
|
+
@doc.oauth_token = nil
|
118
|
+
@doc.setup_oauth_token
|
119
|
+
@doc.oauth_token.must_equal 'thisisalsoatesttoken'
|
120
|
+
end
|
121
|
+
|
103
122
|
it "should load" do
|
104
123
|
@doc.wont_be :loaded
|
105
124
|
@doc.load
|
@@ -148,6 +167,15 @@ describe PMP::CollectionDocument do
|
|
148
167
|
queries["urn:pmp:query:docs"].must_be_instance_of PMP::Link
|
149
168
|
end
|
150
169
|
|
170
|
+
it "should handle 404 results on a search" do
|
171
|
+
|
172
|
+
stub_request(:get, "https://api-sandbox.pmp.io/docs").
|
173
|
+
with(:headers => {'Accept'=>'application/vnd.pmp.collection.doc+json', 'Authorization'=>'Bearer thisisatesttoken', 'Content-Type'=>'application/vnd.pmp.collection.doc+json', 'Host'=>'api-sandbox.pmp.io:443', 'User-Agent'=>'PMP Ruby Gem 0.2.0'}).
|
174
|
+
to_return(:status=>404, :body=>"Not Found", :remote_ip=>"107.20.158.113", :headers=>{"Access-Control-Allow-Headers"=>"origin, x-http-method-override, accept, content-type, authorization, x-pingother", "Access-Control-Allow-Methods"=>"GET,OPTIONS,HEAD,PUT,POST,DELETE,PATCH", "Access-Control-Allow-Origin"=>"*", "Content-Type"=>"application/vnd.pmp.collection.doc+json", "Date"=>"Thu, 24 Oct 2013 17:20:04 GMT", "Vary"=>"Accept-Encoding", "X-Powered-By"=>"Express", "X-Response-Time"=>"18ms", "Content-Length"=>"9", "Connection"=>"keep-alive"})
|
175
|
+
|
176
|
+
@doc.query["urn:pmp:query:docs"].items.must_equal []
|
177
|
+
end
|
178
|
+
|
151
179
|
end
|
152
180
|
|
153
181
|
describe 'persistence' do
|
data/spec/response_spec.rb
CHANGED
@@ -18,11 +18,15 @@ describe PMP::Response do
|
|
18
18
|
response = PMP::Response.new(@raw, @request)
|
19
19
|
end
|
20
20
|
|
21
|
-
it "can raise an error" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
# it "can raise an error" do
|
22
|
+
# raw = Minitest::Mock.new
|
23
|
+
# raw.expect(:status, 500).expect(:status, 500)
|
24
|
+
# proc{ PMP::Response.new(raw, @request) }.must_raise RuntimeError
|
25
|
+
|
26
|
+
# raw = Minitest::Mock.new
|
27
|
+
# raw.expect(:status, 600).expect(:status, 600)
|
28
|
+
# proc{ PMP::Response.new(raw, @request) }.must_raise RuntimeError
|
29
|
+
# end
|
26
30
|
|
27
31
|
it "can return body" do
|
28
32
|
@raw.expect(:body, {foo: 'bar'})
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
3
|
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
4
6
|
SimpleCov.command_name 'Unit Tests'
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
5
11
|
SimpleCov.start
|
6
12
|
|
7
13
|
require 'minitest'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kuklewicz
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - '>='
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: coveralls
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: faraday
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|