class PyBossa::API

A Ruby wrapper for the PyBossa API. @see docs.pybossa.com/en/latest/model.html#restful-api

Constants

VERSION

Attributes

api_key[RW]

Public Class Methods

create(klass, opts = {}) click to toggle source

Creates an object.

@param [String] klass one of “app”, “task” or “taskrun” @param [Hash] opts optional arguments @option opts [String] :api_key an API key @return [Hash] an object @raise [PyBossa::API::Error] if operation failed

@see github.com/PyBossa/pybossa/blob/master/pybossa/api.py#L91

# File lib/pybossa-api.rb, line 51
def create(klass, opts = {})
  request :post, "/#{klass}", opts
end
destroy(klass, id, opts = {}) click to toggle source

Deletes an object.

@param [String] klass one of “app”, “task” or “taskrun” @param [Integer] id an object ID @param [Hash] opts optional arguments @option opts [String] :api_key an API key @raise [PyBossa::API::Error] if operation failed

@see github.com/PyBossa/pybossa/blob/master/pybossa/api.py#L108

# File lib/pybossa-api.rb, line 77
def destroy(klass, id, opts = {})
  request :delete, "/#{klass}/#{id}", opts
end
many(klass, opts = {}) click to toggle source

Lists objects. To filter objects, add key-value pairs to the :opts argument in which each key is the name of a field on which you want to filter. For example, you may want to filter tasks by app_id.

@param [String] klass one of “app”, “task” or “taskrun” @param [Hash] opts optional arguments @option opts [Integer] :limit number of results to return [default 20] @option opts [String] :api_key an API key @return [Array] a list of objects

# File lib/pybossa-api.rb, line 27
def many(klass, opts = {})
  request :get, "/#{klass}", opts
end
retrieve(klass, id, opts = {}) click to toggle source

Gets an object by ID.

@param [String] klass one of “app”, “task” or “taskrun” @param [Integer] id an object ID @param [Hash] opts optional arguments @option opts [String] :api_key an API key @return [Hash] an object

# File lib/pybossa-api.rb, line 38
def retrieve(klass, id, opts = {})
  request :get, "/#{klass}/#{id}", opts
end
update(klass, id, opts = {}) click to toggle source

Updates an object.

@param [String] klass one of “app”, “task” or “taskrun” @param [Integer] id an object ID @param [Hash] opts optional arguments @option opts [String] :api_key an API key @raise [PyBossa::API::Error] if operation failed

@see github.com/PyBossa/pybossa/blob/master/pybossa/api.py#L129

# File lib/pybossa-api.rb, line 64
def update(klass, id, opts = {})
  request :put, "/#{klass}/#{id}", opts
end

Private Class Methods

request(http_method, path, opts) click to toggle source

@todo new_task user_progress github.com/PyBossa/pybossa/blob/master/pybossa/api.py#L198 github.com/PyBossa/pybossa/blob/master/pybossa/api.py#L238

# File lib/pybossa-api.rb, line 87
def request(http_method, path, opts)
  opts[:api_key] ||= api_key if api_key
  response = if [:get, :delete].include? http_method
    send http_method, path, :query => opts
  else
    send http_method, path, :query => {:api_key => opts.delete(:api_key)}, :body => JSON.dump(opts), :headers => {'Content-type' => 'application/json'}
  end
  unless [200, 204].include? response.response.code.to_i
    raise PyBossa::API::Error.new Sanitize.clean(response.response.body)
  end
  response.parsed_response
end