Class: Freebase::Api::FreebaseResult

Inherits:
Object
  • Object
show all
Defined in:
lib/freebase/api.rb

Overview

Encapsulates a Freebase result, enables method-based access to the returned values. E.g. result = mqlread(:type => “/music/artist”, :name => “The Police”, :id => nil) result.id => “/topic/en/the_police”

Direct Known Subclasses

Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ FreebaseResult

Returns a new instance of FreebaseResult.



24
25
26
# File 'lib/freebase/api.rb', line 24

def initialize(result)
  @result = result.symbolize_keys!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

provides method based access to the result properties

Raises:

  • (NoMethodError)


91
92
93
94
95
96
97
98
99
100
# File 'lib/freebase/api.rb', line 91

def method_missing(name, *args)
  raise NoMethodError.new(name.to_s) unless args.length == 0
  if @result.has_key?(name)
    resultify @result[name]
  elsif @result.has_key?((singularized_name = name.to_s.singularize.to_sym)) and @result[singularized_name].is_a?(Array)
    resultify @result[singularized_name]
  else
    raise NoMethodError.new(name.to_s)
  end
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



22
23
24
# File 'lib/freebase/api.rb', line 22

def result
  @result
end

Instance Method Details

#depluralize(v) ⇒ Object

returns the first element of an array if it is one this for handling generic mql queries like [{}] that return only a single value



39
40
41
# File 'lib/freebase/api.rb', line 39

def depluralize(v)
  Array(v).first
end

#fb_typeObject

result.type is reserved in ruby. Call result.fb_type to access :type instead.



33
34
35
# File 'lib/freebase/api.rb', line 33

def fb_type
  @result[:type]
end

#idObject



28
29
30
# File 'lib/freebase/api.rb', line 28

def id
  @result[:id]
end

#resultify(v) ⇒ Object

converts a returned value from freebase into the corresponding ruby object This is done first by the core data type and then by the type attribute for an object The casing is done using a method dispatch pattern which should make it easy to mix-in new behaviors and type support



47
48
49
50
51
# File 'lib/freebase/api.rb', line 47

def resultify(v)
  resultify_method = "resultify_#{v.class.to_s.downcase}".to_sym
  v = send(resultify_method, v) if respond_to? resultify_method
  return v
end

#resultify_array(v) ⇒ Object

resultifies each value in the array



54
55
56
# File 'lib/freebase/api.rb', line 54

def resultify_array(v)
  v.map{|vv| resultify(vv)}
end

#resultify_hash(v) ⇒ Object

resultifies an object hash



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/freebase/api.rb', line 59

def resultify_hash(v)
  vtype = indifferent_access(v,:type)
  if value_type? vtype
    resultify_value(vtype,v)
  elsif vtype.blank?
    Logger.debug "What's This: #{v.inspect}"
    FreebaseResult.new(v)
  elsif vtype.is_a? Array
    "Freebase::Types#{vtype.first.classify}".constantize.new(v) #TODO: Union these types
  else
    "Freebase::Types#{vtype.classify}".constantize.new(v)
  end
end

#resultify_value(vtype, v) ⇒ Object

dispatches to a value method for the type or returns the simple value if it doesn’t exist for example /type/text would dispatch to resultify_value_type_text



81
82
83
84
85
86
87
88
# File 'lib/freebase/api.rb', line 81

def resultify_value(vtype,v)
  resultify_method = "resultify_value#{vtype.gsub(/\//,'_')}".to_sym
  if respond_to? resultify_method
    send(resultify_method, v) 
  else
    indifferent_access(v,:value)
  end
end

#value_type?(t) ⇒ Boolean

decides if a type is just an expanded simple value object

Returns:

  • (Boolean)


74
75
76
# File 'lib/freebase/api.rb', line 74

def value_type?(t)
  ['/type/text', '/type/datetime'].include?(t)
end