Class: SmugMugAPI::XMLStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smugmug/xmlstruct.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, feed = false) ⇒ XMLStruct

Returns a new instance of XMLStruct.



14
15
16
17
18
19
20
21
# File 'lib/smugmug/xmlstruct.rb', line 14

def initialize(xml, feed=false)
  @feed = feed
  if xml.is_a?(REXML::Document) && !feed
    @xml = xml.root.elements[2]
  else
    @xml = xml
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/smugmug/xmlstruct.rb', line 88

def method_missing(*args)
  if @feed && args.first == :entries
    entries = @xml.get_elements("//entry").map { |e| XMLStruct.new(e, true) }
  else
    method = args.first.to_s
    if method.match(/each_(.*)/)
      if block_given?
        node = XMLStruct.attrib_key($1, @feed)
        @xml.elements.each("//#{node}") do |xml|
          yield XMLStruct.new(xml, @feed)
        end
      else
        @xml.get_elements("//{#{node}}").map { |e| XMLStruct.new(e, @feed) }
      end
    else
      path = XMLStruct.attrib_key(args.shift, @feed)
      node = @xml.elements[path] rescue nil
      if node
        XMLStruct.new(node, @feed)
      else
        self[path]
      end
    end
  end
end

Class Method Details

.attrib_key(key, feed) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/smugmug/xmlstruct.rb', line 27

def self.attrib_key(key, feed)
  case key
  when "Id", "id", :id
    "id"
  when Fixnum
    key
  else
    if feed
      key.to_s
    else
      SmugMugAPI.camelize(key)
    end
  end
end

Instance Method Details

#[](attrib) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smugmug/xmlstruct.rb', line 42

def [](attrib)
  if attrib.is_a?(Fixnum)
    XMLStruct.new(@xml.elements[attrib], @feed)
  else
    attrib = XMLStruct.attrib_key(attrib, @feed)
    if attrib.match(/(.*)\?$/)
      # special processing for boolean (ends with?) lookups
      @xml.attributes[$1] == "1"
    else
      @xml.attributes[attrib]
    end
  end
end

#[]=(attrib, value) ⇒ Object



72
73
74
# File 'lib/smugmug/xmlstruct.rb', line 72

def []=(attrib,value)
  @xml.attributes[XMLStruct.attrib_key(attrib, @feed)] = value
end

#dumpObject



122
123
124
# File 'lib/smugmug/xmlstruct.rb', line 122

def dump
  @xml.write($stdout, 3, true)
end

#each(path = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/smugmug/xmlstruct.rb', line 76

def each(path=nil)
  if path
    @xml.elements.each(path) do |xml|
      yield XMLStruct.new(xml, @feed)
    end
  else
    @xml.elements.each do |xml|
      yield XMLStruct.new(xml, @feed)
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/smugmug/xmlstruct.rb', line 64

def empty?
  size == 0
end

#feed?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/smugmug/xmlstruct.rb', line 23

def feed?
  @feed
end

#firstObject



56
57
58
# File 'lib/smugmug/xmlstruct.rb', line 56

def first
  XMLStruct.new(@xml.elements[1], @feed)
end

#id(*args) ⇒ Object



10
11
12
# File 'lib/smugmug/xmlstruct.rb', line 10

def id(*args)
  method_missing(:id, *args)
end

#lastObject



68
69
70
# File 'lib/smugmug/xmlstruct.rb', line 68

def last
  XMLStruct.new(@xml.elements[@xml.elements.size], @feed)
end

#sizeObject



60
61
62
# File 'lib/smugmug/xmlstruct.rb', line 60

def size
  @xml.elements.size
end

#to_sObject



114
115
116
# File 'lib/smugmug/xmlstruct.rb', line 114

def to_s
  @xml.text
end

#to_xmlObject



118
119
120
# File 'lib/smugmug/xmlstruct.rb', line 118

def to_xml
  @xml.to_s
end