trav3 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec72696776f9747858b7651d1995adae3f75846de1078f79d166dceb9cebd3aa
4
- data.tar.gz: 2640ff730d6fe51bd4898f1f62690a24b48230dea7e03c6eda122a358fb66c62
3
+ metadata.gz: c296d42d106927366d13fb6e1f35fc684a70c7dc75f1d9f9c0e78d2eb0cfa892
4
+ data.tar.gz: 6a11b0d89c2d16e6ff7d7b20717f08e3c1f06133d5b8018a06bc81acf50e951c
5
5
  SHA512:
6
- metadata.gz: 3cb082ad8b24ca68f5e03c76a92de944e61dfe46fcf66f631cfc5cf4c6ed4b6f2e5613021a09628e8ff0f9af95cf614cdc9d076b78424b6ce876a5a019bb02ca
7
- data.tar.gz: a5a282d39eeaa1d12e14179a91d53ee49396ed04e55fa65153468fa2fc8defead7fd3f085d9dda510d9d324e5f9431db07e2fb8d2e9dc5b592097a1ff47ea72f
6
+ metadata.gz: 9bbcb15fa5788684920be24d4fb0daf526ca79e4b44bb5401d36c622622fc9a89c544b6f6cd45c8d68dbda53641d0edbf3996f76eaf489c9727194c29499af90
7
+ data.tar.gz: 28eabbd1ef143bde815187a83d4aed45a419aae6d87a66c729bfe7e907a915ce657dfccc72764959d6c8e4d60ad5306d4c6d75cf4ac12b2f3ba6c4574d0dc629
data/.rubocop.yml CHANGED
@@ -15,6 +15,7 @@ AllCops:
15
15
  - '**/Rakefile'
16
16
  Exclude:
17
17
  - '.git/**/*'
18
+ - 'vendor/**/*'
18
19
  # Default formatter will be used if no `-f/--format` option is given.
19
20
  DefaultFormatter: progress
20
21
  # Cop names are displayed in offense messages by default. Change behavior
@@ -78,7 +79,7 @@ AllCops:
78
79
  # is specified in the Gemfile or gems.rb file, RuboCop reads the final value
79
80
  # from the lock file.) If the Ruby version is still unresolved, RuboCop will
80
81
  # use the oldest officially supported Ruby version (currently Ruby 2.2).
81
- TargetRubyVersion: ~
82
+ TargetRubyVersion: 2.3
82
83
 
83
84
  #################### Bundler ###############################
84
85
 
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+ cache: bundler
2
3
 
3
4
  rvm:
4
5
  - 2.4.5
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
 
3
5
  require 'rspec/core/rake_task'
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'trav3'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/MethodLength
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ module Trav3
8
+ module DELETE
9
+ def self.call(travis, url)
10
+ uri = URI( url.sub(/\?.*$/, '') )
11
+ req = Net::HTTP::Delete.new(uri.request_uri)
12
+ travis.headers.each_pair do |header, value|
13
+ req[header] = value
14
+ end
15
+ http = Net::HTTP.new(uri.host, uri.port)
16
+ http.use_ssl = (uri.scheme == 'https')
17
+ response = http.request(req)
18
+
19
+ if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
20
+ Success.new(travis, response)
21
+ else
22
+ RequestError.new(travis, response)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ # rubocop:enable Metrics/MethodLength
data/lib/trav3/get.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  # rubocop:disable Metrics/MethodLength
4
4
  require 'net/http'
5
5
  require 'uri'
6
- require 'json'
7
6
 
8
7
  module Trav3
9
8
  module GET
data/lib/trav3/headers.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'forwardable'
2
4
 
3
5
  module Trav3
data/lib/trav3/options.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trav3
2
4
  class Options
3
5
  def initialize(**args)
@@ -23,6 +25,22 @@ module Trav3
23
25
  self
24
26
  end
25
27
 
28
+ def fetch(key)
29
+ @opts.each do |item|
30
+ return item if key.to_s == split.call(item).first
31
+ end
32
+
33
+ raise KeyError, "key not found #{key}" unless block_given?
34
+
35
+ yield
36
+ end
37
+
38
+ def fetch!(key, &block)
39
+ result = fetch(key, &block)
40
+ remove(key)
41
+ result
42
+ end
43
+
26
44
  def remove(key)
27
45
  return_value = nil
28
46
 
