lib/trav3.rb in trav3-0.3.4 vs lib/trav3.rb in trav3-0.4.0

- old
+ new

@@ -35,40 +35,35 @@ self.repository = repo initial_defaults end - # rubocop:disable Lint/Void # Set as the API endpoint # # @param endpoint [String] name for value to set - # @return [self] + # @return [Travis] def api_endpoint=(endpoint) validate_api_endpoint endpoint - @api_endpoint = endpoint - - self end # Set the authorization token in the requests' headers # # @param token [String] sets authorization token header - # @return [self] + # @return [Travis] def authorization=(token) validate_string token h('Authorization': "token #{token}") - self end # Set as many options as you'd like for collections queried via an API request # # @overload defaults(key: value, ...) # @param key [Symbol] name for value to set # @param value [Symbol, String, Integer] value for key - # @return [self] + # @return [Travis] def defaults(**args) (@options ||= Options.new).build(args) self end @@ -77,26 +72,24 @@ # h("Authorization": "token xxxxxxxxxxxxxxxxxxxxxx") # # @overload h(key: value, ...) # @param key [Symbol] name for value to set # @param value [Symbol, String, Integer] value for key - # @return [self] + # @return [Travis] def h(**args) (@headers ||= Headers.new).build(args) self end # Change the repository this instance of `Trav3::Travis` uses. # # @param repo_name [String] github_username/repository_name - # @return [self] + # @return [Travis] def repository=(repo_name) validate_repo_format repo_name @repo = sanitize_repo_name repo_name - self end - # rubocop:enable Lint/Void # Please Note that the naming of this endpoint may be changed. Our naming convention for this information is in flux. If you have suggestions for how this information should be presented please leave us feedback by commenting in this issue here or emailing support support@travis-ci.com. # # A list of all the builds in an "active" state, either created or started. # @@ -152,10 +145,106 @@ def active(owner = username) number?(owner) and return get("#{without_repo}/owner/github_id/#{owner}/active") get("#{without_repo}/owner/#{owner}/active") end + # A beta feature (a Travis-CI feature currently in beta). + # + # ## Attributes + # + # Name Type Description + # id Integer Value uniquely identifying the beta feature. + # name String The name of the feature. + # description String Longer description of the feature. + # enabled Boolean Indicates if the user has this feature turned on. + # feedback_url String Url for users to leave Travis CI feedback on this feature. + # + # ## Actions + # + # **Update** + # + # This will update a user's beta_feature. + # + # Use namespaced params in the request body to pass the `enabled` value (either true or false): + # + # ```bash + # curl -X PATCH \ + # -H "Content-Type: application/json" \ + # -H "Travis-API-Version: 3" \ + # -H "Authorization: token xxxxxxxxxxxx" \ + # -d '{"beta_feature.enabled":true}' \ + # https://api.travis-ci.com/user/1234/{beta_feature.id} + # ``` + # + # PATCH <code>/user/{user.id}/beta_feature/{beta_feature.id}</code> + # + # Template Variable Type Description + # user.id Integer Value uniquely identifying the user. + # beta_feature.id Integer Value uniquely identifying the beta feature. + # Accepted Parameter Type Description + # beta_feature.id Integer Value uniquely identifying the beta feature. + # beta_feature.enabled Boolean Indicates if the user has this feature turned on. + # + # **Delete** + # + # This will delete a user's beta feature. + # + # DELETE <code>/user/{user.id}/beta_feature/{beta_feature.id}</code> + # + # Template Variable Type Description + # user.id Integer Value uniquely identifying the user. + # beta_feature.id Integer Value uniquely identifying the beta feature. + # + # @param action [Symbol] either `:enable`, `:disable` or `:delete` + # @param beta_feature_id [String, Integer] id for the beta feature + # @param user_id [String] user id + # @return [Success, RequestError] + def beta_feature(action, beta_feature_id, user_id) + validate_number beta_feature_id + validate_number user_id + + if action != :delete + params = { 'beta_feature.id' => beta_feature_id, 'beta_feature.enabled' => action == :enable } + + patch("#{without_repo}/user/#{user_id}/beta_feature/#{beta_feature_id}", params) + else + delete("#{without_repo}/user/#{user_id}/beta_feature/#{beta_feature_id}") + end + end + + # A list of beta features. Beta features are new Travis CI features in beta mode. They can be toggled on or off via the API or on this page on our site: https://travis-ci.com/features + # + # ## Attributes + # + # Name Type Description + # beta_features [Beta feature] List of beta_features. + # + # ## Actions + # + # **Find** + # + # This will return a list of beta features available to a user. + # + # GET <code>/user/{user.id}/beta_features</code> + # + # Template Variable Type Description + # user.id Integer Value uniquely identifying the user. + # Query Parameter Type Description + # include [String] List of attributes to eager load. + # + # Example: GET /user/119240/beta_features + # + # @note requests require an authorization token set in the headers. See: {authorization=} + # + # @param user_id [String, Integer] user id + # @return [Success, RequestError] + def beta_features(user_id) + validate_number user_id + + get("#{without_repo}/user/#{user_id}/beta_features") + end + # The branch of a GitHub repository. Useful for obtaining information about the last build on a given branch. # # **If querying using the repository slug, it must be formatted using {http://www.w3schools.com/tags/ref_urlencode.asp standard URL encoding}, including any special characters.** # # ## Attributes @@ -279,10 +368,51 @@ # @return [Success, RequestError] def branches get("#{with_repo}/branches#{opts}") end + # A list of broadcasts for the current user. + # + # ## Attributes + # + # Name Type Description + # broadcasts [Broadcast] List of broadcasts. + # + # **Collection Items** + # + # Each entry in the broadcasts array has the following attributes: + # + # Name Type Description + # id Integer Value uniquely identifying the broadcast. + # message String Message to display to the user. + # created_at String When the broadcast was created. + # category String Broadcast category (used for icon and color). + # active Boolean Whether or not the brodacast should still be displayed. + # recipient Object Either a user, organization or repository, or null for global. + # + # ## Actions + # + # **For Current User** + # + # This will return a list of broadcasts for the current user. + # + # GET <code>/broadcasts</code> + # + # Query Parameter Type Description + # active [Boolean] Alias for broadcast.active. + # broadcast.active [Boolean] Filters broadcasts by whether or not the brodacast should still be displayed. + # include [String] List of attributes to eager load. + # + # Example: GET /broadcasts + # + # @note requests require an authorization token set in the headers. See: {authorization=} + # + # @return [Success, RequestError] + def broadcasts + get("#{without_repo}/broadcasts") + end + # An individual build. # # ## Attributes # # **Minimal Representation** @@ -1084,16 +1214,11 @@ # @note requests require an authorization token set in the headers. See: {authorization=} # # @param create [Hash] Optional argument. A hash of the `name`, `value`, and `public` visibleness for a env var to create # @return [Success, RequestError] def env_vars(create = nil) - if create - validate_env_var create - - return create("#{with_repo}/env_vars", env_var_keys(create)) - end - + create and return create("#{with_repo}/env_vars", env_var_keys(create)) get("#{with_repo}/env_vars") end # A GitHub App installation. # @@ -2728,11 +2853,11 @@ def get_path(url) get("#{without_repo}#{url}") end def get_path_with_opts(url) - url, opt = url.match(/(.+)\?(.*)/)&.captures || url + url, opt = url.match(/^(.+)\?(.*)$/)&.captures || url opts.immutable do |o| o.send(:update, opt) get_path("#{url}#{opts}") end end @@ -2743,11 +2868,12 @@ h('Accept': 'application/json') h('Travis-API-Version': 3) end def inject_property_name(name, hash) - return hash.map { |k, v| ["#{name}.#{k}", v] }.to_h unless hash.keys.first.match?(/\A#{name}\.\w+\z/) + raise TypeError, "Hash expected, #{input.class} given" unless hash.is_a? Hash + return hash.map { |k, v| ["#{name}.#{k}", v] }.to_h unless hash.keys.first.match?(/^#{name}\.\w+$/) hash end def key_pair_keys(hash) @@ -2771,23 +2897,9 @@ end def validate_api_endpoint(input) raise InvalidAPIEndpoint unless /^https:\/\/api\.travis-ci\.(?:org|com)$/.match? input end - - # rubocop:disable Metrics/CyclomaticComplexity - def validate_env_var(input) - raise TypeError, "Hash expected, #{input.class} given" unless input.is_a? Hash - raise EnvVarError unless input.all? do |k, v| - k.match?(/name|value|public/) && - case k.to_s - when /name/ then v.is_a? String - when /value/ then v.is_a? String - when /public/ then [true, false].include? v - end - end - end - # rubocop:enable Metrics/CyclomaticComplexity def validate_number(input) raise TypeError, "Integer expected, #{input.class} given" unless number? input end