lib/git-semaphore/api.rb in git-semaphore-0.0.3 vs lib/git-semaphore/api.rb in git-semaphore-0.0.4

- old
+ new

@@ -9,47 +9,51 @@ SEMAPHORE_API_HOST = 'semaphoreapp.com' SEMAPHORE_API_URI = '/api/v1' def self.projects_uri auth_token - URI::HTTPS.build( - :host => SEMAPHORE_API_HOST, - :path => File.join(SEMAPHORE_API_URI, 'projects'), - :query => "auth_token=#{auth_token}" - ) + request_uri(auth_token, :path => File.join(SEMAPHORE_API_URI, 'projects')) end def self.branches_uri project_hash_id, auth_token - URI::HTTPS.build( - :host => SEMAPHORE_API_HOST, - :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, 'branches'), - :query => "auth_token=#{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 - URI::HTTPS.build( - :host => SEMAPHORE_API_HOST, - :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, branch_id, 'status'), - :query => "auth_token=#{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 - URI::HTTPS.build( - :host => SEMAPHORE_API_HOST, - :path => File.join(SEMAPHORE_API_URI, 'projects', project_hash_id, branch_id), - :query => "auth_token=#{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 + 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| - net_http.get(uri.request_uri) + 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