Class: Basuco::Topic

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Topic

initializes a topic using a json result



7
8
9
10
11
# File 'lib/basuco/topic.rb', line 7

def initialize(data)
 # assert_kind_of 'data', data, Hash
  @data = data
  @schema_loaded, @attributes_loaded = false, false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/basuco/topic.rb', line 4

def data
  @data
end

Class Method Details

.get(id) ⇒ Object

Retrieves a topic using the Topic API by Freebase returns a Topic Object.

Examples

Basuco::Topic.get('/en/the_police') => #<Topic id="/en/the_police" name="The Police">

Raises:



20
21
22
23
24
25
# File 'lib/basuco/topic.rb', line 20

def self.get(id)
  #assert_kind_of 'id', id, String
  result = Basuco.search.topic(id)
  raise TopicNotFound unless result
  Basuco::Topic.new(result)
end

Instance Method Details

#aliasesObject

topic aliases



34
35
36
# File 'lib/basuco/topic.rb', line 34

def aliases
  @data["alias"]
end

#attribute(name) ⇒ Object

search for an attribute by name and return it



131
132
133
134
# File 'lib/basuco/topic.rb', line 131

def attribute(name)
  attributes.each { |a| return a if a.property.id == name }
  nil
end

#attributesObject

returns all attributes for every type the topic is an instance of



110
111
112
113
# File 'lib/basuco/topic.rb', line 110

def attributes
  load_attributes! unless attributes_loaded?
  @attributes.values
end

#attributes_loaded?Boolean

returns true if attributes are already loaded

Returns:

  • (Boolean)


144
145
146
# File 'lib/basuco/topic.rb', line 144

def attributes_loaded?
  @attributes_loaded
end

#descriptionObject

topic description



51
52
53
# File 'lib/basuco/topic.rb', line 51

def description
  @data["description"]
end

#idObject

topic id



29
30
31
# File 'lib/basuco/topic.rb', line 29

def id
  @data["id"] || ""
end

#inspectObject



72
73
74
# File 'lib/basuco/topic.rb', line 72

def inspect
  result = "#<Topic id=\"#{id}\" name=\"#{name || "nil"}\">"
end

#nameObject

topic name



45
46
47
# File 'lib/basuco/topic.rb', line 45

def name
  text
end

#propertiesObject

returns all the properties from all assigned types



100
101
102
103
104
105
106
# File 'lib/basuco/topic.rb', line 100

def properties
  @properties = Basuco::Collection.new
  types.each do |type|
    @properties.concat(type.properties)
  end
  @properties
end

#schema_loaded?Boolean

returns true if type information is already loaded

Returns:

  • (Boolean)


138
139
140
# File 'lib/basuco/topic.rb', line 138

def schema_loaded?
  @schema_loaded
end

#textObject

topic text



57
58
59
# File 'lib/basuco/topic.rb', line 57

def text
  @data["text"]
end

#thumbnailObject

topic thumbnail



62
63
64
# File 'lib/basuco/topic.rb', line 62

def thumbnail
  @data["thumbnail"]
end

#to_sObject



67
68
69
# File 'lib/basuco/topic.rb', line 67

def to_s
  name || id || ""
end

#type(type) ⇒ Object

returns individual type based on the requested type id



93
94
95
96
# File 'lib/basuco/topic.rb', line 93

def type(type)
  types.each { |t| return t if t.id =~ /^#{Regexp.escape(type)}$/}
  nil
end

#typesObject

returns all assigned types



86
87
88
89
# File 'lib/basuco/topic.rb', line 86

def types
  load_schema! unless schema_loaded?
  @types
end

#urlObject

topic freebase url



39
40
41
# File 'lib/basuco/topic.rb', line 39

def url
  @data["url"]
end

#view(type) ⇒ Object

returns individual view based on the requested type id



123
124
125
126
# File 'lib/basuco/topic.rb', line 123

def view(type)
  views.each { |v| return v if v.type.id =~ /^#{Regexp.escape(type)}$/}
  nil
end

#viewsObject

returns all available views based on the assigned types



117
118
119
# File 'lib/basuco/topic.rb', line 117

def views
  @views ||= Basuco::Collection.new(types.map { |type| Basuco::View.new(self, type) })
end

#webpagesObject

topic webpages currently returned as an array of hashes containing the keys “text” and “url” that hashes may be wrapped into a Webpage class later



80
81
82
# File 'lib/basuco/topic.rb', line 80

def webpages
  @data["webpage"]
end