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

- (Resource) initialize(data)

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

- (Object) data (readonly)

Returns the value of attribute data



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

def data
  @data
end

Class Method Details

+ (Object) get(id)

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

- (Object) attribute(name)

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

- (Object) attributes

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

- (Boolean) attributes_loaded?

returns true if attributes are already loaded

Returns:

  • (Boolean)


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

def attributes_loaded?
  @attributes_loaded
end

- (Boolean) data_fetched?

returns true if json data is already loaded

Returns:

  • (Boolean)


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

def data_fetched?
  @data_fetched
end

- (Object) guid

resource guid



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

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

- (Object) id

resource id



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

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

- (Object) inspect



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

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

- (Object) name

resource name



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

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

- (Object) properties

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

- (Boolean) schema_loaded?

returns true if type information is already loaded

Returns:

  • (Boolean)


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

def schema_loaded?
  @schema_loaded
end

- (Object) to_s



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

def to_s
  name || id || ""
end

- (Object) type(type)

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

- (Object) types

returns all assigned types



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

def types
  load_schema! unless schema_loaded?
  @types
end

- (Object) view(type)

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

- (Object) views

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