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 included from ParseHelper

#xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ Category

Returns a new instance of Category.



7
8
9
10
11
12
13
# File 'lib/amee/data_category.rb', line 7

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.



15
16
17
# File 'lib/amee/data_category.rb', line 15

def children
  @children
end

#itemdefObject (readonly)

Returns the value of attribute itemdef.



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

def itemdef
  @itemdef
end

#itemsObject (readonly)

Returns the value of attribute items.



16
17
18
# File 'lib/amee/data_category.rb', line 16

def items
  @items
end

#pagerObject (readonly)

Returns the value of attribute pager.



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

def pager
  @pager
end

Class Method Details

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



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
187
# File 'lib/amee/data_category.rb', line 155

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['Location']
    location = response['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



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

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



25
26
27
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
# File 'lib/amee/data_category.rb', line 25

def self.from_json(json)
  # Parse json
  doc = JSON.parse(json)
  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
  raise AMEE::BadData.new("Couldn't load DataCategory from JSON data. Check that your URL is correct.\n#{json}")
end

.from_xml(xml) ⇒ Object



64
65
66
67
68
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
# File 'lib/amee/data_category.rb', line 64

def self.from_xml(xml)
  # Parse XML
  @doc = doc= REXML::Document.new(xml)
  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
  raise AMEE::BadData.new("Couldn't load DataCategory from XML data. Check that your URL is correct.\n#{xml}")
end

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/amee/data_category.rb', line 102

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
  response = connection.get(path, options).body

  # Parse data from response
  if response.is_json?
    cat = Category.from_json(response)
  else
    cat = Category.from_xml(response)
  end
  # Store connection in object for future use
  cat.connection = connection
  # Done
  return cat
rescue
  raise AMEE::BadData.new("Couldn't load DataCategory. Check that your URL is correct.\n#{response}")
end

.root(connection) ⇒ Object



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

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

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



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

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



61
62
63
# File 'lib/amee/data_category.rb', line 61

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

Instance Method Details

#child(child_path) ⇒ Object



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

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

#drillObject



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

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

#item(options) ⇒ Object



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

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



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

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