Sha256: 0dd95fbb6411da93637d839aead9574237275597392a91a3157b83a50df247a8

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'circleci'
require 'circle/cli/build'

module Circle
  module CLI
    class Project
      attr_reader :repo

      def initialize(repo)
        @repo = repo
        configure
      end

      def build!
        request CircleCi::Project, :build_branch, repo.branch_name
      end

      def builds
        @builds ||= request_builds(CircleCi::Project, :recent_builds_branch, repo.branch_name)
      end

      def recent_builds
        @recent_builds ||= request_builds(CircleCi::Project, :recent_builds)
      end

      def latest
        builds.first
      end

      def clear_cache!
        @builds = nil
        @recent_builds = nil
      end

      def request(klass, action, *args)
        response = klass.send(action, repo.user_name, repo.project, *args)

        if response.success?
          response.body
        else
          abort "The following error occurred: #{response.body['message']}"
        end
      end

      private

      def configure
        CircleCi.configure do |config|
          config.token = repo.circle_token
        end
      end

      def request_builds(*args)
        request(*args).to_a.map { |build| Build.new(self, build) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
circle-cli-0.1.3 lib/circle/cli/project.rb
circle-cli-0.1.2 lib/circle/cli/project.rb