class WerckerAPI::Client

Constants

API_ENDPOINT

Attributes

api_token[RW]
api_version[RW]

Public Class Methods

new(token = nil, api_version = 'v3') click to toggle source
# File lib/wercker_api/client.rb, line 17
def initialize(token = nil, api_version = 'v3')
  self.api_token = token || ENV['WERCKER_API_TOKEN']
  raise_token_nil_error if api_token.nil?
  self.api_version = api_version
end

Public Instance Methods

abort_run(run_id) click to toggle source

Abort a run

Abort an already running run instance.

Returns an object.

@param run_id [String] An id as returned by an API call @return WerckerAPI::Run object

# File lib/wercker_api/client.rb, line 222
def abort_run(run_id)
  request build_put_request(Run::ABORT[api_version, run_id]), Run
end
application(user_name, application) click to toggle source

Get an application

Get the details of a single application.

@param user_name [String] A wercker user name @param application [String] An wercker application name (probably a repository name). @return [WerckerAPI::Application] A DAO wrapper around the API response.

# File lib/wercker_api/client.rb, line 47
def application(user_name, application)
  request(
    build_get_request(
      Application::SHOW[api_version, user_name, application]
    ),
    Application
  )
end
application_builds(user_name, application) click to toggle source

List builds

Retrieve all builds of an application.

@param user_name [String] A wercker user name @param application [String] An wercker application name (probably a repository name). @return WerckerAPI::Application::BuildCollection An enumerable yielding WerckerAPI::Application::Build objects

# File lib/wercker_api/client.rb, line 80
def application_builds(user_name, application)
  request(
    build_get_request(
      Application::Build::INDEX[api_version, user_name, application]
    ),
    Application::BuildCollection
  )
end
application_deploys(user_name, application) click to toggle source

List deploys

Retrieve all deploys of an application.

@param user_name [String] A wercker user name @param application [String] An wercker application name (probably a repository name). @return WerckerAPI::Application::DeployCollection An Enumerable yielding WerckerAPI::Application::Deploy

# File lib/wercker_api/client.rb, line 96
def application_deploys(user_name, application)
  request(
    build_get_request(
      Application::Deploy::INDEX[api_version, user_name, application]
    ),
    Application::DeployCollection
  )
end
application_workflow(workflow_id) click to toggle source

Get a workflow

Get the details of a single workflow.

Returns a workflow object, which contains a collection of runs. @param workflow_id [String] A wercker workflow id as returned by an API call @return WerckerAPI::Application::Workflow object

# File lib/wercker_api/client.rb, line 128
def application_workflow(workflow_id)
  request(
    build_get_request(
      Application::Workflow::SHOW[api_version, workflow_id]
    ),
    Application::Workflow
  )
end
application_workflows(application_id) click to toggle source

Get all workflows

Get the last 10 workflows.

@param application_id [String] A worker application id as returned by an API call @return WerckerAPI::Application::WorkflowCollection An Enumerable yeilding WerckerAPI::Application::Workflow

# File lib/wercker_api/client.rb, line 112
def application_workflows(application_id)
  request(
    build_get_request(
      Application::Workflow::INDEX[api_version], applicationId: application_id
    ),
    Application::WorkflowCollection
  )
end
applications(user_name, params = {}) click to toggle source

List user applications

List all applications owned by the user or organization. The result will only contain applications that the authenticated user has access to. If the call is made without a token, only public applications will be returned.

@param user_name [String] A wercker user name @param params [Hash] Other params to pass as a query string to the API call @return [WerckerAPI::ApplicationCollection] An enumerable the yields `WerkerAPI::Application`

# File lib/wercker_api/client.rb, line 32
def applications(user_name, params = {})
  request(
    build_get_request(
      Application::INDEX[api_version, user_name], params
    ), ApplicationCollection
  )
end
run(run_id) click to toggle source

Get a run

Get the details of a single run.

Returns a run object.

@params run_id [String, Integer] An id as returned by an API call @return WerckerAPI::Run object

# File lib/wercker_api/client.rb, line 177
def run(run_id)
  request build_get_request(Run::SHOW[api_version, run_id]), Run
end
run_steps(run_id) click to toggle source

Get steps for run

Get the steps for a run.

Returns an array of step objects.

@param run_id [String] An id as returned by an API call @return WerckerAPI::Run::Steps object An Enumerable yielding a WerckerAPI::Run::Step

# File lib/wercker_api/client.rb, line 189
def run_steps(run_id)
  request build_get_request(Run::STEPS[api_version, run_id]), Run::StepCollection
end
runs(application_id: nil, pipeline_id: nil, params: {}) click to toggle source

Get all runs

Get the last 20 runs for a given pipeline or application.

Returns an array of run objects.

An application_id
or a pipeline_id

is required!

@param application_id [String] The ID of the application. @param pipeline_id [String] @param [Hash] params Other params passed as a query string to the API @option params [Integer] :limit Specify how many run objects should be returned. Max: 20, default: 20 @option params [Integer] :skip Skip the first X runs. Min: 1, default: 0

@option params [Integer] :sort Valid values: creationDateAsc
or creationDateDesc::. Default creationDateDesc
@option params [Integer] :status Filter by status. Valid values: notstarted, started::, finished::, running
@option params [Integer] :result Filter by result. Valid values: aborted::, unknown::, passed::, **failed