@@ -37,6 +55,8 @@ module Trav3
37
55
 
38
56
  def reset!
39
57
  @opts = []
58
+
59
+ self
40
60
  end
41
61
 
42
62
  def +(other)
data/lib/trav3/post.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  # rubocop:disable Metrics/MethodLength
4
4
  require 'net/http'
5
5
  require 'uri'
6
- require 'json'
7
6
 
8
7
  module Trav3
9
8
  module POST
data/lib/trav3/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trav3
2
- VERSION = '0.2.2'.freeze
4
+ VERSION = '0.2.3'
3
5
  end
data/lib/trav3.rb CHANGED
@@ -6,12 +6,13 @@ require 'trav3/pagination'
6
6
  require 'trav3/options'
7
7
  require 'trav3/headers'
8
8
  require 'trav3/result'
9
+ require 'trav3/delete'
9
10
  require 'trav3/post'
10
11
  require 'trav3/get'
11
12
 
12
13
  # Trav3 project namespace
13
14
  module Trav3
14
- API_ROOT = 'https://api.travis-ci.org'.freeze
15
+ API_ROOT = 'https://api.travis-ci.org'
15
16
 
16
17
  # An abstraction for the Travis CI v3 API
17
18
  #
@@ -33,7 +34,7 @@ module Trav3
33
34
  # conform to valid repository identifier format
34
35
  # @return [Travis]
35
36
  def initialize(repo)
36
- raise InvalidRepository unless repo_slug_or_id? repo
37
+ validate_repo_format repo
37
38
 
38
39
  @api_endpoint = API_ENDPOINT
39
40
  @repo = sanitize_repo_name repo
@@ -47,7 +48,7 @@ module Trav3
47
48
  # @return [self]
48
49
  # rubocop:disable Lint/Void
49
50
  def api_endpoint=(endpoint)
50
- raise InvalidAPIEndpoint unless /^https:\/\/api\.travis-ci\.(?:org|com)$/.match? endpoint
51
+ validate_api_endpoint endpoint
51
52
 
52
53
  @api_endpoint = endpoint
53
54
 
@@ -136,7 +137,7 @@ module Trav3
136
137
  # @param name [String] the branch name for the current repository
137
138
  # @return [Success, RequestError]
138
139
  def branch(name)
139
- get("#{with_repo}/branch/#{name}#{opts}")
140
+ get("#{with_repo}/branch/#{name}")
140
141
  end
141
142
 
142
143
  # A list of branches.
@@ -289,10 +290,21 @@ module Trav3
289
290
  #
290
291
  # @note POST requests require an authorization token set in the headers. See: {h}
291
292
  #
292
- # @param id [String, Integer] the build id number
293
+ # @param build_id [String, Integer] the build id number
294
+ # @param option [Symbol] options for :cancel or :restart
295
+ # @raise [TypeError] if given build id is not a number
293
296
  # @return [Success, RequestError]
294
- def build(id)
295
- get("#{without_repo}/build/#{id}")
297
+ def build(build_id, option = nil)
298
+ validate_number build_id
299
+
300
+ case option
301
+ when :cancel
302
+ post("#{without_repo}/build/#{build_id}/cancel")
303
+ when :restart
304
+ post("#{without_repo}/build/#{build_id}/restart")
305
+ else
306
+ get("#{without_repo}/build/#{build_id}")
307
+ end
296
308
  end
297
309
 
298
310
  # A list of builds.
@@ -472,10 +484,109 @@ module Trav3
472
484
  # **Sortable by:** <code>id</code>, append <code>:desc</code> to any attribute to reverse order.
473
485
  # The default value is id:desc.
474
486
  #
