Module: Joey::RestAPI
Defined Under Namespace
Modules: ClassMethods
Classes: UnrecognizeableClassError
Class Method Summary
collapse
Instance Method Summary
collapse
-
#constantize_string(klass) ⇒ Object
-
#create_instance(klass, data) ⇒ Object
-
#determine_class(klass_or_klasses, data) ⇒ Object
-
#extract_fetching_array(hash, klass) ⇒ Object
-
#extract_hash_or_array(hash_or_array, klass) ⇒ Object
-
#get_all_and_map(ids, klass = nil, args = {}) ⇒ Object
-
#get_and_map(path, klass = nil, args = {}) ⇒ Object
path can be some node id in the Facebook Graph e.g.
-
#get_and_map_url(url, klass = nil) ⇒ Object
-
#map_data(data, klass = nil) ⇒ Object
-
#map_to_class(hash_or_array, klass) ⇒ Object
-
#me ⇒ Object
-
#raise_error_if_necessary(data) ⇒ Object
-
#revoke_app_permission(ext_perm) ⇒ Object
#boolianize
Class Method Details
.included(base) ⇒ Object
10
11
12
|
# File 'lib/joey/rest_api.rb', line 10
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#constantize_string(klass) ⇒ Object
94
95
96
97
98
|
# File 'lib/joey/rest_api.rb', line 94
def constantize_string(klass)
klass.is_a?(String) ? (klass =~ /Joey/ ? klass.constantize : ("Joey::"+ klass).constantize) : klass
end
|
#create_instance(klass, data) ⇒ Object
86
87
88
89
90
91
92
|
# 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) ⇒ Object
100
101
102
103
|
# 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
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/joey/rest_api.rb', line 61
def (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
|
54
55
56
57
58
59
|
# File 'lib/joey/rest_api.rb', line 54
def (hash_or_array, klass)
return nil unless hash_or_array
return hash_or_array if hash_or_array.kind_of?(Array)
return (hash_or_array, klass) if hash_or_array.has_key?("data")
return hash_or_array
end
|
#get_all_and_map(ids, klass = nil, args = {}) ⇒ Object
33
34
35
36
|
# 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 = {}) ⇒ Object
path can be some node id in the Facebook Graph e.g. ‘me’, ‘me/feed’, ‘1234567890/feed’. klass is wrapper class for that node.
28
29
30
31
|
# 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) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/joey/rest_api.rb', line 38
def get_and_map_url(url, klass = nil)
data = self.class.get_object(url)
map_data(data,klass)
end
|
#map_data(data, klass = nil) ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/joey/rest_api.rb', line 45
def map_data(data, klass = nil)
raise_error_if_necessary(data)
hash_or_array = (data, klass)
hash_or_array = map_to_class(hash_or_array, klass) if klass
hash_or_array
end
|
#map_to_class(hash_or_array, klass) ⇒ Object
76
77
78
79
80
81
82
83
84
|
# 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 ⇒ Object
17
18
19
|
# File 'lib/joey/rest_api.rb', line 17
def me
get_and_map('me', Joey::User)
end
|
#raise_error_if_necessary(data) ⇒ Object
105
106
107
108
109
110
111
112
113
|
# 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) ⇒ Object
21
22
23
24
|
# File 'lib/joey/rest_api.rb', line 21
def revoke_app_permission(ext_perm)
self.rest_call("auth.revokeExtendedPermission", :perm => ext_perm.to_s)
end
|