Skip to content

Commit

Permalink
Support file store download
Browse files Browse the repository at this point in the history
  • Loading branch information
goofmint committed Sep 23, 2016
1 parent 8fce9ed commit bc25da9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
10 changes: 10 additions & 0 deletions examples/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions lib/ncmb/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/ncmb/data_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] != ""
Expand Down
5 changes: 5 additions & 0 deletions lib/ncmb/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,10 @@ def save
end
alias :update :save

def get
@content = @@client.get path
end

def path
"#{base_path}/#{@fields[:fileName]}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ncmb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ncmb
VERSION = "0.1.2"
VERSION = "0.1.3"
end

0 comments on commit bc25da9

Please sign in to comment.