trav3 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0e2d396b8b3a4e5aa159cf69661b88b2d3919bb7e9aceac6658251592e69f02
4
- data.tar.gz: e3956e9b90b658689b21026d4e69c1198eb2ad10d15f9376bef4c16bb95eae6f
3
+ metadata.gz: df20e3328d17a3337d9a09631285a18d5aa3d59381651c09db938f0e95edd324
4
+ data.tar.gz: 583fc08a041c80d8ebee2bc0684b7cea15a8888defdb6bd3bc5d17165406c1ae
5
5
  SHA512:
6
- metadata.gz: 111a2df7f87c5748c86d319231f6b0a09c0dd47507ecabcb20844fb8b4335cc2841377e52d01f82305e3111329dc81470b8bfee126289df068ccb174d2483390
7
- data.tar.gz: 2dda90b5419e0f1761344ba6ce1d365e2af5565ca8e9095cd123d6c2d1eb9f17f706b1f10d4b8ef22599fa6a2a982a7949937185f761c52afebf679b960cf3d9
6
+ metadata.gz: 4c08c5042a02094911029e1ddfab0cfb3e2f960f2fcc54222ddc5c020a9dff19f973f8b6593a893dd95e27c5fc389546115b4bf732ba9861c2dfa947f800b55d
7
+ data.tar.gz: d7cf95030b983f865874cfc033954597222af945b3c11a00e907fea4463caf6b5187c50059e17e94e4f893897cb221b2cb4bf9cafe10413c21a3d40c177d68d4
@@ -2150,7 +2150,7 @@ Style/Alias:
2150
2150
  Style/AndOr:
2151
2151
  Description: 'Use &&/|| instead of and/or.'
2152
2152
  StyleGuide: '#no-and-or-or'
2153
- Enabled: true
2153
+ Enabled: false
2154
2154
  VersionAdded: '0.9'
2155
2155
  VersionChanged: '0.25'
