Class: AMEE::Data::Category

Inherits:
Object show all
Defined in:
lib/amee/data_category.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#path

Attributes inherited from Object

#connection, #created, #modified, #name, #path, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#full_path

Methods inherited from Object

#expire_cache, get_and_parse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ Category

Returns a new instance of Category.



10
11
12
13
14
15
16
# File 'lib/amee/data_category.rb', line 10

def initialize(data = {})
  @children = data ? data[:children] : []
  @items = data ? data[:items] : []
  @pager = data ? data[:pager] : nil
  @itemdef = data ? data[:itemdef] : nil
  super
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



18
19
20
# File 'lib/amee/data_category.rb', line 18

def children
  @children
end

#itemdefObject (readonly)

Returns the value of attribute itemdef.



21
22
23
# File 'lib/amee/data_category.rb', line 21

def itemdef
  @itemdef
end

#itemsObject (readonly)

Returns the value of attribute items.



19
20
21
# File 'lib/amee/data_category.rb', line 19

def items
  @items
end

#pagerObject (readonly)

Returns the value of attribute pager.



20
21
22
# File 'lib/amee/data_category.rb', line 20

def pager
  @pager
end

Class Method Details

.create(category, options = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/amee/data_category.rb', line 154

def self.create(category, options = {})

  connection = category.connection
  path = category.full_path

  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)

  get_item = true if get_item.nil?
  # Store format if set
  format = options[:format]
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Third argument must be a hash of options!")
  end
  # Send data to path
  options[:newObjectType] = "DC"
  response = connection.post(path, options)
  if response.headers_hash.has_key?('Location') && response.headers_hash['Location']
    location = response.headers_hash['Location'].match("https??://.*?(/.*)")[1]
  else
    category = Category.parse(connection, response.body)
    location = category.full_path
  end
  if get_item == true
    get_options = {}
    get_options[:format] = format if format
    return AMEE::Data::Category.get(connection, location, get_options)
  else
    return location
  end
rescue
  raise AMEE::BadData.new("Couldn't create DataCategory. Check that your information is correct.\n#{response}")
end

.delete(connection, path) ⇒ Object



188
189
190
191
192
# File 'lib/amee/data_category.rb', line 188

def self.delete(connection, path)
  connection.delete(path)
rescue
  raise AMEE::BadData.new("Couldn't delete DataCategory. Check that your information is correct.")
end

.from_json(json) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/amee/data_category.rb', line 28

def self.from_json(json)
  # Parse json
  doc = JSON.parse(json)
  begin
    data = {}
    data[:uid] = doc['dataCategory']['uid']
    data[:created] = DateTime.parse(doc['dataCategory']['created'])
    data[:modified] = DateTime.parse(doc['dataCategory']['modified'])
    data[:name] = doc['dataCategory']['name']
    data[:path] = doc['path']
    data[:children] = []
    data[:pager] = AMEE::Pager.from_json(doc['children']['pager'])
    itemdef=doc['dataCategory']['itemDefinition']
    data[:itemdef] = itemdef ? itemdef['uid'] : nil
    doc['children']['dataCategories'].each do |child|
      category_data = {}
      category_data[:name] = child['name']
      category_data[:path] = child['path']
      category_data[:uid] = child['uid']
      data[:children] << category_data
    end
    data[:items] = []
    if doc['children']['dataItems']['rows']
      doc['children']['dataItems']['rows'].each do |item|
        item_data = {}
        item.each_pair do |key, value|
          item_data[key.to_sym] = value
        end
        data[:items] << item_data
      end
    end
    # Create object
    Category.new(data)
  rescue AMEE::JSONParseError
    raise AMEE::BadData.new("Couldn't load DataCategory from JSON data. Check that your URL is correct.\n#{json}")
  end
end

.from_xml(xml) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/amee/data_category.rb', line 69

