Class: Ken::Topic
- Inherits:
-
Object
- Object
- Ken::Topic
- Extended by:
- Extlib::Assertions
- Includes:
- Extlib::Assertions
- Defined in:
- lib/ken/topic.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.get(id) ⇒ Object
Retrieves a topic using the Topic API by Freebase returns a
TopicObject.
Instance Method Summary collapse
-
#aliases ⇒ Object
topic aliases.
-
#attribute(name) ⇒ Object
search for an attribute by name and return it.
-
#attributes ⇒ Object
returns all attributes for every type the topic is an instance of.
-
#attributes_loaded? ⇒ Boolean
returns true if attributes are already loaded.
-
#description ⇒ Object
topic description.
-
#id ⇒ Object
topic id.
-
#initialize(data) ⇒ Topic
constructor
initializes a topic using a json result.
- #inspect ⇒ Object
-
#name ⇒ Object
topic name.
-
#properties ⇒ Object
returns all the properties from all assigned types.
-
#schema_loaded? ⇒ Boolean
returns true if type information is already loaded.
-
#text ⇒ Object
topic text.
-
#thumbnail ⇒ Object
topic thumbnail.
- #to_s ⇒ Object
-
#type(type) ⇒ Object
returns individual type based on the requested type id.
-
#types ⇒ Object
returns all assigned types.
-
#url ⇒ Object
topic freebase url.
-
#view(type) ⇒ Object
returns individual view based on the requested type id.
-
#views ⇒ Object
returns all available views based on the assigned types.
-
#webpages ⇒ Object
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.
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
#data ⇒ Object (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">
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
#aliases ⇒ Object
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 |
#attributes ⇒ Object
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
146 147 148 |
# File 'lib/ken/topic.rb', line 146 def attributes_loaded? @attributes_loaded end |
#description ⇒ Object
topic description
53 54 55 |
# File 'lib/ken/topic.rb', line 53 def description @data["description"] end |
#id ⇒ Object
topic id
31 32 33 |
# File 'lib/ken/topic.rb', line 31 def id @data["id"] || "" end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/ken/topic.rb', line 74 def inspect result = "#<Topic id=\"#{id}\" name=\"#{name || "nil"}\">" end |
#name ⇒ Object
topic name
47 48 49 |
# File 'lib/ken/topic.rb', line 47 def name text end |
#properties ⇒ Object
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
140 141 142 |
# File 'lib/ken/topic.rb', line 140 def schema_loaded? @schema_loaded end |
#text ⇒ Object
topic text
59 60 61 |
# File 'lib/ken/topic.rb', line 59 def text @data["text"] end |
#thumbnail ⇒ Object
topic thumbnail
64 65 66 |
# File 'lib/ken/topic.rb', line 64 def thumbnail @data["thumbnail"] end |
#to_s ⇒ Object
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 |
#types ⇒ Object
returns all assigned types
88 89 90 91 |
# File 'lib/ken/topic.rb', line 88 def types load_schema! unless schema_loaded? @types end |
#url ⇒ Object
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 |
#views ⇒ Object
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 |
#webpages ⇒ Object
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 |