module Joey::RestAPI
Public Class Methods
included(base)
click to toggle source
# File lib/joey/rest_api.rb, line 10 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
constantize_string(klass)
click to toggle source
# File lib/joey/rest_api.rb, line 94 def constantize_string(klass) # FIXME: cost_get is buggy on some versions of Ruby # klass.is_a?(String) ? Joey.const_get(klass) : klass klass.is_a?(String) ? (klass =~ /Joey/ ? klass.constantize : ("Joey::"+ klass).constantize) : klass end
create_instance(klass, data)
click to toggle source
# File lib/joey/rest_api.rb, line 86 def create_instance(klass, data) klass = determine_class(klass, data) if klass.nil? raise UnrecognizeableClassError.new("unable to recognize klass for #{klass.inspect} => #{data.inspect}") end klass.new(data, self) end
determine_class(klass_or_klasses, data)
click to toggle source
# File lib/joey/rest_api.rb, line 100 def determine_class(klass_or_klasses, data) klasses = Array(klass_or_klasses).map { |k| constantize_string(k)} klasses.detect {|klass| klass.recognize?(data)} || klasses.first end
extract_fetching_array(hash, klass)
click to toggle source
# File lib/joey/rest_api.rb, line 61 def extract_fetching_array(hash, klass) f = Joey::FetchingArray.new f.concat(hash["data"]) f.client = self f.classes = Array(klass) if hash["paging"] f.next_url = hash["paging"]["next"] f.previous_url = hash["paging"]["previous"] end if hash["count"] f.count = hash['count'] end f end
extract_hash_or_array(hash_or_array, klass)
click to toggle source
# File lib/joey/rest_api.rb, line 54 def extract_hash_or_array(hash_or_array, klass) return nil unless hash_or_array return hash_or_array if hash_or_array.kind_of?(Array) return extract_fetching_array(hash_or_array, klass) if hash_or_array.has_key?("data") return hash_or_array end
get_all_and_map(ids, klass = nil, args = {})
click to toggle source
# File lib/joey/rest_api.rb, line 33 def get_all_and_map(ids, klass = nil, args = {}) data = self.get_objects(ids, args) map_data({ 'data' => data.values }, klass) end
get_and_map(path, klass = nil, args = {})
click to toggle source
path can be some node id in the Facebook Graph e.g. ‘me’, ‘me/feed’, ‘1234567890/feed’. klass is wrapper class for that node.
# File lib/joey/rest_api.rb, line 28 def get_and_map(path, klass = nil, args = {}) data = self.get_object(path, args) map_data(data, klass) end
get_and_map_url(url, klass = nil)
click to toggle source
# File lib/joey/rest_api.rb, line 38 def get_and_map_url(url, klass = nil) # FIXME: following only returns a hash like {"id"=>"http://graph.facebook.com/100000637452380/feed"} # try to write a method in koala which can request absolute Facebook urls. See fetching_array.rb:7. data = self.class.get_object(url) map_data(data,klass) end
map_data(data, klass = nil)
click to toggle source
# File lib/joey/rest_api.rb, line 45 def map_data(data, klass = nil) raise_error_if_necessary(data) hash_or_array = extract_hash_or_array(data, klass) hash_or_array = map_to_class(hash_or_array, klass) if klass # TODO: Validate an object here. #hash_or_array.validate and puts hash_or_array.class.inspect if hash_or_array.is_a?(Model) hash_or_array end
map_to_class(hash_or_array, klass)
click to toggle source
# File lib/joey/rest_api.rb, line 76 def map_to_class(hash_or_array, klass) return nil if hash_or_array.nil? if hash_or_array.kind_of?(Array) hash_or_array.map! {|elmnt| create_instance(klass, elmnt)} else hash_or_array = create_instance(klass, hash_or_array) end hash_or_array end
me()
click to toggle source
# File lib/joey/rest_api.rb, line 17 def me get_and_map('me', Joey::User) end
raise_error_if_necessary(data)
click to toggle source
# File lib/joey/rest_api.rb, line 105 def raise_error_if_necessary(data) if data.kind_of?(Hash) if data.keys.size == 1 and data["error"] type = data["error"]["type"] message = data["error"]["message"] raise Exception.new("#{type}: #{message}") end end end
revoke_app_permission(ext_perm)
click to toggle source
# File lib/joey/rest_api.rb, line 21 def revoke_app_permission(ext_perm) # no need to boolianize. It returns true/false. self.rest_call("auth.revokeExtendedPermission", :perm => ext_perm.to_s) end