@option params [Integer] :branch ilter by branch @option params [Integer] :pipelineId Filter by pipeline @option params [Integer] :commit Filter by commit hash @option params [Integer] :sourceRun Filter by source run @option params [Integer] :author Filter by Wercker username @return WerckerAPI::RunCollection An enumerable yielding a WerckerAPI::Run object

# File lib/wercker_api/client.rb, line 160
def runs(application_id: nil, pipeline_id: nil, params: {})
  if application_id
    params[:applicationId] = application_id
  elsif pipeline_id
    params[:pipelineId] = pipeline_id
  end
  request build_get_request(Run::INDEX[api_version], params), RunCollection
end
trigger_run(pipeline_id, params = {}) click to toggle source

Trigger a new run

Trigger a new run for an application.

Returns a run object.

It is possible to add environment variables, which will be added to the run. The order of the array will be maintained, which makes it possible to use environment variables that were defined earlier. Any environment variables defined as part of the application or workflow will be overwritten, if using the same key.

@param pipeline_id [String] An id as returned by an API call @param [Hash] params Other params to pass as in body as query string to the API call

@option params [String] sourceRunId The id
of the run that should be used as input for this run, including artifacts. This is the same as chaining

a pipeline.

@option params [String] branch The Git branch

that the run should use. If not specified, the default branch will be used.

@option params [String] commitHash The Git commit hash that the run should used. *Requires branch*

to be set. If not specified, the latest commit is fetched

@option params [String] message The message to use for the run. If not specified, the Git commit message is used.

@option params [Array] envVars Environment variables which should be added to run. Contains objects with key
and value

properties.

@return WerckerAPI::Run object

# File lib/wercker_api/client.rb, line 209
def trigger_run(pipeline_id, params = {})
  params[:pipelineId] = pipeline_id
  request build_post_request(Run::TRIGGER[api_version], params), Run
end
update_application(user_name, application, branches) click to toggle source

Update an application

Update a single application. Currently it is only possible to change the ignored branches for the application. The updated application will be returned.

@param user_name [String] A wercker user name @param application [String] An wercker application name (probably a repository name). @params branches [Array] An array of branches you want to ignore @return [WerckerAPI::Application] A DAO wrapper around the API response.

# File lib/wercker_api/client.rb, line 64
def update_application(user_name, application, branches)
  request(
    build_patch_request(
      Application::SHOW[api_version, user_name, application], ignoredBranches: branches
    ),
    Application
  )
end

Private Instance Methods

authorise_request(request) click to toggle source
# File lib/wercker_api/client.rb, line 271
def authorise_request(request)
  request['Authorization'] = "Bearer #{api_token}"
  request
end
build_get_request(uri, params = {}) click to toggle source
# File lib/wercker_api/client.rb, line 245
def build_get_request(uri, params = {})
  uri = URI::HTTP.build(path: uri, query: URI.encode_www_form(params))
  authorise_request(Net::HTTP::Get.new(uri))
end
build_http_client() click to toggle source
# File lib/wercker_api/client.rb, line 276
def build_http_client
  http_client = Net::HTTP.new(API_ENDPOINT.host, API_ENDPOINT.port)
  http_client.use_ssl = true
  http_client
end
build_patch_request(uri, params) click to toggle source
# File lib/wercker_api/client.rb, line 255
def build_patch_request(uri, params)
  request = Net::HTTP::Patch.new(URI::HTTP.build(path: uri))
  build_request_with_body(request, params)
end
build_post_request(uri, params) click to toggle source
# File lib/wercker_api/client.rb, line 260
def build_post_request(uri, params)
  request = Net::HTTP::Post.new(URI::HTTP.build(path: uri))
  build_request_with_body(request, params)
end
build_put_request(uri, params = {}) click to toggle source
# File lib/wercker_api/client.rb, line 250
def build_put_request(uri, params = {})
  request = Net::HTTP::Put.new(URI::HTTP.build(path: uri))
  build_request_with_body(request, params)
end
build_request_with_body(request, params) click to toggle source
# File lib/wercker_api/client.rb, line 265
def build_request_with_body(request, params)
  request.body = JSON.dump(params)
  request['Content-Type'] = 'application/json'
  authorise_request(request)
end
handle(response, klass) click to toggle source
# File lib/wercker_api/client.rb, line 286
def handle(response, klass)
  case response
  when Net::HTTPSuccess
    klass.new(JSON.parse(response.body))
  else
    handle_error(response)
  end
end
handle_error(response) click to toggle source
# File lib/wercker_api/client.rb, line 295
def handle_error(response)
  raise WerckerAPI::Error, response
end
http_client() click to toggle source
# File lib/wercker_api/client.rb, line 230
def http_client
  @http_client ||= build_http_client
end
raise_token_nil_error() click to toggle source
# File lib/wercker_api/client.rb, line 234
    def raise_token_nil_error
      msg = <<-EOM
A token is required to communicate with the API, please refer to the read me.

   client = WerckerAPI::Client.new('2039e0239840239u0239uf0293v2093urbv0293urbv')

More inforation at: http://devcenter.wercker.com/docs/api/getting-started/authentication
EOM
      raise ArgumentError, msg
    end
request(request, serializer) click to toggle source
# File lib/wercker_api/client.rb, line 282
def request(request, serializer)
  handle(http_client.request(request), serializer)
end