Class: MiniFB::GraphObject

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

Overview

Wraps a graph object for easily accessing its connections

Instance Method Summary collapse

Constructor Details

#initialize(session_or_token, id) ⇒ GraphObject

Creates a GraphObject using an OAuthSession or access_token



462
463
464
465
466
467
468
469
470
471
# File 'lib/mini_fb.rb', line 462

def initialize(session_or_token, id)
    @oauth_session = if session_or_token.is_a?(MiniFB::OAuthSession)
        session_or_token
                     else
                         MiniFB::OAuthSession.new(session_or_token)
                     end
    @id = id
    @object = @oauth_session.get(id, :metadata => true)
    @connections_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/mini_fb.rb', line 501

def method_missing(method, *args, &block)
    key = method.to_s
    if @object.keys.include?(key)
        @object[key]
    elsif @connections_cache.has_key?(key)
        @connections_cache[key]
    elsif connections.include?(key)
        @connections_cache[key] = @oauth_session.get(@id, :type => key)
    else
        super
    end
end

Instance Method Details

#[](key) ⇒ Object



497
498
499
# File 'lib/mini_fb.rb', line 497

def [](key)
    @object[key]
end

#connectionsObject



477
478
479
# File 'lib/mini_fb.rb', line 477

def connections
    @object..connections.keys
end

#inspectObject



473
474
475
# File 'lib/mini_fb.rb', line 473

def inspect
    "<##{self.class.name} #{@object.inspect}>"
end

#keysObject



493
494
495
# File 'lib/mini_fb.rb', line 493

def keys
    @object.keys
end

#methodsObject



485
486
487
# File 'lib/mini_fb.rb', line 485

def methods
    super + @object.keys.include?(key) + connections.include?(key)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


489
490
491
# File 'lib/mini_fb.rb', line 489

def respond_to?(method)
    @object.keys.include?(key) || connections.include?(key) || super
end