class Box::Client

Constants

VERSION

Attributes

session[RW]

Public Class Methods

new(session) click to toggle source
# File lib/box/client.rb, line 9
def initialize(session)
  @session = session
end

Public Instance Methods

connection() click to toggle source
# File lib/box/client.rb, line 60
def connection
  conn = Faraday.new(Box::API_URL) do |builder|
    builder.request :json
    builder.request :multipart
    # builder.response :logger
    builder.response :json, :content_type => /\bjson$/
    # What a joke. This must be LAST to avoid encoding errors in JSON request body
    builder.adapter  :net_http
  end

  conn.headers['Authorization'] = "Bearer #{@session.access_token}"
  conn
end
delete(path, params = {}, retries = 0) click to toggle source
# File lib/box/client.rb, line 90
def delete(path, params = {}, retries = 0)
  request('DELETE', path, params)
end
folder(path) click to toggle source

Starting at the “root” of Box which is always “All Files”, make successive requests untl you have reached the final path in the input

# File lib/box/client.rb, line 31
def folder(path) # /path/to/folder
  path = Pathname(path).each_filename.to_a
  folder = root
  path.each do |name|
    folder = folder.folders.select {|folder| folder.name == name}.first
    return nil unless folder
  end
  folder
end
get(path, params = {}, retries = 0) click to toggle source
# File lib/box/client.rb, line 78
def get(path, params = {}, retries = 0)
  request('GET', path, params)
end
handle_response(response) click to toggle source
# File lib/box/client.rb, line 153
def handle_response(response)
  case response.status
    when 400
      raise Box::MalformedAuthHeaders, response.headers
    when 404
      raise Box::ResourceNotFound, JSON.dump(response.body)
    when 409
      raise Box::NameConflict,  JSON.dump(response.body)
    when 500
      ap response.body
  end
  return response
end
make_uri(path) click to toggle source
# File lib/box/client.rb, line 74
def make_uri(path)

end
parse_items(results) click to toggle source

Process the Box “items” returned from a request

# File lib/box/client.rb, line 49
def parse_items(results)
  return [] if results['entries'].empty?
  results['entries'].reduce([]) do |entries, entry|
    entries << case entry['type']
      when 'file'   then Box::File.new(self, entry)
      when 'folder' then Box::Folder.new(self, entry)
    end
    entries
  end
end
post(path, params = {}, retries = 0) click to toggle source
# File lib/box/client.rb, line 82
def post(path, params = {}, retries = 0)
  request('POST', path, params)
end
put(path, params = {}, retries = 0) click to toggle source
# File lib/box/client.rb, line 86
def put(path, params = {}, retries = 0)
  request('PUT', path, params)
end
request(method, path, params = {}, retries = 0) click to toggle source

Generic HTTP request method with retries for bad authorization

# File lib/box/client.rb, line 126
def request(method, path, params = {}, retries = 0)
  uri = Addressable::URI.parse(::File.join(VERSION, path))
  if method == 'GET'
    uri.query_values = params.reduce({}){|hash, (k,v)| hash[k] = v.to_s; hash}
    # params = {}
  end

  Box.log "#{method} #{::File.join(Box::API_URL, uri.to_s)}"
  response = connection.send(method.downcase, uri.to_s, params)

  case response.status
    when 401
      try_token_refresh!
      return request(method, path, params, retries + 1) if retries == 0
    else
      return handle_response(response)
  end
# TODO: We need to retry connection failures - or at least catch them
# rescue Faraday::ConnectionFailed => e
end
root() click to toggle source
# File lib/box/client.rb, line 13
def root
  Folder.new(self, id: 0)
end
try_token_refresh!() click to toggle source
# File lib/box/client.rb, line 147
def try_token_refresh!
  @session.refresh_token!
rescue OAuth2::Error => e
  raise "Sorry, could not refresh tokens: #{e.message}"
end
upload(params = {}, retries = 0) click to toggle source
# File lib/box/client.rb, line 94
def upload(params = {}, retries = 0)
  # Basic parameters
  local_path, content_type, file_name, parent_id = params[:local_path], params[:content_type], params[:file_name], params[:parent_id]
  # If there is a file_id, it means we want to replace it.
  uri = if params[:box_id]
    "https://upload.box.com/api/2.0/files/#{params[:box_id]}/content"
  else
    'https://upload.box.com/api/2.0/files/content'
  end

  Box.log "POST #{uri}"

  # Construct the payload
  payload = {
    filename:  Faraday::UploadIO.new(local_path, content_type, file_name),
    parent_id: parent_id
  }

  response = connection.post(uri, payload) do |request|
    request.headers['Content-Type'] = 'multipart/form-data'
  end

  case response.status
    when 401
      try_token_refresh!
      return upload(params, retries + 1) if retries == 0
    else
      return handle_response(response)
  end
end
walk(root) { |item| ... } click to toggle source
# File lib/box/client.rb, line 17
def walk(root, &block)
  root.items.each do |item|
    if item.folder?
      walk(item, &block)
    elsif item.file?
      yield item
    else
      Box.log "Unknown item type #{item.id}:#{item.type}"
    end
  end
end