pmp 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a973bed0cc15ec213cce42e732f6592d627c5e0
4
- data.tar.gz: 3b75ef9379836b6b773e6508c111ecf4b34d4945
3
+ metadata.gz: 1aaa1c5c06065ba12e2b7b0ed571fb423b4bc845
4
+ data.tar.gz: 614cac49e7b7e22ce65a84e8f9378f3ca69cfa78
5
5
  SHA512:
6
- metadata.gz: 6563f341fcbf74f75a8cc19731f8add4812126811710b9a02fd49bedd50efb4cd9c365a3e1eb474f4cd0260b2cb4ca374f53e0c341c926d9bd530f0be58a2fb4
7
- data.tar.gz: 955430a5c6be07476f746f57af4b3d79efdd834447d9442495dd34da982252bcbe778ca7de8a625666c33b591f5ad5e9893420a1c161c7e13defe8816deb3401
6
+ metadata.gz: afc288ea482769b5b8423403242bb0951ed9c7aa9c279da3b687a4d5c7d405596a96bf2459a42052a2a57d21b534368075e5460109d3aa691348f587d677a14c
7
+ data.tar.gz: aa8b076cb62f6b9f0d635836208633a31f79129c6aa77dc210f744abb6bd8cb6e7558d6e755df31e071b787e3d5e8bc693d116d6670ca901872cb3ab0d321cb4
data/lib/pmp/client.rb CHANGED
@@ -11,11 +11,11 @@ module PMP
11
11
  end
12
12
 
13
13
  def token(opts={})
14
- PMP::Token.new(options.merge(opts)).get_token
14
+ @token ||= PMP::Token.new(options.merge(opts)).get_token
15
15
  end
16
16
 
17
- def root(opts={}, &block)
18
- PMP::CollectionDocument.new(options.merge(opts), &block)
17
+ def root(opts={}, &block)
18
+ @root ||= PMP::CollectionDocument.new(options.merge(opts), &block)
19
19
  end
20
20
 
21
21
  end
@@ -30,6 +30,10 @@ module PMP
30
30
  # private var to save links obj, to handle link additions
31
31
  attr_accessor :links
32
32
 
33
+ # this is a tricky, read-only list of items, same as if loaded from link->item links
34
+ # do not put into json form of this doc
35
+ attr_accessor :items
36
+
33
37
  # document is the original json derived doc used to create this resource
34
38
  # assumption is that doc is a parsed json doc confirming to collection.doc+json
35
39
  # TODO: check if this is a json string or hash, for now assume it has been mashified
data/lib/pmp/link.rb CHANGED
@@ -25,13 +25,26 @@ module PMP
25
25
 
26
26
  attr_accessor :params
27
27
 
28
- def initialize(parent, link)
28
+ def initialize(parent=PMP::CollectionDocument.new, link={})
29
29
  super()
30
- self.parent = parent || PMP::CollectionDocument.new
30
+ self.parent = parent
31
31
  self.params = link.delete('params') || {}
32
+ # puts "params: #{params.inspect}"
32
33
  parse_attributes(link)
33
34
  end
34
35
 
36
+ def href
37
+ self[:href]
38
+ end
39
+
40
+ def href_template
41
+ self[:href_template]
42
+ end
43
+
44
+ def method
45
+ self[:method]
46
+ end
47
+
35
48
  def attributes
36
49
  HashWithIndifferentAccess.new(marshal_dump)
37
50
  end
@@ -45,17 +58,28 @@ module PMP
45
58
  end
46
59
 
47
60
  def url
61
+ # puts "url href_template: #{href_template}"
62
+ # puts "url href: #{href}"
48
63
  URITemplate.new(href_template || href).expand(params)
49
64
  end
50
65
 
51
66
  def retrieve
52
- @_retrieved ||= parent.request((method || 'get').to_sym, url)
67
+ # puts "retrieve method: #{method}"
68
+ # puts "retrieve url: #{url}"
69
+ parent.request((method || 'get').to_sym, url)
53
70
  end
54
71
 
55
72
  def method_missing(method, *args)
56
- begin
57
- super
58
- rescue NoMethodError => err
73
+ # puts "mm: #{method}"
74
+ # this is a method the link supports, call the link
75
+ # if this is an assignment, assign to the link
76
+ # if you want to assign to a linked doc(s), need to retrieve first
77
+ if self.respond_to?(method)
78
+ self.send(method, *args)
79
+ elsif method.to_s.last == '='
80
+ super
81
+ else
82
+ # puts "mm retrieve and send: #{method}"
59
83
  self.retrieve.send(method, *args)
60
84
  end
61
85
  end
data/lib/pmp/parser.rb CHANGED
@@ -11,9 +11,6 @@ module PMP
11
11
  result['links'] = extract_links
12
12
  result['attributes'] = extract_attributes
13
13
 
14
- # more elegant?
15
- result['attributes'].delete('items')
16
-
17
14
  result
18
15
  end
19
16
 
@@ -74,19 +71,16 @@ module PMP
74
71
  end
75
72
 
76
73
  def parse_link(name, info)
77
-
78
- return parse_query_links(info) if (name.to_s == 'query')
79
-
80
74
  if !info.is_a?(Array)
81
75
  Link.new(self, info)
82
76
  elsif info.size == 1
83
77
  Link.new(self, info.first)
84
78
  elsif info.size > 0
85
- info.map{|l| Link.new(self, l)}
79
+ parse_links_list(info)
86
80
  end
87
81
  end
88
82
 
89
- def parse_query_links(links)
83
+ def parse_links_list(links)
90
84
  links.inject({}) do |results, query|
91
85
  rel = query['rels'].first
92
86
  results[rel] = Link.new(self, query)
data/lib/pmp/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module PMP
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/spec/link_spec.rb CHANGED
@@ -18,6 +18,14 @@ describe PMP::Link do
18
18
  @link.wont_be_nil
19
19
  end
20
20
 
21
+ it "has a parent" do
22
+ @link = PMP::Link.new
23
+ @link.parent.wont_be_nil
24
+
25
+ @link = PMP::Link.new({}, @info)
26
+ @link.parent.must_equal({})
27
+ end
28
+
21
29
  it "can save params to attributes" do
22
30
  @link.href.must_equal 'http://api-sandbox.pmp.io/docs/'
23
31
  end
@@ -31,7 +39,12 @@ describe PMP::Link do
31
39
  end
32
40
 
33
41
  it "gets a url based on an href string" do
34
- @link.url.must_equal 'http://api-sandbox.pmp.io/docs/'
42
+ @link.href.must_equal 'http://api-sandbox.pmp.io/docs/'
43
+ @link.href_template.must_be_nil
44
+ @link.params.must_equal({})
45
+
46
+ url = @link.url
47
+ url.must_equal 'http://api-sandbox.pmp.io/docs/'
35
48
  end
36
49
 
37
50
  it "can get a link for templated href" do
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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz