pmp 0.1.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.
@@ -0,0 +1,178 @@
1
+ {
2
+ "attributes": {
3
+ "created": "2013-10-01T15:57:55+00:00",
4
+ "guid": "04224975-e93c-4b17-9df9-96db37d318f3",
5
+ "modified": "2013-10-04T14:45:34+00:00",
6
+ "published": "2013-07-04T04:00:44+00:00",
7
+ "title": "PMP Home Document",
8
+ "valid": {
9
+ "from": "2013-07-04T04:00:44+00:00",
10
+ "to": "3013-07-04T04:00:44+00:00"
11
+ }
12
+ },
13
+ "links": {
14
+ "creator": [
15
+ {
16
+ "href": "https://api-sandbox.pmp.io/docs/af676335-21df-4486-ab43-e88c1b48f026"
17
+ }
18
+ ],
19
+ "edit": [
20
+ {
21
+ "href": "https://publish-sandbox.pmp.io/docs",
22
+ "method": "PUT"
23
+ }
24
+ ],
25
+ "navigation": [
26
+ {
27
+ "href": "https://api-sandbox.pmp.io/docs?guid=04224975-e93c-4b17-9df9-96db37d318f3",
28
+ "rels": [
29
+ "urn:pmp:navigation:self"
30
+ ]
31
+ }
32
+ ],
33
+ "query": [
34
+ {
35
+ "hints": {
36
+ "allow": [
37
+ "GET"
38
+ ]
39
+ },
40
+ "href-template": "https://api-sandbox.pmp.io/users{?limit,offset,tag,collection,text,searchsort,has}",
41
+ "href-vars": {
42
+ "collection": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
43
+ "has": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
44
+ "limit": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
45
+ "offset": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
46
+ "searchsort": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
47
+ "tag": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
48
+ "text": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
49
+ },
50
+ "rels": [
51
+ "urn:pmp:query:users"
52
+ ],
53
+ "title": "Query for users"
54
+ },
55
+ {
56
+ "hints": {
57
+ "allow": [
58
+ "GET"
59
+ ]
60
+ },
61
+ "href-template": "https://api-sandbox.pmp.io/groups{?limit,offset,tag,collection,text,searchsort,has}",
62
+ "href-vars": {
63
+ "collection": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
64
+ "limit": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
65
+ "offset": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
66
+ "searchsort": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
67
+ "tag": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
68
+ "text": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
69
+ },
70
+ "rels": [
71
+ "urn:pmp:query:groups"
72
+ ],
73
+ "title": "Query for groups"
74
+ },
75
+ {
76
+ "hints": {
77
+ "allow": [
78
+ "GET",
79
+ "PUT",
80
+ "DELETE"
81
+ ]
82
+ },
83
+ "href-template": "https://api-sandbox.pmp.io/profiles{/guid}",
84
+ "href-vars": {
85
+ "guid": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
86
+ },
87
+ "rels": [
88
+ "urn:pmp:hreftpl:profiles"
89
+ ],
90
+ "title": "Access profiles"
91
+ },
92
+ {
93
+ "hints": {
94
+ "allow": [
95
+ "GET",
96
+ "PUT",
97
+ "DELETE"
98
+ ]
99
+ },
100
+ "href-template": "https://api-sandbox.pmp.io/schemas{/guid}",
101
+ "href-vars": {
102
+ "guid": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
103
+ },
104
+ "rels": [
105
+ "urn:pmp:hreftpl:schemas"
106
+ ],
107
+ "title": "Access schemas",
108
+ "type": "application/schema+json"
109
+ },
110
+ {
111
+ "hints": {
112
+ "allow": [
113
+ "GET",
114
+ "PUT",
115
+ "DELETE"
116
+ ]
117
+ },
118
+ "href-template": "https://api-sandbox.pmp.io/docs{/guid}{?limit,offset}",
119
+ "href-vars": {
120
+ "guid": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
121
+ "limit": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
122
+ "offset": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
123
+ },
124
+ "rels": [
125
+ "urn:pmp:hreftpl:docs"
126
+ ],
127
+ "title": "Access documents"
128
+ },
129
+ {
130
+ "hints": {
131
+ "allow": [
132
+ "GET"
133
+ ]
134
+ },
135
+ "href-template": "https://api-sandbox.pmp.io/docs{?limit,offset,tag,collection,text,searchsort,has,author,distributor,distributorgroup,startdate,enddate,profile,language}",
136
+ "href-vars": {
137
+ "author": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
138
+ "collection": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
139
+ "distributor": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
140
+ "distributorgroup": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
141
+ "enddate": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
142
+ "has": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
143
+ "language": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
144
+ "limit": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
145
+ "offset": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
146
+ "profile": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
147
+ "searchsort": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
148
+ "startdate": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
149
+ "tag": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
150
+ "text": "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
151
+ },
152
+ "rels": [
153
+ "urn:pmp:query:docs"
154
+ ],
155
+ "title": "Query for documents"
156
+ },
157
+ {
158
+ "href": "https://api-sandbox.pmp.io/guids",
159
+ "method": "POST",
160
+ "rels": [
161
+ "urn:pmp:query:guids"
162
+ ],
163
+ "title": "Generate guids",
164
+ "type": "application/json"
165
+ },
166
+ {
167
+ "href": "https://publish-sandbox.pmp.io/files",
168
+ "method": "POST",
169
+ "rels": [
170
+ "urn:pmp:query:files"
171
+ ],
172
+ "title": "Upload media files",
173
+ "type": "multipart/form-data"
174
+ }
175
+ ]
176
+ },
177
+ "version": "1.0"
178
+ }
data/spec/link_spec.rb ADDED
@@ -0,0 +1,74 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ require 'pmp/link'
6
+
7
+ require 'webmock/minitest'
8
+
9
+ describe PMP::Link do
10
+
11
+ before(:each) {
12
+ @parent = Minitest::Mock.new
13
+ @info = {'href' => 'http://api-sandbox.pmp.io/docs/'}
14
+ @link = PMP::Link.new(@parent, @info)
15
+ }
16
+
17
+ it "can create a new link" do
18
+ @link.wont_be_nil
19
+ end
20
+
21
+ it "can save params to attributes" do
22
+ @link.href.must_equal 'http://api-sandbox.pmp.io/docs/'
23
+ end
24
+
25
+ it "can retrieve link resource" do
26
+ @link.href.must_equal 'http://api-sandbox.pmp.io/docs/'
27
+ end
28
+
29
+ it "can become json" do
30
+ @link.as_json.must_equal @info
31
+ end
32
+
33
+ it "gets a url based on an href string" do
34
+ @link.url.must_equal 'http://api-sandbox.pmp.io/docs/'
35
+ end
36
+
37
+ it "can get a link for templated href" do
38
+ l = {
39
+ "hints" => {
40
+ "allow" => [
41
+ "GET"
42
+ ]
43
+ },
44
+ "href-template" => "https://api-sandbox.pmp.io/docs{?limit,offset,tag,collection,text,searchsort,has,author,distributor,distributorgroup,startdate,enddate,profile,language}",
45
+ "href-vars" => {
46
+ "author" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
47
+ "collection" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
48
+ "distributor" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
49
+ "distributorgroup" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
50
+ "enddate" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
51
+ "has" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
52
+ "language" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
53
+ "limit" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
54
+ "offset" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
55
+ "profile" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
56
+ "searchsort" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
57
+ "startdate" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
58
+ "tag" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval",
59
+ "text" => "https://github.com/publicmediaplatform/pmpdocs/wiki/Content-Retrieval"
60
+ },
61
+ "rels" => [
62
+ "urn:pmp:query:docs"
63
+ ],
64
+ "title" => "Query for documents"
65
+ }
66
+
67
+ @link = PMP::Link.new(@parent, l)
68
+ @link.hints.must_equal l['hints']
69
+ @link.url.must_equal "https://api-sandbox.pmp.io/docs"
70
+ @link.where('limit' => 10).url.must_equal "https://api-sandbox.pmp.io/docs?limit=10"
71
+ @link.url.must_equal "https://api-sandbox.pmp.io/docs"
72
+ end
73
+
74
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ require 'pmp/links'
6
+
7
+
8
+ describe PMP::Links do
9
+
10
+ before(:each) {
11
+ @parent = Minitest::Mock.new
12
+ @links = PMP::Links.new(@parent)
13
+ }
14
+
15
+ it "can create a new links obj" do
16
+ @links.wont_be_nil
17
+ end
18
+
19
+ it "can access _parent" do
20
+ @links.parent = {}
21
+ @links.parent.wont_be_nil
22
+ end
23
+
24
+ it "can have a link assigned" do
25
+ link = {}
26
+ @parent.expect(:foo=, link, Array(Object))
27
+ @links['foo'] = link
28
+ @links['foo'].must_equal link
29
+ end
30
+
31
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ require 'ostruct'
6
+
7
+ require 'pmp/parser'
8
+
9
+ class TestParser < OpenStruct
10
+ include PMP::Parser
11
+
12
+ attr_accessor :version
13
+
14
+ def links
15
+ @links ||= PMP::Links.new(self)
16
+ end
17
+
18
+ def attributes
19
+ marshal_dump.delete_if{|k,v| links.keys.include?(k)}
20
+ end
21
+
22
+ end
23
+
24
+ describe PMP::Parser do
25
+
26
+ it "will not parse nil document" do
27
+ tc = TestParser.new
28
+ tc.parse(nil)
29
+ end
30
+
31
+ it "will un-parse to hash for json serialization" do
32
+ tc = TestParser.new
33
+ tc.parse(json_fixture(:collection_basic))
34
+ hash = tc.as_json
35
+ # puts "basic as_json: #{hash}"
36
+ hash.keys.sort.must_equal ['attributes', 'links', 'version']
37
+ hash['attributes']['guid'].must_equal "f84e9018-5c21-4b32-93f8-d519308620f0"
38
+ hash['links'].keys.sort.must_equal ["collection", "profile", "self"]
39
+ end
40
+
41
+ it "parses query links into a hash based on rels" do
42
+ tc = TestParser.new
43
+ tc.parse(json_fixture(:collection_root))
44
+ tc.query.must_be_instance_of Hash
45
+ tc.query.keys.sort.must_equal ["urn:pmp:hreftpl:docs", "urn:pmp:hreftpl:profiles", "urn:pmp:hreftpl:schemas", "urn:pmp:query:docs", "urn:pmp:query:files", "urn:pmp:query:groups", "urn:pmp:query:guids", "urn:pmp:query:users"]
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ require 'ostruct'
6
+
7
+ require 'pmp/response'
8
+
9
+ describe PMP::Response do
10
+
11
+ before {
12
+ @raw = Minitest::Mock.new
13
+ @raw.expect(:status, 200)
14
+ @request = {}
15
+ }
16
+
17
+ it "can make a new response" do
18
+ response = PMP::Response.new(@raw, @request)
19
+ end
20
+
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
+ end
26
+
27
+ it "can return body" do
28
+ @raw.expect(:body, {foo: 'bar'})
29
+ response = PMP::Response.new(@raw, @request)
30
+ response.body[:foo].must_equal 'bar'
31
+ end
32
+
33
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'simplecov'
4
+ SimpleCov.command_name 'Unit Tests'
5
+ SimpleCov.start
6
+
7
+ require 'minitest'
8
+ require 'minitest/autorun'
9
+ require 'minitest/spec'
10
+ require 'minitest/mock'
11
+ require 'webmock/minitest'
12
+ require 'hashie/mash'
13
+
14
+ require 'pmp'
15
+
16
+ # helper method to create mashified test docs, that look like what comes out of the faraday middleware
17
+ def mashify(body)
18
+ case body
19
+ when Hash
20
+ ::Hashie::Mash.new(body)
21
+ when Array
22
+ body.map { |item| parse(item) }
23
+ else
24
+ body
25
+ end
26
+ end
27
+
28
+ def json_fixture(name)
29
+ mashify(JSON.parse(json_file(name)))
30
+ end
31
+
32
+ def json_file(name)
33
+ File.read( File.dirname(__FILE__) + "/fixtures/#{name}.json")
34
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ require 'pmp/token'
6
+
7
+ require 'webmock/minitest'
8
+
9
+ describe PMP::Token do
10
+
11
+ it "gets the base path for this subclass of API" do
12
+ token = PMP::Token.new
13
+ token.token_url.must_equal '/auth/access_token'
14
+ end
15
+
16
+ it "gets an access_token" do
17
+ access_token = "thisisnotanaccesstokenno"
18
+ response_body = {
19
+ access_token: access_token,
20
+ token_type: "Bearer",
21
+ token_issue_date: DateTime.now,
22
+ token_expires_in: 24*60*60
23
+ }.to_json
24
+
25
+ stub_request(:post, "https://api-sandbox.pmp.io/auth/access_token").
26
+ with(:body => {"grant_type"=>"client_credentials"},
27
+ :headers => {'Accept'=>'application/json', 'Authorization'=>'Basic dGhpc2lzbm90YS1yZWFsLWNsaWVudC1pZC1zb3Zlcnlzb3JyeTp0aGlzaXNub3RhcmVhbHNlY3JldGVpdGhlcg==', 'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'api-sandbox.pmp.io:443'}).
28
+ to_return(:status => 200, :body => response_body, :headers => {'Content-Type' => 'application/json; charset=utf-8'})
29
+
30
+ client_id = "thisisnota-real-client-id-soverysorry"
31
+ client_secret = "thisisnotarealsecreteither"
32
+ endpoint = "https://api-sandbox.pmp.io"
33
+ token_path = '/auth/access_token'
34
+
35
+ token_api= PMP::Token.new(client_id: client_id, client_secret: client_secret, endpoint: endpoint)
36
+ token = token_api.get_token
37
+ token.token.must_equal access_token
38
+ end
39
+
40
+ end