lib/namba/api_methods.rb in namba-1.3.1 vs lib/namba/api_methods.rb in namba-1.3.2

- old
+ new

@@ -5,18 +5,17 @@ module APIMethods def get_user_info name = self.username get_response_from "http://api.namba.#{self.locale}/getUserInfo.php?username=" + name end - def get_user_video name = self.username - get_response_from "http://api.namba.#{self.locale}/getVideo.php?username=" + name + %w(photo video).map do |key| + define_method "get_user_#{key}" do |*args| + name = args.first || self.username + response = get_response_from "http://api.namba.#{self.locale}/get#{key.capitalize}.php?username=" + name + end end - def get_user_photo name = self.username - get_response_from "http://api.namba.#{self.locale}/getPhoto.php?username=" + name - end - def get_friends_list name = self.username get_response_from "http://api.namba.#{self.locale}/friends.php?username=" + name end def set_status text @@ -37,16 +36,23 @@ private def get_response_from url response = Net::HTTP.get_response(URI.parse(url)) - raise InvalidResponseError, "Invalid response from service" unless response.code == "200" - MultiJson.decode(response.body) + raise_or_return response end def post_data_to url, params = { :username => self.username, :password => self.password } response = Net::HTTP.post_form(URI.parse(url), params) + raise_or_return response + end + + def raise_or_return response raise InvalidResponseError, "Invalid response from service" unless response.code == "200" - MultiJson.decode(response.body) + if response.body == "null" + return [] + else + MultiJson.decode(response.body) + end end end end