Class: Springnote::Resource

Inherits:
RestClient::Resource show all
Defined in:
lib/springnote_client/base.rb

Direct Known Subclasses

Page

Constant Summary collapse

XML =
'application/xml'

Instance Attribute Summary collapse

Attributes inherited from RestClient::Resource

#url

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



73
74
75
76
77
78
# File 'lib/springnote_client/base.rb', line 73

def method_missing(method, *args)
  str = method.to_s
  str[-1] == ?= ?
    @hash[str[0..-2]] = args[0] : 
    @hash[str]
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



21
22
23
# File 'lib/springnote_client/base.rb', line 21

def hash
  @hash
end

#holderObject

Returns the value of attribute holder.



21
22
23
# File 'lib/springnote_client/base.rb', line 21

def holder
  @holder
end

Class Method Details

.build(hsh, holder) ⇒ Object



24
25
26
# File 'lib/springnote_client/base.rb', line 24

def build(hsh, holder)
  new_resource(collection_url(holder), holder, hsh)
end

.collection_url(holder, params = {}) ⇒ Object



45
46
47
# File 'lib/springnote_client/base.rb', line 45

def collection_url(holder, params = {})
  holder.url("/#{collection_name}", params)
end

.element_url(holder, id, params = {}) ⇒ Object



41
42
43
# File 'lib/springnote_client/base.rb', line 41

def element_url(holder, id, params = {})
  holder.url("/#{collection_name}/#{id}", params)
end

.find(*args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/springnote_client/base.rb', line 49

def find(*args)
  holder = args.pop

  args.length == 1 ? 
    find_one(args[0], holder) : 
    find_some(args, holder)
end

.find_one(id, holder) ⇒ Object



57
58
59
60
61
# File 'lib/springnote_client/base.rb', line 57

def find_one(id, holder)
  new_resource(element_url(holder, id), holder) do |ret| 
    Hash.from_xml(ret.get(:accept => XML))[ret.singular_name]
  end
end

.find_some(ids, holder) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/springnote_client/base.rb', line 63

def find_some(ids, holder)
  ret = new_resource(collection_url(holder, :identifiers => ids.join(','), :detail => true), holder).get(:accept => 'application/xml')
  Hash.from_xml(ret)[collection_name].map do |item|
    new_resource(element_url(holder, item['identifier']), holder, item)
  end
rescue RuntimeError
  []  
end

.new_resource(url, holder, hash = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/springnote_client/base.rb', line 28

def new_resource(url, holder, hash = nil)
  ret = new(url, holder.username, holder.password)
  ret.holder = holder
  
  if block_given?
    ret.hash = yield(ret)
  elsif hash
    ret.hash = hash
  end

  ret
end

Instance Method Details

#element_urlObject



94
95
96
# File 'lib/springnote_client/base.rb', line 94

def element_url
  self.class.element_url(holder, @hash['identifier'])
end

#inspectObject



102
103
104
# File 'lib/springnote_client/base.rb', line 102

def inspect
  @hash.inspect
end

#saveObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/springnote_client/base.rb', line 80

def save
  xml = @hash.to_xml(:root => singular_name)
  
  if @hash['identifier']
    put xml, :content_type => XML
  else
    ret = post(xml, :content_type => XML)
    @hash = Hash.from_xml(ret)[singular_name]
    self.url = element_url
  end
  
  self
end

#singular_nameObject



98
99
100
# File 'lib/springnote_client/base.rb', line 98

def singular_name
  self.class.singular_name
end

#to_sObject



106
107
108
# File 'lib/springnote_client/base.rb', line 106

def to_s
  @hash.to_s
end