From bc25da9d86efeafc19b22393360a7ece660c47f4 Mon Sep 17 00:00:00 2001 From: Atsushi Nakatsugawa Date: Fri, 23 Sep 2016 18:24:04 +0900 Subject: [PATCH] Support file store download --- examples/file_test.rb | 10 ++++++++++ lib/ncmb/client.rb | 9 +++++++-- lib/ncmb/data_store.rb | 12 ++++++++++-- lib/ncmb/file.rb | 5 +++++ lib/ncmb/version.rb | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/examples/file_test.rb b/examples/file_test.rb index afa9842..7ef4177 100644 --- a/examples/file_test.rb +++ b/examples/file_test.rb @@ -18,3 +18,13 @@ puts "Updated" f.delete() puts "Deleted" + +f = NCMB::NFile.new('http://mb.cloud.nifty.com/assets/images/logo.png') +f.acl.public('read', true) +f.acl.public('write', true) +f.fileName = "test.png" +f.save() +file = NCMB::NFile.new("test.png") +fp = open("test.png", "w") +fp.write(file.get) +fp.close diff --git a/lib/ncmb/client.rb b/lib/ncmb/client.rb index abf884f..c7b061a 100644 --- a/lib/ncmb/client.rb +++ b/lib/ncmb/client.rb @@ -157,7 +157,12 @@ def request(method, path, queries = {}) "#{key}=#{value}" end.join("&") path = path + (query == '' ? "" : "?"+query) - json = JSON.parse(http.get(path, headers).body, symbolize_names: true) + rp = Regexp.new "/#{NCMB::API_VERSION}/files/.*" + if path =~ rp + json = http.get(path, headers).body + else + json = JSON.parse(http.get(path, headers).body, symbolize_names: true) + end when :post req = Net::HTTP::Post.new(path) if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO) @@ -195,7 +200,7 @@ def request(method, path, queries = {}) @@last_error = e raise NCMB::APIError.new(e.to_s) end - if json[:error] != nil + if json.is_a?(Hash) && json[:error] != nil raise NCMB::APIError.new(json[:error]) end json diff --git a/lib/ncmb/data_store.rb b/lib/ncmb/data_store.rb index 06a0f32..a1fc208 100644 --- a/lib/ncmb/data_store.rb +++ b/lib/ncmb/data_store.rb @@ -74,11 +74,19 @@ def [](count) def path return @path if @path - path = "/#{@@client.api_version}/classes/#{@name}" + if ["file", "user", "push", "installation"].include? @name + if @name == "push" + "/#{@@client.api_version}/#{@name}" + else + "/#{@@client.api_version}/#{@name}s" + end + else + "/#{@@client.api_version}/classes/#{@name}" + end end def get - return @items unless @items.nil? + # return @items unless @items.nil? results = @@client.get path, @queries return [] unless results if results[:error] && results[:error] != "" diff --git a/lib/ncmb/file.rb b/lib/ncmb/file.rb index b2e8260..e0c8d94 100644 --- a/lib/ncmb/file.rb +++ b/lib/ncmb/file.rb @@ -8,6 +8,7 @@ def initialize(file_path = nil) @fields[:fileName] = File.basename(file_path) @fields['mime-type'.to_sym] = MIME::Types.type_for(file_path)[0] end + @content = nil end def save @@ -16,6 +17,10 @@ def save end alias :update :save + def get + @content = @@client.get path + end + def path "#{base_path}/#{@fields[:fileName]}" end diff --git a/lib/ncmb/version.rb b/lib/ncmb/version.rb index ff5bae7..e956411 100644 --- a/lib/ncmb/version.rb +++ b/lib/ncmb/version.rb @@ -1,3 +1,3 @@ module Ncmb - VERSION = "0.1.2" + VERSION = "0.1.3" end