Class: ChunkyBaconfile::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chunky_baconfile/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ Object

Intialize the client

Parameters:

  • username (optional, String) (defaults to: nil)

    your Baconfile username

  • password (optional, String) (defaults to: nil)

    your Baconfile password



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

#usernameObject

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

Parameters:

  • parts (Hash)

    keys/values of items to convert for upload

Returns:

  • (Hash)

    encoded form data for POST



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

Parameters:

  • folder_name (String)

    the name of the folder to create

Returns:

  • (FileInfo)

    info about the 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

Parameters:

  • name (String)

    the folder or file to delete

Returns:

  • (Hashie::Mash)

    :success or :error



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

Parameters:

  • folder_name (String)

    the name of the folder

  • file_name (String)

    the name of the file

Returns:

  • (FileInfo)

    the items in the folder



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

Parameters:

  • folder_name (String)

    the name of the folder

Returns:

  • (Array<FileInfo>)

    the items in the 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

Parameters:

  • file (File)

Returns:

  • (String)

    mime type for the 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

#publicArray<FileInfo>

Fetch the latest public files

Returns:

  • (Array<FileInfo>)

    returns last 20 most recent 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

Parameters:

  • path (String, File)

    or file object to upload

Returns:

  • (FileInfo)

    info about the new folder



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)
  body_options = {:file => file}
  opts = build_multipart_bodies(body_options).merge({:basic_auth => @auth})
  ChunkyBaconfile::FileInfo.new(self.class.post("/#{@username}.json", opts))
end

#verifytrue, false

Verify credentials

Returns:

  • (true, false)

    whether or not the credentials are a match



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