class FbGraph2::Node

Attributes

access_token[RW]
id[RW]
identifier[RW]

Public Class Methods

new(id, attributes = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 7
def initialize(id, attributes = {})
  self.id = id
  assign attributes
  authenticate attributes[:access_token] if attributes.include? :access_token
end

Public Instance Methods

authenticate(access_token) click to toggle source
# File lib/fb_graph2/node.rb, line 13
def authenticate(access_token)
  self.access_token = access_token
  self
end
destroy(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 44
def destroy(params = {}, options = {})
  delete params, options
end
edge(edge, params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 23
def edge(edge, params = {}, options = {})
  Edge.new(
    self,
    edge,
    params,
    options.merge(
      collection: edge_for(edge, params, options)
    )
  )
end
edges() click to toggle source
# File lib/fb_graph2/node.rb, line 34
def edges
  @edges ||= self.class.included_modules.select do |_module_|
    _module_.name =~ /FbGraph2::Edge/
  end.collect(&:instance_methods).flatten.sort
end
fetch(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 18
def fetch(params = {}, options = {})
  attributes = get params, options
  self.class.new(attributes[:id], attributes).authenticate access_token
end
update(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 40
def update(params = {}, options = {})
  post params, options
end

Protected Instance Methods

delete(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 66
def delete(params = {}, options = {})
  handle_response do
    http_client.delete build_endpoint(options), build_params(params)
  end
end
get(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 54
def get(params = {}, options = {})
  handle_response do
    http_client.get build_endpoint(options), build_params(params)
  end
end
http_client() click to toggle source
# File lib/fb_graph2/node.rb, line 50
def http_client
  FbGraph2.http_client(access_token)
end
post(params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 60
def post(params = {}, options = {})
  handle_response do
    http_client.post build_endpoint(options), build_params(params)
  end
end

Private Instance Methods

build_endpoint(options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 79
def build_endpoint(options = {})
  File.join [
    File.join(
      FbGraph2.root_url,
      options[:api_version] || FbGraph2.api_version,
      id.to_s
    ),
    options[:edge],
    Util.as_identifier(options[:edge_scope])
  ].compact.collect(&:to_s)
end
build_params(params = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 91
def build_params(params = {})
  params = Hash(params).merge(debug: :all) if FbGraph2.debugging?
  if params.present?
    if params.include? :fields
      params[:fields] = Array(params[:fields]).join(',')
    end
    params
  else
    nil
  end
end
edge_for(edge, params = {}, options = {}) click to toggle source
# File lib/fb_graph2/node.rb, line 74
def edge_for(edge, params = {}, options = {})
  collection = get params, options.merge(edge: edge)
  Collection.new collection
end
handle_response() { || ... } click to toggle source
# File lib/fb_graph2/node.rb, line 103
def handle_response
  response = yield
  _response_ = MultiJson.load response.body
  _response_ = _response_.with_indifferent_access if _response_.respond_to? :with_indifferent_access
  case response.status
  when 200...300
    if _response_.respond_to?(:has_key?) && _response_.has_key?(:success)
      _response_[:success]
    else
      _response_
    end
  else
    raise Exception.detect(response.status, _response_, response.headers)
  end
rescue MultiJson::DecodeError
  raise Exception.new(response.status, "Unparsable Response: #{response.body}")
end