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 # # Users # # def create_user(user) # perform_post("/users", :body => { :user => user }) # 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", :body => { :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 # Options: user_id def user_facets(id, query = {}) perform_get("/users/#{id.to_i}/facets.json", :query => query) end # # Badges # def badges perform_get("/badges.json") end def user_badges(id, query = {}) perform_get("/users/#{id.to_i}/badges.json", :query => query) end # # Mission Statuses # 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_complete!(id, mission_id) perform_put("/users/#{id.to_i}/mission_statuses/#{mission_id.to_i}/complete") 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) perform_get("/users/#{id.to_i}/posts.json") end def create_post(post) perform_post("/posts/create", :body => { :post => post }) end def post(id) perform_get("/posts/#{id.to_i}.json") end def publish_post(id, stream_id) perform_post("/posts/#{id.to_i}/publish",:body =>{ :post => { :stream_id => stream_id } }) en # # Roles # def user_roles(id) perform_get("/users/#{id.to_i}/roles.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 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