module MambaNation class Base extend Forwardable def_delegators :client, :get, :post, :put, :delete attr_reader :client, :fbs_cookies, :signed_request def initialize(client, options = {}) @client = client @fbs_cookies = options.delete(:fbs_cookies) if options.has_key? :fbs_cookies @signed_request = options.delete(:signed_request) if options.has_key? :signed_request end # Class method to fetch current user based on cookie def self.current_user(client, options = {}) MambaNation::Base.new(client, options).current_user end # # Famous # def famous(query = {}) perform_get("/users/famous.json", :query => query) end def current_user(query = {}) perform_get("/users/me", :query => query) 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 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, query = {}) perform_post("/users/#{id.to_i}/update_websession", :body => { :user => { :user_credentials => credentials } }.merge(query)) end # # User actions # def user_last_login(id, query = {}) perform_get("/users/#{id.to_i}/last_login.json", :query => query) 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 => {}.merge(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(facet_id, query = {}) perform_get("/facets/#{facet_id.to_i}.json", :query => query) end def famous_facets(query = {}) perform_get("/facets/famous.json", :query => query) end def favorite_facet(id, facet_id, query = {}) perform_put("/facets/#{facet_id.to_i}/favorite.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(facet_id, query = {}) perform_get("/facets/#{facet_id.to_i}/coms.json", :query => query) end def facet_messages(facet_id, query = {}) perform_get("/facets/#{facet_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(id, mission_id, query = {}) perform_get("/users/#{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, query = {}) perform_put("/users/#{id.to_i}/mission_statuses/#{mission_id.to_i}/start", :query => {}.merge(query)) end # # Posts # def posts(id, query = {}) perform_get("/posts/#{id}.json", :query => query) end # options: # application_id => Integer, Array def user_posts(id, query = {}) perform_get("/users/#{id.to_i}/posts.json", :query => query) end def create_post(id, post, application_id, query = {}) perform_post("/posts", :body => { :user_id => id, :post => post, :application_id => application_id }.merge(query)) end def publish_post(id, stream_id, query = {}) perform_post("/posts/#{id}/publish.json", :body => { :post => { :stream_id => stream_id } }.merge(query)) end # # Roles # def user_roles(id, query = {}) perform_get("/users/#{id.to_i}/roles.json", :query => query) end # # Users System Messages # def user_system_messages(id, query = {}) perform_get("/users/#{id.to_i}/system_messages.json", :query => query) end def user_unread_system_messages(id, query = {}) perform_get("/users/#{id.to_i}/unread_system_messages.json", :query => query) end def read_user_message(id, message_id, query = {}) perform_post("/users/#{id.to_i}/messages/#{message_id.to_i}/read.json", :body => {}.merge(query)) end # # User Announcements # def announcements(id, query = {}) perform_get("/users/#{id.to_i}/announcements.json", :query => query) end def read_user_announcement(id, announcement_id, query = {}) perform_put("/users/#{id.to_i}/announcements/#{announcement_id.to_i}/read.json", :query => query) 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!("X-FB-COOKIES" => @fbs_cookies) if @fbs_cookies opts[:query] ||= {} opts[:query].merge!("signed_request" => @signed_request) if @signed_request 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