Class: Ken::Resource

Inherits:
Object
  • Object
show all
Extended by:
Extlib::Assertions
Includes:
Extlib::Assertions
Defined in:
lib/ken/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Resource

initializes a resource using a json result



8
9
10
11
12
# File 'lib/ken/resource.rb', line 8

def initialize(data)
  assert_kind_of 'data', data, Hash
  @schema_loaded, @attributes_loaded, @data = false, false, data
  @data_fechted = data["/type/reflect/any_master"] != nil
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data



5
6
7
# File 'lib/ken/resource.rb', line 5

def data
  @data
end

Class Method Details

.get(id) ⇒ Object

Executes an Mql Query against the Freebase API and returns the result wrapped in a Resource Object.

Examples

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

Raises:



21
22
23
24
25
26
# File 'lib/ken/resource.rb', line 21

def self.get(id)
  assert_kind_of 'id', id, String
  result = Ken.session.mqlread(FETCH_DATA_QUERY.merge!(:id => id))
  raise ResourceNotFound unless result
  Ken::Resource.new(result)
end

Instance Method Details

#attribute(name) ⇒ Object

search for an attribute by name and return it



102
103
104
105
# File 'lib/ken/resource.rb', line 102

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

#attributesObject

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



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

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

#attributes_loaded?Boolean

returns true if attributes are already loaded



115
116
117
# File 'lib/ken/resource.rb', line 115

def attributes_loaded?
  @attributes_loaded
end

#data_fetched?Boolean

returns true if json data is already loaded



120
121
122
# File 'lib/ken/resource.rb', line 120

def data_fetched?
  @data_fetched
end

#guidObject

resource guid



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

def guid
  @data['guid'] || ""
end

#idObject

resource id



30
31
32
# File 'lib/ken/resource.rb', line 30

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

#inspectObject



52
53
54
# File 'lib/ken/resource.rb', line 52

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

#nameObject

resource name



42
43
44
# File 'lib/ken/resource.rb', line 42

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

#propertiesObject

returns all the properties from all assigned types



85
86
87
88
89
90
91
# File 'lib/ken/resource.rb', line 85

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



109
110
111
# File 'lib/ken/resource.rb', line 109

def schema_loaded?
  @schema_loaded
end

#to_sObject



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

def to_s
  name || id || ""
end

#type(type) ⇒ Object

returns individual type based on the requested type id



78
79
80
81
# File 'lib/ken/resource.rb', line 78

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

#typesObject

returns all assigned types



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

def types
  load_schema! unless schema_loaded?
  @types
end

#view(type) ⇒ Object

returns individual view based on the requested type id



71
72
73
74
# File 'lib/ken/resource.rb', line 71

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



65
66
67
# File 'lib/ken/resource.rb', line 65

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