Class: ChunkyBaconfile::Client
- Inherits:
-
Object
- Object
- ChunkyBaconfile::Client
- Includes:
- HTTParty
- Defined in:
- lib/chunky_baconfile/client.rb
Instance Attribute Summary collapse
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#build_multipart_bodies(parts) ⇒ Hash
Build multi-part POST form data from file.
-
#create_folder(folder_name) ⇒ FileInfo
Create a new folder.
-
#delete(name) ⇒ Hashie::Mash
Delete a folder or file.
-
#file(folder_name, file_name) ⇒ FileInfo
Fetch info about an item.
-
#folder(folder_name) ⇒ Array<FileInfo>
Fetch the files in a folder.
-
#initialize(username = nil, password = nil) ⇒ Object
constructor
Intialize the client.
-
#mime_type(file) ⇒ String
Return the MIME type for a file.
-
#public ⇒ Array<FileInfo>
Fetch the latest public files.
-
#upload_file(file) ⇒ FileInfo
Upload a new file.
-
#verify ⇒ true, false
Verify credentials.
Constructor Details
#initialize(username = nil, password = nil) ⇒ Object
Intialize the client
13 14 15 16 |
# File 'lib/chunky_baconfile/client.rb', line 13 def initialize(username=nil, password=nil) @username = username @auth = {:username => username, :password => password} end |
Instance Attribute Details
#username ⇒ Object
Returns the value of attribute username.
3 4 5 |
# File 'lib/chunky_baconfile/client.rb', line 3 def username @username end |
Instance Method Details
#build_multipart_bodies(parts) ⇒ Hash
Build multi-part POST form data from file
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/chunky_baconfile/client.rb', line 97 def 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}#{"\r\n"}" if value.respond_to?(:read) body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{"\r\n"}" body << "Content-Type: #{mime_type(value.path)}#{"\r\n"*2}" body << value.read else body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{"\r\n"*2}#{value}" end body << "\r\n" end body << "--#{boundary}--#{"\r\n"*2}" { :body => body, :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"} } end |
#create_folder(folder_name) ⇒ FileInfo
Create a new folder
48 49 50 |
# File 'lib/chunky_baconfile/client.rb', line 48 def create_folder(folder_name) ChunkyBaconfile::FileInfo.new(self.class.post("/#{@username}.json", :body => {:name => folder_name}, :basic_auth => @auth)) end |
#delete(name) ⇒ Hashie::Mash
Delete a folder or file
67 68 69 |
# File 'lib/chunky_baconfile/client.rb', line 67 def delete(name) response = Hashie::Mash.new(self.class.delete("/#{username}/#{name}.json", :basic_auth => @auth)) end |
#file(folder_name, file_name) ⇒ FileInfo
Fetch info about an item
32 33 34 |
# File 'lib/chunky_baconfile/client.rb', line 32 def file(folder_name, file_name) ChunkyBaconfile::FileInfo.new(self.class.get("/#{@username}/#{folder_name}/#{file_name}.json")) end |
#folder(folder_name) ⇒ Array<FileInfo>
Fetch the files in a folder
22 23 24 25 |
# File 'lib/chunky_baconfile/client.rb', line 22 def folder(folder_name) response = self.class.get("/#{@username}/#{folder_name}.json") response['items'].map {|item| ChunkyBaconfile::FileInfo.new(item)} end |
#mime_type(file) ⇒ String
Return the MIME type for a file
83 84 85 86 87 88 89 90 |
# File 'lib/chunky_baconfile/client.rb', line 83 def 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 |
#public ⇒ Array<FileInfo>
Fetch the latest public files
39 40 41 42 |
# File 'lib/chunky_baconfile/client.rb', line 39 def public response = self.class.get("/public.json") response['items'].map {|item| ChunkyBaconfile::FileInfo.new(item)} end |
#upload_file(file) ⇒ FileInfo
Upload a new file
56 57 58 59 60 61 |
# File 'lib/chunky_baconfile/client.rb', line 56 def upload_file(file) file = File.new(file) if file.is_a?(String) = {:file => file} opts = build_multipart_bodies().merge({:basic_auth => @auth}) ChunkyBaconfile::FileInfo.new(self.class.post("/#{@username}.json", opts)) end |
#verify ⇒ true, false
Verify credentials
74 75 76 77 |
# File 'lib/chunky_baconfile/client.rb', line 74 def verify response = Hashie::Mash.new(self.class.get("/verify.json", :basic_auth => @auth)) response.key?(:user) end |