login_radius 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,152 +0,0 @@
1
- #Include this module in UserProfile, and UserProfile will magically have all the methods
2
- #I dynamically generate below!
3
- module LoginRadius
4
- module UserProfileGetters
5
- # Below is metaprogramming. This is what Ruby is magic for.
6
- # Since most API calls are similar, I define an interface for them.
7
- # You add a hash with these keys below, and it makes a method for them on loadup.
8
- # It creates both a ! version of the method, which throws errors if you don't have access
9
- # To an API endpoint, and a safe one without a ! symbol(login! vs login) that just returns false.
10
- #
11
- # @param method [Symbol] Method's name
12
- # @param route [String] Route, ex. is "/users/:token/:secret" (:something is interpolated to be self.something)
13
- # @param params [Hash] Hash of params you wish to send to the route. If you use symbols for values, are interpolated.
14
- # @param key_success_check [Symbol] Key to check for in the response to see if it was successful. Ex, :id for login, if not set, whole response hash is returned instead of true/false
15
- # @return [Boolean] Whether or not it was successful.
16
- [
17
- {
18
- :method => :access_token,
19
- :route => "api/v2/access_token",
20
- :params => {:token=>:token, :secret=>:secret},
21
- :key_success_check => :access_token
22
- },
23
- {
24
- :method => :userprofile,
25
- :route => "api/v2/userprofile",
26
- :params => {:access_token => :access_token},
27
- :key_success_check => :id
28
- },
29
- {
30
- :method => :event,
31
- :route => "api/v2/event",
32
- :params => {:access_token => :access_token}
33
- },
34
- {
35
- :method => :contact,
36
- :route => "api/v2/contact",
37
- :params => {:access_token => :access_token}
38
- },
39
- {
40
- :method => :mention,
41
- :route => "api/v2/mention",
42
- :params => {:access_token => :access_token}
43
- },
44
- {
45
- :method => :company,
46
- :route => "api/v2/company",
47
- :params => {:access_token => :access_token}
48
- },
49
- {
50
- :method => :group,
51
- :route => "api/v2/group",
52
- :params => {:access_token => :access_token}
53
- },
54
- {
55
- :method => :post,
56
- :route => "api/v2/post",
57
- :params => {:access_token => :access_token}
58
- },
59
- {
60
- :method => :status,
61
- :route => "api/v2/status",
62
- :params => {:access_token => :access_token}
63
- },
64
- {
65
- :method => :following,
66
- :route => "api/v2/following",
67
- :params => {:access_token => :access_token}
68
- },
69
- {
70
- :method => :album,
71
- :route => "api/v2/album",
72
- :params => {:access_token => :access_token}
73
- },
74
- {
75
- :method => :checkin,
76
- :route => "api/v2/checkin",
77
- :params => {:access_token => :access_token}
78
- },
79
- {
80
- :method => :like,
81
- :route => "api/v2/like",
82
- :params => {:access_token => :access_token}
83
- },
84
- {
85
- :method => :photo,
86
- :route => "api/v2/photo",
87
- :params => {:access_token => :access_token, :id => :id}
88
- },
89
- {
90
- :method => :audio,
91
- :route => "api/v2/audio",
92
- :params => {:access_token => :access_token}
93
- },
94
- {
95
- :method => :video,
96
- :route => "api/v2/video",
97
- :params => {:access_token => :access_token}
98
- },
99
- {
100
- :method => :page,
101
- :route => "api/v2/page",
102
- :params => {:access_token => :access_token, :pagename => :PageNameOrId}
103
- }
104
- ].each do |method_info|
105
- define_method(method_info[:method].to_s + "!") do
106
-
107
- #when params have symbols as values, means we actually want fields on the object,
108
- #so we dynamically generate real params.
109
- real_params = method_info[:params].inject(Hash.new) do |hash, entry|
110
- hash[entry.first] = self.send(entry.last)
111
- hash
112
- end
113
-
114
- #allows interpolation of routes - so /blah/:token becomes /blah/2323-233d3e etc.
115
- real_route = method_info[:route].gsub(/\/:(\w+)/) do |match|
116
- key = match.split(":").last
117
- "/"+self.send(key).to_s
118
- end
119
- response = call_api(real_route, real_params)
120
-
121
- if response.is_a?(Hash)
122
- #Special feature: If we get a hash back instead of an array,
123
- #we create methods on the user profile object for each key.
124
- #If we're just getting an array back, there is no need for this,
125
- #The method itself that it's called from is all that is needed to access
126
- #the data.
127
-
128
- #define methods for each key in the hash, so they are accessible:
129
- #(I dont like using method missing returns because then respond_to? doesn't work)
130
- response.each do |key, value|
131
- define_singleton_method(key) do
132
- return value
133
- end
134
- end
135
- end
136
-
137
- #raise an error if there is one, otherwise, return.
138
- raise LoginRadius::Exception.new(response[:errormessage]) if (response.is_a?(Hash) and response[:errorcode])
139
- return response
140
- end
141
-
142
- #safe version of the method that doesn't throw an exception
143
- define_method(method_info[:method]) do
144
- begin
145
- self.send(method_info[:method].to_s+"!")
146
- rescue LoginRadius::Exception
147
- return false
148
- end
149
- end
150
- end
151
- end
152
- end