Module: Joey::RestAPI
Defined Under Namespace
Modules: ClassMethods
Classes: UnrecognizeableClassError
Class Method Summary
collapse
Instance Method Summary
collapse
#boolianize
Class Method Details
.included(base) ⇒ Object
6
7
8
|
# File 'lib/joey/rest_api.rb', line 6
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#constantize_string(klass) ⇒ Object
82
83
84
85
86
|
# File 'lib/joey/rest_api.rb', line 82
def constantize_string(klass)
klass.is_a?(String) ? (klass =~ /Joey/ ? klass.constantize : ("Joey::"+ klass).constantize) : klass
end
|
#create_instance(klass, data) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/joey/rest_api.rb', line 74
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
88
89
90
91
|
# File 'lib/joey/rest_api.rb', line 88
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
|
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/joey/rest_api.rb', line 52
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
f
end
|
45
46
47
48
49
50
|
# File 'lib/joey/rest_api.rb', line 45
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_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.
24
25
26
27
|
# File 'lib/joey/rest_api.rb', line 24
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
29
30
31
32
33
34
|
# File 'lib/joey/rest_api.rb', line 29
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
36
37
38
39
40
41
42
43
|
# File 'lib/joey/rest_api.rb', line 36
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
64
65
66
67
68
69
70
71
72
|
# File 'lib/joey/rest_api.rb', line 64
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
13
14
15
|
# File 'lib/joey/rest_api.rb', line 13
def me
get_and_map('me', Joey::User)
end
|
#raise_error_if_necessary(data) ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'lib/joey/rest_api.rb', line 93
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
17
18
19
20
|
# File 'lib/joey/rest_api.rb', line 17
def revoke_app_permission(ext_perm)
self.rest_call("auth.revokeExtendedPermission", :perm => ext_perm.to_s)
end
|