475
- # @param id [String, Integer] the build id number
487
+ # @param build_id [String, Integer] the build id number
488
+ # @return [Success, RequestError]
489
+ def build_jobs(build_id)
490
+ get("#{without_repo}/build/#{build_id}/jobs")
491
+ end
492
+
493
+ # A list of caches.
494
+ #
495
+ # 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.
496
+ #
497
+ # ## Attributes
498
+ #
499
+ # Name Type Description
500
+ # branch String The branch the cache belongs to.
501
+ # match String The string to match against the cache name.
502
+ #
503
+ # ## Actions
504
+ #
505
+ # **Find**
506
+ #
507
+ # This returns all the caches for a repository.
508
+ #
509
+ # It's possible to filter by branch or by regexp match of a string to the cache name.
510
+ #
511
+ # ```bash
512
+ # curl \
513
+ # -H "Content-Type: application/json" \
514
+ # -H "Travis-API-Version: 3" \
515
+ # -H "Authorization: token xxxxxxxxxxxx" \
516
+ # https://api.travis-ci.com/repo/1234/caches?branch=master
517
+ # ```
518
+ #
519
+ # ```bash
520
+ # curl \
521
+ # -H "Content-Type: application/json" \
522
+ # -H "Travis-API-Version: 3" \
523
+ # -H "Authorization: token xxxxxxxxxxxx" \
524
+ # https://api.travis-ci.com/repo/1234/caches?match=linux
525
+ # ```
526
+ #
527
+ # GET <code>/repo/{repository.id}/caches</code>
528
+ #
529
+ # Template Variable Type Description
530
+ # repository.id Integer Value uniquely identifying the repository.
531
+ # Query Parameter Type Description
532
+ # branch [String] Alias for caches.branch.
533
+ # caches.branch [String] Filters caches by the branch the cache belongs to.
534
+ # caches.match [String] Filters caches by the string to match against the cache name.
535
+ # include [String] List of attributes to eager load.
536
+ # match [String] Alias for caches.match.
537
+ #
538
+ # Example: GET /repo/891/caches
539
+ #
540
+ # GET <code>/repo/{repository.slug}/caches</code>
541
+ #
542
+ # Template Variable Type Description
543
+ # repository.slug String Same as {repository.owner.name}/{repository.name}.
544
+ # Query Parameter Type Description
545
+ # branch [String] Alias for caches.branch.
546
+ # caches.branch [String] Filters caches by the branch the cache belongs to.
547
+ # caches.match [String] Filters caches by the string to match against the cache name.
548
+ # include [String] List of attributes to eager load.
549
+ # match [String] Alias for caches.match.
550
+ #
551
+ # Example: GET /repo/rails%2Frails/caches
552
+ #
553
+ # **Delete**
554
+ #
555
+ # This deletes all caches for a repository.
556
+ #
557
+ # As with `find` it's possible to delete by branch or by regexp match of a string to the cache name.
558
+ #
559
+ # ```bash
560
+ # curl -X DELETE \
561
+ # -H "Content-Type: application/json" \
562
+ # -H "Travis-API-Version: 3" \
563
+ # -H "Authorization: token xxxxxxxxxxxx" \
564
+ # https://api.travis-ci.com/repo/1234/caches?branch=master
565
+ # ```
566
+ #
567
+ # DELETE <code>/repo/{repository.id}/caches</code>
568
+ # Template Variable Type Description
569
+ # repository.id Integer Value uniquely identifying the repository.
570
+ #
571
+ # Example: DELETE /repo/891/caches
572
+ #
573
+ # DELETE <code>/repo/{repository.slug}/caches</code>
574
+ #
575
+ # Template Variable Type Description
576
+ # repository.slug String Same as {repository.owner.name}/{repository.name}.
577
+ #
578
+ # Example: DELETE /repo/rails%2Frails/caches
579
+ #
580
+ # @note DELETE requests require an authorization token set in the headers. See: {h}
581
+ #
582
+ # @param delete [Boolean] option for deleting cache(s)
476
583
  # @return [Success, RequestError]
477
- def build_jobs(id)
478
- get("#{without_repo}/build/#{id}/jobs")
584
+ def caches(delete = false)
585
+ if delete
586
+ without_limit { delete("#{with_repo}/caches#{opts}") }
587
+ else
588
+ get("#{with_repo}/caches")
589
+ end
479
590
  end
480
591
 
481
592
  # An individual job.
@@ -560,19 +671,19 @@ module Trav3
560
671
  #
561
672
  # @note POST requests require an authorization token set in the headers. See: {h}
562
673
  #
563
- # @param id [String, Integer] the job id number
674
+ # @param job_id [String, Integer] the job id number
564
675
  # @param option [Symbol] options for :cancel, :restart, or :debug
565
676
  # @return [Success, RequestError]
566
- def job(id, option = nil)
677
+ def job(job_id, option = nil)
567
678
  case option
568
679
  when :cancel
569
- post("#{without_repo}/job/#{id}/cancel")
680
+ post("#{without_repo}/job/#{job_id}/cancel")
570
681
  when :restart
