Ruby/Faraday - Add project file
So, you want to add a file to a project as described in File Storage and you're using Ruby and Faraday?
This is a two step process, first of all we're going to upload the file, then we're going to create a file object to link that upload with your project.
Upload the file
require 'faraday'
require 'json'
USERNAME = 'account/username'
APIKEY = 'myapikey'
PROJECT_PERMALINK = 'myproject'
connection = Faraday.new(:url => "https://api3.codebasehq.com/") do |builder|
builder.basic_auth USERNAME, APIKEY
builder.request :multipart
builder.response :logger
builder.adapter Faraday.default_adapter
end
payload = { 'files[]' => Faraday::UploadIO.new('path/to/file', 'file_mime_type') }
response = connection.post '/upload' do |req|
req.body = payload
req.headers['Accept'] = 'application/json'
end
rtn_data = JSON.load(response.body)
token = rtn_data['uploads'][0]['identifier']
Create the project file
payload = JSON.dump({ :name => "My screenshot", :description => 'shows the broken thing', :file_upload_token => token })
response = connection.post "/#{PROJECT_PERMALINK}/files" do
req.body = payload
req.headers['Accept'] = 'application/json'
req.headers['Content-Type'] = 'application/json'
end
You should now have a new file in the Files area of your project.