lib/trav3.rb in trav3-0.1.1 vs lib/trav3.rb in trav3-0.2.0
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
require 'trav3/version'
+require 'trav3/pagination'
require 'trav3/options'
require 'trav3/headers'
require 'trav3/result'
require 'trav3/post'
require 'trav3/get'
@@ -11,16 +12,19 @@
API_ROOT = 'https://api.travis-ci.org'
# An abstraction for the Travis CI v3 API
#
# @author Daniel P. Clark https://6ftdan.com
+ # @!attribute [r] api_endpoint
+ # @return [String] API endpoint
# @!attribute [r] options
# @return [Options] Request options object
# @!attribute [r] headers
# @return [Headers] Request headers object
class Travis
API_ENDPOINT = API_ROOT
+ attr_reader :api_endpoint
attr_reader :options
attr_reader :headers
# @param repo [String] github_username/repository_name
# @raise [InvalidRepository] if given input does not
@@ -28,15 +32,31 @@
# @return [Travis]
def initialize(repo)
raise InvalidRepository unless repo.is_a?(String) and
Regexp.new(/(^\d+$)|(^\w+(?:\/|%2F){1}\w+$)/) === repo
+ @api_endpoint = API_ENDPOINT
@repo = repo.gsub(/\//, '%2F')
defaults(limit: 25)
- h("Travis-API-Version": 3)
+ h('Content-Type': 'application/json')
+ h('Accept': 'application/json')
+ h('Travis-API-Version': 3)
end
+ # @overload api_endpoint=(endpoint)
+ # Set as the API endpoint
+ # @param endpoint [String] name for value to set
+ # @return [self]
+ def api_endpoint= endpoint
+ if /^https:\/\/api\.travis-ci\.(?:org|com)$/ === endpoint
+ @api_endpoint = endpoint
+ else
+ raise InvalidAPIEndpoint
+ end
+ self
+ end
+
# @overload defaults(key: value, ...)
# Set as many options as you'd like for collections queried via an API request
# @param key [Symbol, String] name for value to set
# @param value [Symbol, String, Integer] value for key
# @return [self]
@@ -133,11 +153,11 @@
# Example: GET /owner/github_id/639823
#
# @param owner [String] username or github ID
# @return [Success, RequestError]
def owner(owner = username)
- if /^\d+$/ === owner
+ if /^\d+$/ === "#{owner}"
get("#{self[]}/owner/github_id/#{owner}")
else
get("#{self[]}/owner/#{owner}")
end
end
@@ -279,11 +299,11 @@
# **Sortable by:** id, github_id, owner_name, name, active, default_branch.last_build, append :desc to any attribute to reverse order.
#
# @param owner [String] username or github ID
# @return [Success, RequestError]
def repositories(owner = username)
- if /^\d+$/ === owner
+ if /^\d+$/ === "#{owner}"
get("#{self[]}/owner/github_id/#{owner}/repos#{opts}")
else
get("#{self[]}/owner/#{owner}/repos#{opts}")
end
end
@@ -883,40 +903,40 @@
#
# @note DELETE is unimplemented
#
# @param id [String, Integer] the job id number
# @param option [Symbol] options for :text or :delete
- # @return [Success, RequestError]
+ # @return [Success, String, RequestError]
def log(id, option = nil)
case option
when :text
- get("#{self[]}/job/#{id}/log.txt")
+ get("#{self[]}/job/#{id}/log.txt", true)
when :delete
raise Unimplemented
else
get("#{self[]}/job/#{id}/log")
end
end
private # @private
def [](repository = false)
- [API_ENDPOINT].tap {|a| a.push("repo/#{@repo}") if repository }.join('/')
+ [api_endpoint()].tap {|a| a.push("repo/#{@repo}") if repository }.join('/')
end
def repository_name
@repo
end
def opts
@options
end
- def get(x)
- Trav3::GET.(x, headers())
+ def get(x, raw_reply = false)
+ Trav3::GET.(self, x, raw_reply)
end
def post(x, fields = {})
- Trav3::POST.(x, headers(), fields)
+ Trav3::POST.(self, x, fields)
end
def username
@repo[/.*?(?=(?:\/|%2F)|$)/]
end