2156
2156
  # Whether `and` and `or` are banned only in conditionals (conditionals)
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/trav3.svg)](http://badge.fury.io/rb/trav3)
2
2
  [![Build Status](https://travis-ci.org/danielpclark/trav3.svg?branch=master)](https://travis-ci.org/danielpclark/trav3)
3
+ [![Commits Since Release](https://img.shields.io/github/commits-since/danielpclark/trav3/v0.2.5.svg)](https://github.com/danielpclark/trav3/graphs/commit-activity)
3
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/1ed07a4baea3832b6207/maintainability)](https://codeclimate.com/github/danielpclark/trav3/maintainability)
4
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/1ed07a4baea3832b6207/test_coverage)](https://codeclimate.com/github/danielpclark/trav3/test_coverage)
5
6
  [![Red The Docs](https://img.shields.io/badge/Read-the%20docs-blue.svg)](http://danielpclark.github.io/trav3/Trav3/Travis.html)
@@ -2,16 +2,16 @@
2
2
 
3
3
  # rubocop:disable Metrics/ClassLength
4
4
  require 'trav3/version'
5
+ require 'forwardable'
6
+ require 'trav3/errors'
7
+ require 'trav3/response'
5
8
  require 'trav3/pagination'
6
9
  require 'trav3/options'
7
10
  require 'trav3/headers'
8
- require 'trav3/result'
9
11
  require 'trav3/rest'
10
12
 
11
13
  # Trav3 project namespace
12
14
  module Trav3
13
- API_ROOT = 'https://api.travis-ci.org'
14
-
15
15
  # An abstraction for the Travis CI v3 API
16
16
  #
17
17
  # @author Daniel P. Clark https://6ftdan.com
@@ -22,7 +22,6 @@ module Trav3
22
22
  # @!attribute [r] headers
23
23
  # @return [Headers] Request headers object
24
24
  class Travis
25
- API_ENDPOINT = API_ROOT
26
25
  attr_reader :api_endpoint
27
26
  attr_reader :options
28
27
  attr_reader :headers
@@ -34,7 +33,7 @@ module Trav3
34
33
  def initialize(repo)
35
34
  validate_repo_format repo
36
35
 
37
- @api_endpoint = API_ENDPOINT
36
+ @api_endpoint = 'https://api.travis-ci.org'
38
37
  @repo = sanitize_repo_name repo
39
38
 
40
39
  initial_defaults
@@ -60,7 +59,7 @@ module Trav3
60
59
  # @param value [Symbol, String, Integer] value for key
61
60
  # @return [self]
62
61
  def defaults(**args)
63
- (@options ||= Options.new).build(**args)
62
+ (@options ||= Options.new).build(args)
64
63
  self
65
64
  end
66
65
 
@@ -73,7 +72,7 @@ module Trav3
73
72
  # @param value [Symbol, String, Integer] value for key
74
73
  # @return [self]
75
74
  def h(**args)
76
- (@headers ||= Headers.new).build(**args)
75
+ (@headers ||= Headers.new).build(args)
77
76
  self
78
77
  end
79
78
 
@@ -770,7 +769,7 @@ module Trav3
770
769
  validate_string yaml_content
771
770
 
772
771
  ct = headers.remove(:'Content-Type')
773
- result = post("#{without_repo}/lint", body: yaml_content)
772
+ result = post("#{without_repo}/lint", yaml_content)
774
773
  h('Content-Type': ct) if ct
775
774
  result
776
775
  end
@@ -1050,6 +1049,131 @@ module Trav3
1050
1049
  end
1051
1050
  end
1052
1051
 
1052
+ # Document `resources/preference/overview` not found.
1053
+ #
1054
+ # ## Attributes
1055
+ #
1056
+ # **Standard Representation**
1057
+ #
1058
+ # Included when the resource is the main response of a request, or is {https://developer.travis-ci.com/eager-loading eager loaded}.
1059
+ #
1060
+ # Name Type Description
1061
+ # name Unknown The preference's name.
1062
+ # value Unknown The preference's value.
1063
+ #
1064
+ # **Minimal Representation**
1065
+ #
1066
+ # Included when the resource is returned as part of another resource.
1067
+ #
1068
+ # Name Type Description
1069
+ # name Unknown The preference's name.
1070
+ # value Unknown The preference's value.
1071
+ #
1072
+ # ## Actions
1073
+ #
1074
+ # **For Organization**
1075
+ #
1076
+ # Document `resources/preference/actions/for_organization` not found.
1077
+ #
1078
+ # GET <code>/org/{organization.id}/preference/{preference.name}</code>
1079
+ #
1080
+ # Template Variable Type Description
1081
+ # organization.id Integer Value uniquely identifying the organization.
1082
+ # preference.name Unknown The preference's name.
1083
+ # Query Parameter Type Description
1084
+ # include [String] List of attributes to eager load.
1085
+ #
1086
+ # **Update**
1087
+ #
1088
+ # Document `resources/preference/actions/update` not found.
1089
+ #
1090
+ # PATCH <code>/org/{organization.id}/preference/{preference.name}</code>
1091
+ #
1092
+ # Template Variable Type Description
1093
+ # organization.id Integer Value uniquely identifying the organization.
1094
+ # preference.name Unknown The preference's name.
1095
+ # Accepted Parameter Type Description
1096
+ # preference.value Unknown The preference's value.
1097
+ #
1098
+ # PATCH <code>/preference/{preference.name}</code>
1099
+ #
1100
+ # Template Variable Type Description
1101
+ # preference.name Unknown The preference's name.
1102
+ # Accepted Parameter Type Description
1103
+ # preference.value Unknown The preference's value.
1104
+ #
1105
+ # **Find**
1106
+ #
1107
+ # Document `resources/preference/actions/find` not found.
1108
+ #
1109
+ # GET <code>/preference/{preference.name}</code>
1110
+ #
1111
+ # Template Variable Type Description
1112
+ # preference.name Unknown The preference's name.
1113
+ # Query Parameter Type Description
1114
+ # include [String] List of attributes to eager load.
1115
+ #
1116
+ # @param key [String] preference name to get or set
1117
+ # @param value [String] optional value to set preference
1118
+ # @param org_id [String, Integer] optional keyword argument for an organization id
1119
+ # @return [Success, RequestError]
1120
+ def preference(key, value = nil, org_id: nil)
1121
+ if org_id
1122
+ validate_number org_id
1123
+ org_id = "/org/#{org_id}"
1124
+ end
1125
+
1126
+ if value.nil?
1127
+ get("#{without_repo}#{org_id}/preference/#{key}")
1128
+ else
1129
+ patch("#{without_repo}#{org_id}/preference/#{key}", 'preference.value' => value)
1130
+ end
1131
+ end
1132
+
1133
+ # Document `resources/preferences/overview` not found.
1134
+ #
1135
+ # ## Attributes
1136
+ #
1137
+ # Name Type Description
1138
+ # preferences [Preferenc] List of preferences.
1139
+ #
1140
+ # ## Actions
1141
+ #
1142
+ # **For Organization**
1143
+ #
1144
+ # Document `resources/preferences/actions/for_organization` not found.
1145
+ #
1146
+ # GET <code>/org/{organization.id}/preferences</code>
1147
+ #
1148
+ # Template Variable Type Description
1149
+ # organization.id Integer Value uniquely identifying the organization.
1150
+ # Query Parameter Type Description
1151
+ # include [String] List of attributes to eager load.
1152
+ #
1153
+ # Example: GET /org/87/preferences
1154
+ #
1155
+ # **For User**
1156
+ #
1157
+ # Document `resources/preferences/actions/for_user` not found.
1158
+ #
1159
+ # GET <code>/preferences</code>
1160
+ #
1161
+ # Query Parameter Type Description
1162
+ # include [String] List of attributes to eager load.
1163
+ #
1164
+ # Example: GET /preferences
1165
+ #
1166
+ # @param org_id [String, Integer] optional organization id
1167
+ # @return [Success, RequestError]
1168
+ def preferences(org_id = nil)
1169
+ if org_id
1170
+ validate_number org_id
1171
+ org_id = "/org/#{org_id}"
1172
+ end
1173
+
1174
+ get("#{without_repo}#{org_id}/preferences")
1175
+ end
1176
+
1053
1177
  # A list of repositories for the current user.
1054
1178
  #
1055
1179
  # ## Attributes
@@ -1838,6 +1962,18 @@ module Trav3
1838
1962
  Trav3::REST.get(self, url, raw_reply)
1839
1963
  end
1840
1964
 
1965
+ def get_path(url)
1966
+ get("#{without_repo}#{url}")
1967
+ end
1968
+
1969
+ def get_path_with_opts(url)
1970
+ url, opt = url.match(/(.+)\?(.*)/)&.captures || url
1971
+ opts.immutable do |o|
1972
+ o.send(:update, opt)
1973
+ get_path("#{url}#{opts}")
1974
+ end
1975
+ end
1976
+
1841
1977
  def initial_defaults
1842
1978
  defaults(limit: 25)
1843
1979
  h('Content-Type': 'application/json')
@@ -1857,8 +1993,8 @@ module Trav3
1857
1993
  Trav3::REST.patch(self, url, data)
1858
1994
  end
1859
1995
 
1860
- def post(url, fields = {})
1861
- Trav3::REST.post(self, url, fields)
1996
+ def post(url, body = nil)
1997
+ Trav3::REST.post(self, url, body)
1862
1998
  end
1863
1999
 
1864
2000
  def validate_api_endpoint(input)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class InvalidAPIEndpoint < StandardError
5
+ def message
6
+ "The API endpoint must be either
7
+ 'https://api.travis-ci.com' or
8
+ 'https://api.travis-ci.org'"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class InvalidRepository < StandardError
5
+ def message
6
+ "The repository format was invlaid.
7
+ You must either provide the digit name for the repository,
8
+ or `user/repo` or `user%2Frepo` as the name."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class Unimplemented < StandardError
5
+ def message
6
+ 'This feature is not implemented.'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'error/invalid_api_endpoint'
4
+ require_relative 'error/invalid_repository'
5
+ require_relative 'error/unimplemented'
@@ -7,11 +7,11 @@ module Trav3
7
7
  extend Forwardable
8
8
  def_delegators :@heads, :each_pair
9
9
 
10
- def initialize(**args)
11
- build(**args)
10
+ def initialize(args = {})
11
+ build(args)
12
12
  end
13
13
 
14
- def build(**args)
14
+ def build(args = {})
15
15
  @heads ||= {}
16
16
 
17
17
  args.each do |(key, value)|
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Trav3
4
4
  class Options
5
- def initialize(**args)
6
- build(**args)
5
+ def initialize(args = {})
6
+ build(args)
7
7
  end
8
8
 
9
9
  def opts
@@ -14,7 +14,7 @@ module Trav3
14
14
  end
15
15
  end
16
16
 
17
- def build(**args)
17
+ def build(args = {})
18
18
  @opts ||= []
19
19
 
20
20
  args.each do |(key, value)|
@@ -41,6 +41,13 @@ module Trav3
41
41
  result
42
42
  end
43
43
 
44
+ def immutable
45
+ old = @opts
46
+ result = yield self
47
+ @opts = old
48
+ result
49
+ end
50
+
44
51
  def remove(key)
45
52
  return_value = nil
46
53
 
@@ -62,7 +69,7 @@ module Trav3
62
69
  def +(other)
63
70
  raise TypeError, "Options type expected, #{other.class} given" unless other.is_a? Options
64
71
 
65
- @opts += other.instance_variable_get(:@opts)
72
+ update other.instance_variable_get(:@opts)
66
73
 
67
74
  self
68
75
  end
@@ -80,5 +87,17 @@ module Trav3
80
87
  def split
81
88
  ->(entry) { entry.split('=') }
82
89
  end
90
+
91
+ def parse(other)
92
+ return other.split('&').map(&split).to_h if other.is_a? String
93
+
94
+ other.map(&split).to_h
95
+ end
96
+
97
+ def update(other)
98
+ return self unless other
99
+
100
+ build(parse(other))
101
+ end
83
102
  end
84
103
  end
@@ -2,33 +2,37 @@
2
2
 
3
3
  module Trav3
4
4
  class Pagination
5
+ attr_reader :travis
5
6
  def initialize(travis, result)
6
7
  @travis = travis
7
8
  @result = result
8
9
  end
9
10
 
10
- def dig(opt)
11
- @result.dig(opt)
12
- end
13
-
14
11
  def next
15
- get("#{API_ROOT}#{dig('@pagination').dig('next').dig('@href')}")
12
+ get(action(:next))
16
13
  end
17
14
 
18
15
  def first
19
- get("#{API_ROOT}#{dig('@pagination').dig('first').dig('@href')}")
16
+ get(action(:first))
20
17
  end
21
18
 
22
19
  def last
23
- get("#{API_ROOT}#{dig('@pagination').dig('last').dig('@href')}")
20
+ get(action(:last))
21
+ end
22
+
23
+ private
24
+
25
+ def action(action)
26
+ dig('@pagination').dig(action.to_s).dig('@href')
24
27
  end
25
28
 
26
- def get(url)
27
- Trav3::REST.get(travis, url)
29
+ def dig(opt)
30
+ @result.dig(opt)
28
31
  end
29
- private :get
30
32
 
31
- attr_reader :travis
33
+ def get(path)
34
+ travis.send(:get_path, path.to_s)
35
+ end
32
36
  private :travis
33
37
  end
34
38
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'response/response_collection'
4
+ require_relative 'response/response'
5
+ require_relative 'response/success'
6
+ require_relative 'response/request_error'
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class RequestError < Response
5
+ def success?
6
+ false
7
+ end
8
+
9
+ def failure?
10
+ true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Trav3
6
+ class Response
7
+ extend Forwardable
8
+ attr_reader :travis
9
+ def_delegators :@collection, *ResponseCollection.instance_methods.-(Object.methods)
10
+ def_delegators :@response, :code, :code_type, :uri, :message, :read_header,
11
+ :header, :value, :entity, :response, :body, :decode_content,
12
+ :msg, :reading_body, :read_body, :http_version,
13
+ :connection_close?, :connection_keep_alive?,
14
+ :initialize_http_header, :get_fields, :each_header
15
+ def initialize(travis, response)
16
+ @travis = travis
17
+ @response = response
18
+ @collection = begin
19
+ result = JSON.parse(response.body)
20
+ ResponseCollection.new(travis, result)
21
+ rescue JSON::ParserError
22
+ response.error!
23
+ end
24
+ end
25
+
26
+ def inspect
27
+ "<#{self.class} Response: keys = #{keys}>"
28
+ end
29
+
30
+ def success?
31
+ raise Unimplemented
32
+ end
33
+
34
+ def failure?
35
+ raise Unimplemented
36
+ end
37
+ private :travis
38
+ end
39
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class ResponseCollection
5
+ extend Forwardable
6
+ def_delegators :@collection, :count, :keys, :values, :has_key?, :key?
7
+ def initialize(travis, collection)
8
+ @travis = travis
9
+ @collection = collection
10
+ end
11
+
12
+ def [](target)
13
+ result = collection[target]
14
+ return ResponseCollection.new(travis, result) if collection?(result)
15
+
16
+ result
17
+ end
18
+
19
+ def dig(*target)
20
+ return collection.dig(*target) if target.length != 1
21
+
22
+ result = collection.dig(*target)
23
+ return ResponseCollection.new(travis, result) if collection?(result)
24
+
25
+ result
26
+ end
27
+
28
+ def each(&block)
29
+ return collection.each(&block) if hash?
30
+
31
+ collection.each do |item|
32
+ yield ResponseCollection.new(travis, item)
33
+ end
34
+ end
35
+
36
+ def fetch(idx)
37
+ result = collection.fetch(idx) { nil }
38
+ return ResponseCollection.new(travis, result) if collection?(result)
39
+ return result if result
40
+
41
+ # For error raising behavior
42
+ collection.fetch(idx) unless block_given?
43
+
44
+ yield
45
+ end
46
+
47
+ def first
48
+ self[0]
49
+ end
50
+
51
+ def follow(idx = nil)
52
+ if href? && !idx
53
+ url = collection.fetch('@href')
54
+ return travis.send(:get_path_with_opts, url)
55
+ end
56
+
57
+ result = fetch(idx)
58
+ result.follow
59
+ end
60
+
61
+ def hash?
62
+ collection.is_a? Hash
63
+ end
64
+
65
+ def last
66
+ self[-1]
67
+ end
68
+
69
+ private
70
+
71
+ def collection?(input)
72
+ [Array, Hash].include? input.class
73
+ end
74
+
75
+ def href?
76
+ collection.respond_to?(:key?) and collection.key?('@href')
77
+ end
78
+
79
+ attr_reader :travis
80
+ attr_reader :collection
81
+ end
82
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trav3
4
+ class Success < Response
5
+ def page
6
+ Trav3::Pagination.new(travis, self)
7
+ end
8
+
9
+ def success?
10
+ true
11
+ end
12
+
13
+ def failure?
14
+ false
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,107 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'rest/create'
4
- require_relative 'rest/delete'
5
- require_relative 'rest/get'
6
- require_relative 'rest/patch'
7
- require_relative 'rest/post'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+
7
+ module Trav3
8
+ module REST
9
+ class << self
10
+ def create(travis, url, **data)
11
+ uri = as_uri url
12
+ req = request_post uri
13
+ set_headers travis, req
14
+ set_json_body req, data
15
+ response = get_response uri, req
16
+
17
+ output travis, response
18
+ end
19
+
20
+ def delete(travis, url)
21
+ uri = as_uri url
22
+ req = request_delete uri
23
+ set_headers travis, req
24
+ response = get_response uri, req
25
+
26
+ output travis, response
27
+ end
28
+
29
+ def get(travis, url, raw_reply = false)
30
+ uri = as_uri url
31
+ req = request_get uri
32
+ set_headers travis, req
33
+ response = get_response uri, req
34
+
35
+ return response.body if raw_reply
36
+
37
+ output travis, response
38
+ end
39
+
40
+ def patch(travis, url, data = {})
41
+ uri = as_uri url
42
+ req = request_patch uri
43
+ set_headers travis, req
44
+ set_json_body req, data
45
+ response = get_response uri, req
46
+
47
+ output travis, response
48
+ end
49
+
50
+ def post(travis, url, body = nil)
51
+ uri = as_uri url
52
+ req = request_post uri
53
+ set_headers travis, req
54
+ req.body = body if body
55
+ response = get_response uri, req
56
+
57
+ output travis, response
58
+ end
59
+
60
+ private
61
+
62
+ def as_uri(url)
63
+ URI( url )
64
+ end
65
+
66
+ def get_response(uri, request)
67
+ http = Net::HTTP.new(uri.host, uri.port)
68
+ http.use_ssl = (uri.scheme == 'https')
69
+ http.request(request)
70
+ end
71
+
72
+ def output(travis, response)
73
+ if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
74
+ Success.new(travis, response)
75
+ else
76
+ RequestError.new(travis, response)
77
+ end
78
+ end
79
+
80
+ def request_delete(uri)
81
+ Net::HTTP::Delete.new(uri.request_uri)
82
+ end
83
+
84
+ def request_get(uri)
85
+ Net::HTTP::Get.new(uri.request_uri)
86
+ end
87
+
88
+ def request_patch(uri)
89
+ Net::HTTP::Patch.new(uri.request_uri)
90
+ end
91
+
92
+ def request_post(uri)
93
+ Net::HTTP::Post.new(uri.request_uri)
94
+ end
95
+
96
+ def set_headers(travis, request)
97
+ travis.headers.each_pair do |header, value|
98
+ request[header] = value
99
+ end
100
+ end
101
+
102
+ def set_json_body(req, data = {})
103
+ req.body = JSON.generate(data) unless data.empty?
104
+ end
105
+ end
106
+ end
107
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trav3
4
- VERSION = '0.2.5'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trav3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-21 00:00:00.000000000 Z
11
+ date: 2019-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,16 +86,19 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - lib/trav3.rb
89
+ - lib/trav3/error/invalid_api_endpoint.rb
90
+ - lib/trav3/error/invalid_repository.rb
91
+ - lib/trav3/error/unimplemented.rb
92
+ - lib/trav3/errors.rb
89
93
  - lib/trav3/headers.rb
90
94
  - lib/trav3/options.rb
91
95
  - lib/trav3/pagination.rb
96
+ - lib/trav3/response.rb
97
+ - lib/trav3/response/request_error.rb
98
+ - lib/trav3/response/response.rb
99
+ - lib/trav3/response/response_collection.rb
100
+ - lib/trav3/response/success.rb
92
101
  - lib/trav3/rest.rb
93
- - lib/trav3/rest/create.rb
94
- - lib/trav3/rest/delete.rb
95
- - lib/trav3/rest/get.rb
96
- - lib/trav3/rest/patch.rb
97
- - lib/trav3/rest/post.rb
98
- - lib/trav3/result.rb
99
102
  - lib/trav3/version.rb
100
103
  - trav3.gemspec
101
104
  homepage: https://github.com/danielpclark/trav3
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Metrics/MethodLength
4
- require 'net/http'
5
- require 'uri'
6
- require 'json'
7
-
8
- module Trav3
9
- module REST
10
- def self.create(travis, url, **data)
11
- uri = URI( url.sub(/\?.*$/, '') )
12
- req = Net::HTTP::Post.new(uri.request_uri)
13
- travis.headers.each_pair do |header, value|
14
- req[header] = value
15
- end
16
- req.body = JSON.generate(data) unless data.empty?
17
- http = Net::HTTP.new(uri.host, uri.port)
18
- http.use_ssl = (uri.scheme == 'https')
19
- response = http.request(req)
20
-
21
- if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
22
- Success.new(travis, response)
23
- else
24
- RequestError.new(travis, response)
25
- end
26
- end
27
- end
28
- end
29
- # rubocop:enable Metrics/MethodLength
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Metrics/MethodLength
4
- require 'net/http'
5
- require 'uri'
6
-
7
- module Trav3
8
- module REST
9
- def self.delete(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
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Metrics/MethodLength
4
- require 'net/http'
5
- require 'uri'
6
-
7
- module Trav3
8
- module REST
9
- def self.get(travis, url, raw_reply = false)
10
- uri = URI(url)
11
- req = Net::HTTP::Get.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
- return response.body if raw_reply
20
-
21
- if Net::HTTPOK == response.code_type
22
- Success.new(travis, response)
23
- else
24
- RequestError.new(travis, response)
25
- end
26
- end
27
- end
28
- end
29
- # rubocop:enable Metrics/MethodLength
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Metrics/MethodLength
4
- require 'net/http'
5
- require 'uri'
6
- require 'json'
7
-
8
- module Trav3
9
- module REST
10
- def self.patch(travis, url, data = {})
11
- uri = URI( url.sub(/\?.*$/, '') )
12
- req = Net::HTTP::Patch.new(uri.request_uri)
13
- travis.headers.each_pair do |header, value|
14
- req[header] = value
15
- end
16
- req.body = JSON.generate(data) unless data.empty?
17
- http = Net::HTTP.new(uri.host, uri.port)
18
- http.use_ssl = (uri.scheme == 'https')
19
- response = http.request(req)
20
-
21
- if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
22
- Success.new(travis, response)
23
- else
24
- RequestError.new(travis, response)
25
- end
26
- end
27
- end
28
- end
29
- # rubocop:enable Metrics/MethodLength
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Metrics/MethodLength
4
- require 'net/http'
5
- require 'uri'
6
-
7
- module Trav3
8
- module REST
9
- def self.post(travis, url, **fields)
10
- uri = URI( url.sub(/\?.*$/, '') )
11
- req = Net::HTTP::Post.new(uri.request_uri)
12
- travis.headers.each_pair do |header, value|
13
- req[header] = value
14
- end
15
- body = fields.delete(:body)
16
- req.body = body if body
17
- req.set_form_data(**fields) unless fields.empty?
18
- http = Net::HTTP.new(uri.host, uri.port)
19
- http.use_ssl = (uri.scheme == 'https')
20
- response = http.request(req)
21
-
22
- if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
23
- Success.new(travis, response)
24
- else
25
- RequestError.new(travis, response)
26
- end
27
- end
28
- end
29
- end
30
- # rubocop:enable Metrics/MethodLength
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- module Trav3
6
- class InvalidRepository < StandardError
7
- def message
8
- "The repository format was invlaid.
9
- You must either provide the digit name for the repository,
10
- or `user/repo` or `user%2Frepo` as the name."
11
- end
12
- end
13
-
14
- class InvalidAPIEndpoint < StandardError
15
- def message
16
- "The API endpoint must be either
17
- 'https://api.travis-ci.com' or
18
- 'https://api.travis-ci.org'"
19
- end
20
- end
21
-
22
- class Unimplemented < StandardError
23
- def message
24
- 'This feature is not implemented.'
25
- end
26
- end
27
-
28
- class Response
29
- extend Forwardable
30
- attr_reader :travis
31
- def_delegators :@json, :[], :dig, :keys, :values, :has_key?
32
- def_delegators :@response, :code, :code_type, :uri, :message, :read_header,
33
- :header, :value, :entity, :response, :body, :decode_content,
34
- :msg, :reading_body, :read_body, :http_version,
35
- :connection_close?, :connection_keep_alive?,
36
- :initialize_http_header, :get_fields, :each_header
37
- def initialize(travis, response)
38
- @travis = travis
39
- @response = response
40
- @json = begin
41
- JSON.parse(response.body)
42
- rescue JSON::ParserError
43
- response.error!
44
- end
45
- end
46
-
47
- def inspect
48
- "<#{self.class} Response: keys = #{keys}>"
49
- end
50
-
51
- def success?
52
- raise Unimplemented
53
- end
54
-
55
- def failure?
56
- raise Unimplemented
57
- end
58
- private :travis
59
- end
60
-
61
- class Success < Response
62
- def page
63
- Trav3::Pagination.new(travis, self)
64
- end
65
-
66
- def success?
67
- true
68
- end
69
-
70
- def failure?
71
- false
72
- end
73
- end
74
-
75
- class RequestError < Response
76
- def success?
77
- false
78
- end
79
-
80
- def failure?
81
- true
82
- end
83
- end
84
- end