Sha256: 7898b61ad4fa4509bfb5af2aea3e56de897c68cfefb6f994c584156036a0771d

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'uri'
require 'net/http'

module Git
  module Semaphore
    class Api

      # http://docs.semaphoreapp.com/api

      SEMAPHORE_API_HOST = 'semaphoreapp.com'
      SEMAPHORE_API_URI = '/api/v1'

      def self.projects_uri auth_token
        request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects'))
      end

      def self.branches_uri project_hash_id, auth_token
        request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, 'branches'))
      end

      def self.status_uri project_hash_id, branch_id, auth_token
        request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, branch_id, 'status'))
      end

      def self.history_uri project_hash_id, branch_id, auth_token
        request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, branch_id))
      end

      def self.last_revision_uri project_hash_id, branch_id, auth_token
        request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, branch_id, 'build'))
      end

      # helper functions

      def self.get_response uri, action=:get
        ::Net::HTTP.start(uri.host, uri.port, :use_ssl => (uri.scheme == 'https'), :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |net_http|
          case action
          when :get
            net_http.get uri.request_uri
          when :post
            net_http.post uri.request_uri, uri.query
          else
            raise 'Unsupported action'
          end
        end
      end

      def self.request_uri auth_token, options={}
        URI::HTTPS.build(
          { :host  => SEMAPHORE_API_HOST,
            :query => "auth_token=#{auth_token}"
          }.merge(options)
        )
      end
      private_class_method(:request_uri)

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git-semaphore-0.0.4 lib/git-semaphore/api.rb