module MambaNation class Base extend Forwardable def_delegators :client, :get, :post, :put, :delete attr_reader :client def initialize(client) @client = client end def create_user(query) perform_post("/users", :body => query) end def user_by_facebook_id(facebook_id, query = {}) perform_get("/users/find_by?facebook_id=#{facebook_id}", :query => query) end # Options: user_id, facebook_id def user(id, query = {}) perform_get("/users/#{id}.json", :query => query) end def user_set_websession(id, credentials) perform_post("/users/#{id}/update_websession", :body => { :user_credentials => credentials }) end # Options: facet_id def facet(id) perform_get("/facets/#{id}.json") end # Options: user_id def user_facets(id, query = {}) perform_get("/users/#{id}/facets.json", :query => query) end # Options: user_id, :limit def user_media(id, query = {}) perform_get("/users/#{id}/media.json", :query => query) end # Options: user_id, facebook_id def user_friends(id, query = {}) perform_get("/users/#{id}/friends.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 perform_get(path, options={}) MambaNation::Request.get(self, path, options) end def perform_post(path, options={}) MambaNation::Request.post(self, path, options) end def perform_put(path, options={}) MambaNation::Request.put(self, path, options) end def perform_delete(path, options={}) MambaNation::Request.delete(self, path, options) end end end