Sha256: 33f3f00b7fa9eb8e53b96468bfb9ebf6f2424f5057e27bd1ad0c63ea0436f471

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'json'
require 'excon'

module RakeCircleCI
  class Client
    def initialize(opts)
      @base_url = opts[:base_url]
      @api_token = opts[:api_token]
      @project_slug = opts[:project_slug]
    end

    def create_env_var(name, value)
      body = JSON.dump(name: name, value: value)
      assert_successful(
          Excon.post(env_vars_url, body: body, headers: headers))
    end

    private

    def headers
      {
          "Circle-Token": @api_token,
          "Content-Type": "application/json",
          "Accept": "application/json"
      }
    end

    def assert_successful(response)
      unless response.status >= 200 && response.status < 300
        host = response.data[:host]
        path = response.data[:path]
        status = response.status
        reason = response.data[:reason_phrase]
        raise "Unsuccessful request: #{host}#{path} #{status} #{reason}"
      end
      response
    end

    def env_vars_url
      "#{@base_url}/v2/project/#{@project_slug}/envvar"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rake_circle_ci-0.2.0.pre.2 lib/rake_circle_ci/client.rb