Class: AMEE::Data::Category

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

Instance Attribute Summary collapse

Attributes inherited from Object

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#full_path

Constructor Details

#initialize(data = {}) ⇒ Category

Returns a new instance of Category.



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

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/amee/data_category.rb', line 13

def children
  @children
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Class Method Details

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/amee/data_category.rb', line 137

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("http://.*?(/.*)")[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



171
172
173
174
175
# File 'lib/amee/data_category.rb', line 171

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/amee/data_category.rb', line 16

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] = []
  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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/amee/data_category.rb', line 49

def self.from_xml(xml)
  # Parse XML
  doc = REXML::Document.new(xml)
  data = {}
  data[:uid] = REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@uid").to_s
  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/?ame').text
  data[:path] = REXML::XPath.first(doc, '/Resources/DataCategoryResource//?ath').text || ""
  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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amee/data_category.rb', line 84

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



113
114
115
# File 'lib/amee/data_category.rb', line 113

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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/amee/data_category.rb', line 177

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

Instance Method Details

#child(child_path) ⇒ Object



117
118
119
# File 'lib/amee/data_category.rb', line 117

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

#drillObject



121
122
123
# File 'lib/amee/data_category.rb', line 121

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

#item(options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/amee/data_category.rb', line 125

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