lib/adapters/parse_adapter.rb in dm-parse-0.2.0 vs lib/adapters/parse_adapter.rb in dm-parse-0.2.1

- old
+ new

@@ -9,18 +9,19 @@ VERSION = "1" APP_ID_HEADER = "X-Parse-Application-Id" API_KEY_HEADER = "X-Parse-REST-API-Key" MASTER_KEY_HEADER = "X-Parse-Master-Key" - attr_reader :classes, :users, :login, :password_reset + attr_reader :classes, :users, :login, :password_reset, :file_storage def initialize(name, options) super @classes = build_parse_resource_for "classes" @users = build_parse_resource_for "users" @login = build_parse_resource_for "login" @password_reset = build_parse_resource_for "requestPasswordReset" + @file_storage = build_parse_resource_for "files" end def parse_resources_for(model) storage_name = model.storage_name storage_name == "_User" ? users : classes[storage_name] @@ -91,10 +92,30 @@ # a empty Hash def request_password_reset(email) password_reset.post params: {email: email} end + # Upload a file + # Parse-only + # + # @param [String] filename + # the filename + # + # @param [String] content + # the content + # + # @param [String] content_type + # the content type + # + # @return [Hash] + # the uploaded file information + def upload_file(filename, content, content_type = MIME::Types.type_for(filename).first) + headers = file_storage.options[:headers] + headers = headers.merge("Content-Type" => content_type) if content_type + file_storage[URI.escape(filename)].post body: content, headers: headers + end + def delete(resources) resources.each do |resource| parse_resource_for(resource).delete end.size end @@ -106,12 +127,18 @@ end.size end private def build_parse_resource_for(name) + Parse::Resource.new(HOST, format: :json, headers: key_headers)[VERSION][name] + end + + def key_headers key_type = @options[:master] ? MASTER_KEY_HEADER : API_KEY_HEADER - headers = {APP_ID_HEADER => @options[:app_id], key_type => @options[:api_key]} - Parse::Resource.new(HOST, format: :json, headers: headers)[VERSION][name] + { + APP_ID_HEADER => @options[:app_id], + key_type => @options[:api_key] + } end def parse_params_for(query) result = { :limit => parse_limit_for(query) } if conditions = parse_conditions_for(query)