Class: MetaGraph::Resource

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

Overview

Resource module provide a function that read a data from Graph API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, path) ⇒ Resource

Read a data from Graph API and create Resource class instance.

About arguments, Please see at read method’s description. initialize method arguments are same to read method.



22
23
24
25
26
27
# File 'lib/meta_graph/resource.rb', line 22

def initialize(access_token, path)
  @access_token = access_token
  @api_raw_result = read(@access_token, path)
  @api_result = JSON.parse(@api_raw_result, :symbolize_names => true)
  @metadata = @api_result.delete(:metadata)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



6
7
8
# File 'lib/meta_graph/resource.rb', line 6

def access_token
  @access_token
end

#api_resultObject (readonly)

Returns the value of attribute api_result.



6
7
8
# File 'lib/meta_graph/resource.rb', line 6

def api_result
  @api_result
end

Class Method Details

.http_clientObject

Get the HTTP-Client instance to read a data from Graph API.



11
12
13
14
# File 'lib/meta_graph/resource.rb', line 11

def self.http_client
  @http_client ||= HTTPClient.new(:agent_name => 'MetaGraph')
  @http_client
end

Instance Method Details

#connectionsObject

Get an array of connections that has a key named connection name.



80
81
82
83
84
85
86
87
88
# File 'lib/meta_graph/resource.rb', line 80

def connections
  connections = {}
  if .key?(:connections)
    [:connections].each do |connection, url|
      connections[connection.to_sym] = url
    end
  end
  connections
end

#dataObject

Get a collection data



73
74
75
# File 'lib/meta_graph/resource.rb', line 73

def data
  return @api_result[:data] if @api_result.key?(:data)
end

#fieldsObject

Get a hash that has a key named field name.



62
63
64
65
66
67
68
# File 'lib/meta_graph/resource.rb', line 62

def fields
  fields = {}
  @api_result.each do |key, value|
    fields[key.to_sym] = value
  end
  fields
end

#metadataObject

Get the metadata in Graph API result set. Return empty hash if result set don’t contain the metadata.



55
56
57
# File 'lib/meta_graph/resource.rb', line 55

def 
  @metadata || {}
end

#read(access_token, path) ⇒ Object

read an object specified id from Graph API.

Arguments

path

Graph API path you want. For example, when you specified ‘me’ to this arg, this module read your User object from Graph API. Otherwise, when full api url is specified, you will get data from that url.

access_token

access token to read a data from Graph API. Please set to oauth’s access_token.



41
42
43
44
45
46
47
48
49
# File 'lib/meta_graph/resource.rb', line 41

def read(access_token, path)
  path = GRAPH_API_END_POINT + path.to_s unless path.index(GRAPH_API_END_POINT)
  params = {
    :access_token => access_token,
    :metadata => 1
  }

  Resource.http_client.get(path, params).body
end