Class: Freeb::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/freeb/topic.rb

Instance Method Summary collapse

Constructor Details

#initialize(id_or_hash) ⇒ Topic

Returns a new instance of Topic.



5
6
7
8
9
10
11
12
13
14
# File 'lib/freeb/topic.rb', line 5

def initialize(id_or_hash)
  if id_or_hash.is_a?(String)
    response = API.get(id)
  else
    response = id_or_hash
  end
  init_with_response(response)
  return nil if @raw_data.blank?
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



53
54
55
# File 'lib/freeb/topic.rb', line 53

def method_missing(method, *args)
  get_property(method)
end

Instance Method Details

#[](key) ⇒ Object



47
48
49
# File 'lib/freeb/topic.rb', line 47

def [](key)
  get_property(key)
end

#descriptionObject



21
22
23
# File 'lib/freeb/topic.rb', line 21

def description
  @raw_data["description"] ||= API.description(id)
end

#get_property(property) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/freeb/topic.rb', line 29

def get_property(property)
  property = property.to_s
  return nil if @raw_data.blank?
  return description if property == "description"
  return @raw_data[property] unless @raw_data[property].blank?
  unless @raw_data['type'].blank?
    id = "#{@raw_data['type']}/#{property}"
    return @raw_data[id] unless @raw_data[id].blank?
  end
  mql = {
    "id" => get_property(:id),
    property => nil
  }
  result = API.mqlread(mql)
  @raw_data[property] = result[property]
  return @raw_data[property]
end

#init_with_response(response) ⇒ Object



16
17
18
19
# File 'lib/freeb/topic.rb', line 16

def init_with_response(response)
  raise_error
  @raw_data = response
end

#raw_dataObject



25
26
27
# File 'lib/freeb/topic.rb', line 25

def raw_data
  @raw_data
end