def self.from_xml(xml)
  # Parse XML
  @doc = doc= REXML::Document.new(xml)
  begin
    data = {}
    data[:uid] = x '@uid'
    data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@created").to_s)
    data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@modified").to_s)
    data[:name] = (REXML::XPath.first(doc, '/Resources/DataCategoryResource/DataCategory/Name') || REXML::XPath.first(doc, '/Resources/DataCategoryResource/DataCategory/name')).text
    data[:path] = (REXML::XPath.first(doc, '/Resources/DataCategoryResource/Path') || REXML::XPath.first(doc, '/Resources/DataCategoryResource/DataCategory/path')).text || ""
    data[:pager] = AMEE::Pager.from_xml(REXML::XPath.first(doc, '//Pager'))
    itemdefattrib=REXML::XPath.first(doc, '/Resources/DataCategoryResource//ItemDefinition/@uid')
    data[:itemdef] = itemdefattrib ? itemdefattrib.to_s : nil
    data[:children] = []
    REXML::XPath.each(doc, '/Resources/DataCategoryResource//Children/DataCategories/DataCategory') do |child|
      category_data = {}
      category_data[:name] = (child.elements['Name'] || child.elements['name']).text
      category_data[:path] = (child.elements['Path'] || child.elements['path']).text
      category_data[:uid] = child.attributes['uid'].to_s
      data[:children] << category_data
    end
    data[:items] = []
    REXML::XPath.each(doc, '/Resources/DataCategoryResource//Children/DataItems/DataItem') do |item|
      item_data = {}
      item_data[:uid] = item.attributes['uid'].to_s
      item.elements.each do |element|
        item_data[element.name.to_sym] = element.text
      end
      if item_data[:path].nil?
        item_data[:path] = item_data[:uid]
      end
      data[:items] << item_data
    end
    # Create object
    Category.new(data)
  rescue AMEE::XMLParseError
    raise AMEE::BadData.new("Couldn't load DataCategory from XML data. Check that your URL is correct.\n#{xml}")
  end
end

.get(connection, path, orig_options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/amee/data_category.rb', line 109

def self.get(connection, path, orig_options = {})
  unless orig_options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Third argument must be a hash of options!")
  end
  # Convert to AMEE options
  options = orig_options.clone
  if orig_options[:items_per_page]
    options[:itemsPerPage] = orig_options[:items_per_page]
  else
    options[:itemsPerPage] = 10
  end

  # Load data from path
  cat = get_and_parse(connection, path, options)

  # Store connection in object for future use
  cat.connection = connection
  # Done
  return cat
end

.root(connection) ⇒ Object



130
131
132
# File 'lib/amee/data_category.rb', line 130

def self.root(connection)
  self.get(connection, '/data')
end

.update(connection, path, options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/amee/data_category.rb', line 194

def self.update(connection, path, options = {})
  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)
  get_item = true if get_item.nil?
  # Go
  response = connection.put(path, options)
  if get_item
    if response.body.empty?
      return Category.get(connection, path)
    else
      return Category.parse(connection, response.body)
    end
  end
rescue
  raise AMEE::BadData.new("Couldn't update Data Category. Check that your information is correct.")
end

.xmlpathpreambleObject



66
67
68
# File 'lib/amee/data_category.rb', line 66

def self.xmlpathpreamble
  "/Resources/DataCategoryResource/DataCategory/"
end

Instance Method Details

#child(child_path) ⇒ Object



134
135
136
# File 'lib/amee/data_category.rb', line 134

def child(child_path)
  AMEE::Data::Category.get(connection, "#{full_path}/#{child_path}")
end

#drillObject



138
139
140
# File 'lib/amee/data_category.rb', line 138

def drill
  AMEE::Data::DrillDown.get(connection, "#{full_path}/drill")
end

#item(options) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/amee/data_category.rb', line 142

def item(options)
  # Search fields - from most specific to least specific
  item = items.find{ |x| (x[:uid] && x[:uid] == options[:uid]) ||
                         (x[:name] && x[:name] == options[:name]) ||
                         (x[:path] && x[:path] == options[:path]) ||
                         (x[:label] && x[:label] == options[:label]) }
  # Pass through some options
  new_opts = {}
  new_opts[:format] = options[:format] if options[:format]
  item ? AMEE::Data::Item.get(connection, "#{full_path}/#{item[:path]}", new_opts) : nil
end

#item_definitionObject



23
24
25
26
# File 'lib/amee/data_category.rb', line 23

def item_definition
  return nil unless itemdef
  @item_definition ||= AMEE::Admin::ItemDefinition.load(connection,itemdef)
end