Class: Ken::Topic

Inherits:
Object
  • Object
show all
Extended by:
Extlib::Assertions
Includes:
Extlib::Assertions
Defined in:
lib/ken/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



9
10
11
12
13
# File 'lib/ken/topic.rb', line 9

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.



6
7
8
# File 'lib/ken/topic.rb', line 6

def data
  @data
end

Class Method Details

.get(id) ⇒ Object

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

Examples

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

Raises:



22
23
24
25
26
27
# File 'lib/ken/topic.rb', line 22

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

Instance Method Details

#aliasesObject

topic aliases



36
37
38
# File 'lib/ken/topic.rb', line 36

def aliases
  @data["alias"]
end

#attribute(name) ⇒ Object

search for an attribute by name and return it



133
134
135
136
# File 'lib/ken/topic.rb', line 133

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



112
113
114
115
# File 'lib/ken/topic.rb', line 112

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

#attributes_loaded?Boolean

returns true if attributes are already loaded

Returns:

  • (Boolean)


146
147
148
# File 'lib/ken/topic.rb', line 146

def attributes_loaded?
  @attributes_loaded
end

#descriptionObject

topic description



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

def description
  @data["description"]
end

#idObject

topic id



31
32
33
# File 'lib/ken/topic.rb', line 31

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

#inspectObject



74
75
76
# File 'lib/ken/topic.rb', line 74

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

#nameObject

topic name



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

def name
  text
end

#propertiesObject

returns all the properties from all assigned types



102
103
104
105
106
107
108
# File 'lib/ken/topic.rb', line 102

def properties
  @properties = Ken::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)


140
141
142
# File 'lib/ken/topic.rb', line 140

def schema_loaded?
  @schema_loaded
end

#textObject

topic text



59
60
61
# File 'lib/ken/topic.rb', line 59

def text
  @data["text"]
end

#thumbnailObject

topic thumbnail



64
65
66
# File 'lib/ken/topic.rb', line 64

def thumbnail
  @data["thumbnail"]
end

#to_sObject



69
70
71
# File 'lib/ken/topic.rb', line 69

def to_s
  name || id || ""
end

#type(type) ⇒ Object

returns individual type based on the requested type id



95
96
97
98
# File 'lib/ken/topic.rb', line 95

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

#typesObject

returns all assigned types



88
89
90
91
# File 'lib/ken/topic.rb', line 88

def types
  load_schema! unless schema_loaded?
  @types
end

#urlObject

topic freebase url



41
42
43
# File 'lib/ken/topic.rb', line 41

def url
  @data["url"]
end

#view(type) ⇒ Object

returns individual view based on the requested type id



125
126
127
128
# File 'lib/ken/topic.rb', line 125

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



119
120
121
# File 'lib/ken/topic.rb', line 119

def views
  @views ||= Ken::Collection.new(types.map { |type| Ken::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



82
83
84
# File 'lib/ken/topic.rb', line 82

def webpages
  @data["webpage"]
end