module MambaNation class Base extend Forwardable def_delegators :client, :get, :post, :put, :delete attr_reader :client, :fbs_cookies def initialize(client, fbs_cookies = nil) @client = client @fbs_cookies = fbs_cookies end # # Famous # def famous(query = {}) perform_get("/users/famous.json", :query => query) end def current_user perform_get("/users/me") unless fbs_cookies.nil? end def user_by_facebook_id(facebook_id, query = {}) perform_get("/users/find_by?facebook_id=#{facebook_id.to_i}", :query => query) end def user_by_email(email, query = {}) perform_get("/users/find_by?facebook_id=#{email}", :query => query) end # Options: user_id, facebook_id def user(id, query = {}) perform_get("/users/#{id.to_i}.json", :query => query) end alias_method :user_by_id, :user def user_set_websession(id, credentials) perform_post("/users/#{id.to_i}/update_websession", :query => { :user_credentials => credentials }) end # # User actions # def user_last_login(id) perform_get("/users/#{id.to_i}/last_login.json") end def user_chats(id, query = {}) perform_get("/users/#{id.to_i}/chats.json", :query => query) end def user_coms(id, query = {}) perform_get("/users/#{id.to_i}/coms.json", :query => query) end def user_actions(id, query = {}) perform_get("/users/#{id.to_i}/actions.json", :query => query) end def activate_user(id) perform_put("/users/#{id.to_i}/activate") end def anonymate_user(id) perform_put("/users/#{id.to_i}/anonymate") end def suspend_user(id) perform_put("/users/#{id.to_i}/suspend") end # Options: user_id, :limit def user_media(id, query = {}) perform_get("/users/#{id.to_i}/media.json", :query => query) end # Options: user_id, facebook_id def user_friends(id, query = {}) perform_get("/users/#{id.to_i}/friends.json", :query => query) end # # Facets # # Options: facet_id def facet(id) perform_get("/facets/#{id.to_i}.json") end def famous_facets(query = {}) perform_get("/facets/famous.json", :query => query) end # Options: user_id def user_facets(id, query = {}) perform_get("/users/#{id.to_i}/facets.json", :query => query) end def facet_coms(id, query = {}) perform_get("/facets/#{id.to_i}/coms.json", :query => query) end def facet_messages(id, query = {}) perform_get("/facets/#{id.to_i}/messages.json", :query => query) end # # Badges # def badges(query = {}) perform_get("/badges.json", :query => query) end def user_badges(id, query = {}) perform_get("/users/#{id.to_i}/badges.json", :query => query) end # # Mission Statuses # def mission_status user_id, mission_id, query = {} perform_get("/users/#{user_id.to_i}/mission_status/#{mission_id}.json", :query => query) end def mission_statuses(id, query = {}) perform_get("/users/#{id.to_i}/mission_statuses.json", :query => query) end def missions_family_statuses(id, query = {}) perform_get("/users/#{id.to_i}/missions_family_statuses.json", :query => query) end def mission_start!(id, mission_id) perform_put("/users/#{id.to_i}/mission_statuses/#{mission_id.to_i}/start") end # # Posts # def user_posts(id,app_id) perform_get("/users/#{id.to_i}/posts.json", :query => { :fb_application_id => app_id }) end def create_post(id, app_id, post) perform_post("/posts", :body => { :post => post , :user_id => id , :fb_sig_app_id => app_id}) end def posts(id, query = {}) perform_get("/posts/#{id}.json", :query => query) end def publish_post(id, stream_id) perform_post("/posts/#{id}/publish.json", :body => { :post => { :stream_id => stream_id } }) end # # Roles # def user_roles(id) perform_get("/users/#{id.to_i}/roles.json") end # # Users System Messages # def user_system_messages(id) perform_get("/users/#{id.to_i}/system_messages.json") end def user_unread_system_messages(id) perform_get("/users/#{id.to_i}/unread_system_messages.json") end def read_user_message(id, message_id) perform_post("/users/#{id.to_i}/messages/#{message_id.to_i}/read.json") end # # User Announcements # def announcements(id) perform_get("/users/#{id.to_i}/announcements.json") end def read_user_announcement(id, announcement_id) perform_put("/users/#{id.to_i}/announcements/#{announcement_id.to_i}/read.json") end protected def self.mime_type(file) case when file =~ /\.jpg/ then 'image/jpg' when file =~ /\.gif$/ then 'image/gif' when file =~ /\.png$/ then 'image/png' else 'application/octet-stream' end end def mime_type(f) self.class.mime_type(f) end CRLF = "\r\n" def self.build_multipart_bodies(parts) boundary = Time.now.to_i.to_s(16) body = "" parts.each do |key, value| esc_key = CGI.escape(key.to_s) body << "--#{boundary}#{CRLF}" if value.respond_to?(:read) body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}" body << "Content-Type: #{mime_type(value.path)}#{CRLF*2}" body << value.read else body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}" end body << CRLF end body << "--#{boundary}--#{CRLF*2}" { :body => body, :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"} } end def build_multipart_bodies(parts) self.class.build_multipart_bodies(parts) end private def request_options(opts={}) opts[:headers] ||= {} opts[:headers].merge! "FB-Cookies" => fbs_cookies if fbs_cookies opts end def perform_get(path, options={}) MambaNation::Request.get(self, path, request_options(options)) end def perform_post(path, options={}) MambaNation::Request.post(self, path, request_options(options)) end def perform_put(path, options={}) MambaNation::Request.put(self, path, request_options(options)) end def perform_delete(path, options={}) MambaNation::Request.delete(self, path, request_options(options)) end end end