571
- post("#{without_repo}/job/#{id}/restart")
682
+ post("#{without_repo}/job/#{job_id}/restart")
572
683
  when :debug
573
- post("#{without_repo}/job/#{id}/debug")
684
+ post("#{without_repo}/job/#{job_id}/debug")
574
685
  else
575
- get("#{without_repo}/job/#{id}")
686
+ get("#{without_repo}/job/#{job_id}")
576
687
  end
577
688
  end
578
689
 
@@ -596,8 +707,7 @@ module Trav3
596
707
  # @param yaml_content [String] the contents for the file `.travis.yml`
597
708
  # @return [Success, RequestError]
598
709
  def lint(yaml_content)
599
- raise TypeError, "String expected, #{yaml_content.class} given" unless \
600
- yaml_content.is_a? String
710
+ validate_string yaml_content
601
711
 
602
712
  ct = headers.remove(:'Content-Type')
603
713
  result = post("#{without_repo}/lint", body: yaml_content)
@@ -680,17 +790,17 @@ module Trav3
680
790
  #
681
791
  # @note DELETE is unimplemented
682
792
  #
683
- # @param id [String, Integer] the job id number
793
+ # @param job_id [String, Integer] the job id number
684
794
  # @param option [Symbol] options for :text or :delete
685
795
  # @return [Success, String, RequestError]
686
- def log(id, option = nil)
796
+ def log(job_id, option = nil)
687
797
  case option
688
798
  when :text
689
- get("#{without_repo}/job/#{id}/log.txt", true)
799
+ get("#{without_repo}/job/#{job_id}/log.txt", true)
690
800
  when :delete
691
801
  raise Unimplemented
692
802
  else
693
- get("#{without_repo}/job/#{id}/log")
803
+ get("#{without_repo}/job/#{job_id}/log")
694
804
  end
695
805
  end
696
806
 
@@ -744,7 +854,7 @@ module Trav3
744
854
  # @raise [TypeError] if given organization id is not a number
745
855
  # @return [Success, RequestError]
746
856
  def organization(org_id)
747
- raise TypeError, 'Integer expected for organization id' unless /^\d+$/.match? org_id.to_s
857
+ validate_number org_id
748
858
 
749
859
  get("#{without_repo}/org/#{org_id}")
750
860
  end
@@ -796,7 +906,7 @@ module Trav3
796
906
  get("#{without_repo}/orgs")
797
907
  end
798
908
 
799
- # This will be either a user or organization.
909
+ # This will be either a {https://developer.travis-ci.com/resource/user user} or {https://developer.travis-ci.com/resource/organization organization}.
800
910
  #
801
911
  # ## Attributes
802
912
  #
@@ -810,7 +920,7 @@ module Trav3
810
920
  #
811
921
  # **Standard Representation**
812
922
  #
813
- # Included when the resource is the main response of a request, or is eager loaded.
923
+ # Included when the resource is the main response of a request, or is {https://developer.travis-ci.com/eager-loading eager loaded}.
814
924
  #
815
925
  # Name Type Description
816
926
  # id Integer Value uniquely identifying the owner.
@@ -873,7 +983,7 @@ module Trav3
873
983
  # @param owner [String] username or github ID
874
984
  # @return [Success, RequestError]
875
985
  def owner(owner = username)
876
- if /^\d+$/.match? owner.to_s
986
+ if number? owner
877
987
  get("#{without_repo}/owner/github_id/#{owner}")
878
988
  else
879
989
  get("#{without_repo}/owner/#{owner}")
@@ -1019,7 +1129,7 @@ module Trav3
1019
1129
  # @param owner [String] username or github ID
1020
1130
  # @return [Success, RequestError]
1021
1131
  def repositories(owner = username)
1022
- if /^\d+$/.match? owner.to_s
1132
+ if number? owner
1023
1133
  get("#{without_repo}/owner/github_id/#{owner}/repos#{opts}")
1024
1134
  else
1025
1135
  get("#{without_repo}/owner/#{owner}/repos#{opts}")
@@ -1161,7 +1271,7 @@ module Trav3
1161
1271
  # conform to valid repository identifier format
1162
1272
  # @return [Success, RequestError]
1163
1273
  def repository(repo = repository_name, action = nil)
1164
- raise InvalidRepository unless repo_slug_or_id? repo
1274
+ validate_repo_format repo
1165
1275
 
1166
1276
  repo = sanitize_repo_name repo
