class Almodovar::SingleResource

Public Class Methods

new(url, auth, xml = nil, options = {}) click to toggle source
# File lib/almodovar/single_resource.rb, line 8
def initialize(url, auth, xml = nil, options = {})
  @url = url
  @auth = auth
  @xml = xml
  @options = options
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/almodovar/single_resource.rb, line 47
def [](key) # for resources with type "document"
  return super unless xml.at_xpath("/*[@type='document']")
  Hash.from_xml(xml.to_xml).values.first[key]
end
delete(extra_query_params = {}) click to toggle source
# File lib/almodovar/single_resource.rb, line 23
def delete(extra_query_params = {})
  check_errors(http.delete(@url, extra_query_params), @url, extra_query_params)
end
respond_to?(meth, include_all=false) click to toggle source
Calls superclass method
# File lib/almodovar/single_resource.rb, line 52
def respond_to?(meth, include_all=false)
  super || (node(meth) != nil) || (link(meth) != nil)
end
to_hash() click to toggle source
# File lib/almodovar/single_resource.rb, line 31
def to_hash
  xml_without_type = xml.dup.tap do |xml|
    xml.delete("type")
  end

  if xml_without_type.content.strip.empty?
    {}
  else
    xml_hash = Hash.from_xml(xml_without_type.to_s)
    xml_hash[xml_without_type.name]
  end
end
update(attrs = {}) click to toggle source
# File lib/almodovar/single_resource.rb, line 15
def update(attrs = {})
  raise ArgumentError.new("You must specify one only root element which is the type of resource (e.g. `:project => { :name => 'Wadus' }` instead of just `:name => 'Wadus'`)") if attrs.size > 1
  root, body = attrs.first
  response = http.put(@url, body.to_xml(root: root), {}, { "Content-Type" => "application/xml" })
  check_errors(response, @url)
  @xml = Nokogiri::XML.parse(response.body).root
end
url() click to toggle source
# File lib/almodovar/single_resource.rb, line 27
def url
  @url ||= xml.at_xpath("./link[@rel='self']").try(:[], "href")
end

Private Instance Methods

attribute_name(attribute) click to toggle source
# File lib/almodovar/single_resource.rb, line 92
def attribute_name(attribute)
  attribute.to_s.gsub('_', '-')
end
method_missing(meth, *args, &blk) click to toggle source
Calls superclass method
# File lib/almodovar/single_resource.rb, line 58
def method_missing(meth, *args, &blk)
  if node = node(meth)
    return node['type'] == 'document' ? Resource.from_xml(node.to_xml) : node_text(node)
  end

  link = link(meth)
  return Resource.new(link["href"], @auth, link.at_xpath("./*"), *args) if link

  super
end
node(name) click to toggle source
# File lib/almodovar/single_resource.rb, line 69
def node(name)
  xml.at_xpath("./*[name()='#{name}' or name()='#{attribute_name(name)}']")
end
node_text(node) click to toggle source
# File lib/almodovar/single_resource.rb, line 77
def node_text(node)
  case node['type']
  when "integer"
    node.text.to_i
  when "datetime"
    Time.parse(node.text)
  when "date"
    Date.parse(node.text)
  when "array"
    return node.element_children.map { |c| node_text(c) }
  else
    node.text
  end
end