Class: MetaGraph::Resource
- Inherits:
-
Object
- Object
- MetaGraph::Resource
- Defined in:
- lib/meta_graph/resource.rb
Overview
Resource module provide a function that read a data from Graph API.
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_result ⇒ Object
readonly
Returns the value of attribute api_result.
Class Method Summary collapse
-
.http_client ⇒ Object
Get the HTTP-Client instance to read a data from Graph API.
Instance Method Summary collapse
-
#connections ⇒ Object
Get an array of connections that has a key named connection name.
-
#data ⇒ Object
Get a collection data.
-
#fields ⇒ Object
Get a hash that has a key named field name.
-
#initialize(access_token, path) ⇒ Resource
constructor
Read a data from Graph API and create Resource class instance.
-
#metadata ⇒ Object
Get the metadata in Graph API result set.
-
#read(access_token, path) ⇒ Object
read an object specified id from Graph API.
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_token ⇒ Object (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_result ⇒ Object (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_client ⇒ Object
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
#connections ⇒ Object
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 |
#data ⇒ Object
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 |
#fields ⇒ Object
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 |
#metadata ⇒ Object
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 |