1167
1277
  action = '' unless %w[star unstar activate deavtivate].include? action.to_s
@@ -1214,7 +1324,7 @@ module Trav3
1214
1324
  # @raise [TypeError] if given build id is not a number
1215
1325
  # @return [Success, RequestError]
1216
1326
  def stages(build_id)
1217
- raise TypeError, 'Integer expected for build id' unless /^\d+$/.match? build_id.to_s
1327
+ validate_number build_id
1218
1328
 
1219
1329
  get("#{without_repo}/build/#{build_id}/stages")
1220
1330
  end
@@ -1299,7 +1409,8 @@ module Trav3
1299
1409
  # @return [Success, RequestError]
1300
1410
  def user(user_id = nil, sync = false)
1301
1411
  return get("#{without_repo}/user") if !user_id && !sync
1302
- raise TypeError, 'Integer expected for user id' unless /^\d+$/.match? user_id.to_s
1412
+
1413
+ validate_number user_id
1303
1414
 
1304
1415
  if sync
1305
1416
  get("#{without_repo}/user/#{user_id}/sync")
@@ -1310,6 +1421,10 @@ module Trav3
1310
1421
 
1311
1422
  private # @private
1312
1423
 
1424
+ def delete(url)
1425
+ Trav3::DELETE.call(self, url)
1426
+ end
1427
+
1313
1428
  def get(url, raw_reply = false)
1314
1429
  Trav3::GET.call(self, url, raw_reply)
1315
1430
  end
@@ -1321,6 +1436,10 @@ module Trav3
1321
1436
  h('Travis-API-Version': 3)
1322
1437
  end
1323
1438
 
1439
+ def number?(input)
1440
+ /^\d+$/.match? input.to_s
1441
+ end
1442
+
1324
1443
  def opts
1325
1444
  @options
1326
1445
  end
@@ -1329,8 +1448,24 @@ module Trav3
1329
1448
  Trav3::POST.call(self, url, fields)
1330
1449
  end
1331
1450
 
1332
- def repo_slug_or_id?(repo)
1333
- Regexp.new(/(^\d+$)|(^\w+(?:\/|%2F){1}\w+$)/).match? repo
1451
+ def validate_api_endpoint(input)
1452
+ raise InvalidAPIEndpoint unless /^https:\/\/api\.travis-ci\.(?:org|com)$/.match? input
1453
+ end
1454
+
1455
+ def validate_number(input)
1456
+ raise TypeError, "Integer expected, #{input.class} given" unless number? input
1457
+ end
1458
+
1459
+ def validate_repo_format(input)
1460
+ raise InvalidRepository unless repo_slug_or_id? input
1461
+ end
1462
+
1463
+ def validate_string(input)
1464
+ raise TypeError, "String expected, #{input.class} given" unless input.is_a? String
1465
+ end
1466
+
1467
+ def repo_slug_or_id?(input)
1468
+ Regexp.new(/(^\d+$)|(^\w+(?:\/|%2F){1}\w+$)/).match? input
1334
1469
  end
1335
1470
 
1336
1471
  def repository_name
@@ -1352,6 +1487,13 @@ module Trav3
1352
1487
  def without_repo
1353
1488
  api_endpoint
1354
1489
  end
1490
+
1491
+ def without_limit
1492
+ limit = opts.remove(:limit)
1493
+ result = yield
1494
+ opts.build(limit: limit) if limit
1495
+ result
1496
+ end
1355
1497
  end
1356
1498
  end
1357
1499
  # rubocop:enable Metrics/ClassLength
data/trav3.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require 'trav3/version'
@@ -23,7 +25,7 @@ Gem::Specification.new do |spec|
23
25
  # Generate YARD DOC on install
24
26
  spec.metadata['yard.run'] = 'yri'
25
27
 
26
- spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'bundler', '> 1.16'
27
29
  spec.add_development_dependency 'factory_bot', '~> 4.11'
28
30
  spec.add_development_dependency 'rake', '~> 10.0'
29
31
  spec.add_development_dependency 'rspec', '~> 3.8'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trav3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
@@ -14,14 +14,14 @@ dependencies:
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
@@ -86,6 +86,7 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - lib/trav3.rb
89
+ - lib/trav3/delete.rb
89
90
  - lib/trav3/get.rb
90
91
  - lib/trav3/headers.rb
91
92
  - lib/trav3/options.rb