trav3 0.4.1 → 0.5.0
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 +4 -4
- data/.travis.yml +1 -1
- data/README.md +70 -9
- data/lib/trav3.rb +848 -17
- data/lib/trav3/error/invalid_repository.rb +1 -1
- data/lib/trav3/headers.rb +8 -0
- data/lib/trav3/options.rb +2 -1
- data/lib/trav3/pagination.rb +7 -1
- data/lib/trav3/response/request_error.rb +3 -0
- data/lib/trav3/response/response.rb +109 -6
- data/lib/trav3/response/response_collection.rb +75 -6
- data/lib/trav3/response/success.rb +4 -0
- data/lib/trav3/rest.rb +1 -0
- data/lib/trav3/version.rb +1 -1
- data/trav3.gemspec +2 -0
- metadata +3 -3
data/lib/trav3/headers.rb
CHANGED
@@ -3,8 +3,16 @@
|
|
3
3
|
require 'forwardable'
|
4
4
|
|
5
5
|
module Trav3
|
6
|
+
# A header builder class for outgoing requests
|
6
7
|
class Headers
|
7
8
|
extend Forwardable
|
9
|
+
# @!macro [attach] def_delegators
|
10
|
+
# @!method each_pair
|
11
|
+
# Forwards to $1.
|
12
|
+
# @see Hash#each_pair
|
13
|
+
# @!method fetch
|
14
|
+
# Forwards to $1.
|
15
|
+
# @see Hash#fetch
|
8
16
|
def_delegators :@heads, :each_pair, :fetch
|
9
17
|
|
10
18
|
def initialize(args = {})
|
data/lib/trav3/options.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Trav3
|
4
|
+
# A url options builder class for outgoing requests.
|
4
5
|
class Options
|
5
6
|
def initialize(args = {})
|
6
7
|
build(args)
|
@@ -111,7 +112,7 @@ module Trav3
|
|
111
112
|
@opts.map(&split).to_h
|
112
113
|
end
|
113
114
|
|
114
|
-
private
|
115
|
+
private # @private
|
115
116
|
|
116
117
|
def split
|
117
118
|
->(entry) { entry.split('=') }
|
data/lib/trav3/pagination.rb
CHANGED
@@ -8,19 +8,25 @@ module Trav3
|
|
8
8
|
@result = result
|
9
9
|
end
|
10
10
|
|
11
|
+
# Next page of results
|
12
|
+
# @return [Success, RequestError]
|
11
13
|
def next
|
12
14
|
get(action(:next))
|
13
15
|
end
|
14
16
|
|
17
|
+
# First page of results
|
18
|
+
# @return [Success, RequestError]
|
15
19
|
def first
|
16
20
|
get(action(:first))
|
17
21
|
end
|
18
22
|
|
23
|
+
# Last page of results
|
24
|
+
# @return [Success, RequestError]
|
19
25
|
def last
|
20
26
|
get(action(:last))
|
21
27
|
end
|
22
28
|
|
23
|
-
private
|
29
|
+
private # @private
|
24
30
|
|
25
31
|
def action(action)
|
26
32
|
dig('@pagination').dig(action.to_s).dig('@href')
|
@@ -3,15 +3,115 @@
|
|
3
3
|
require 'forwardable'
|
4
4
|
|
5
5
|
module Trav3
|
6
|
+
# The results from queries return either `Success` or `RequestError` which both
|
7
|
+
# repsond with Hash like query methods for the JSON data or the Net::HTTP resonse object methods.
|
8
|
+
#
|
9
|
+
# The `Response` classes `Success` and `RequestError` forward method calls for all of the instance
|
10
|
+
# methods of a `ResponseCollection` to the collection. And many of the methods calls for the Net::HTTP
|
11
|
+
# response are also available on this class and those method calls are forwarded to the response.
|
6
12
|
class Response
|
7
13
|
extend Forwardable
|
8
14
|
attr_reader :travis
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
# @!macro [attach] def_delegators
|
16
|
+
# @!method []
|
17
|
+
# Forwards to $1
|
18
|
+
# @see ResponseCollection#[]
|
19
|
+
# @!method count
|
20
|
+
# Forwards to $1.
|
21
|
+
# @see ResponseCollection#count
|
22
|
+
# @!method dig
|
23
|
+
# Forwards to $1
|
24
|
+
# @see ResponseCollection#dig
|
25
|
+
# @!method each
|
26
|
+
# Forwards to $1
|
27
|
+
# @see ResponseCollection#each
|
28
|
+
# @!method empty?
|
29
|
+
# Forwards to $1.
|
30
|
+
# @see ResponseCollection#empty?
|
31
|
+
# @!method fetch
|
32
|
+
# Forwards to $1
|
33
|
+
# @see ResponseCollection#fetch
|
34
|
+
# @!method first
|
35
|
+
# Forwards to $1
|
36
|
+
# @see ResponseCollection#first
|
37
|
+
# @!method follow
|
38
|
+
# Forwards to $1
|
39
|
+
# @see ResponseCollection#follow
|
40
|
+
# @!method has_key?
|
41
|
+
# Forwards to $1.
|
42
|
+
# @see ResponseCollection#has_key?
|
43
|
+
# @!method hash?
|
44
|
+
# Forwards to $1
|
45
|
+
# @see ResponseCollection#hash?
|
46
|
+
# @!method key?
|
47
|
+
# Forwards to $1.
|
48
|
+
# @see ResponseCollection#key?
|
49
|
+
# @!method last
|
50
|
+
# Forwards to $1
|
51
|
+
# @see ResponseCollection#last
|
52
|
+
# @!method keys
|
53
|
+
# Forwards to $1.
|
54
|
+
# @see ResponseCollection#keys
|
55
|
+
# @!method values
|
56
|
+
# Forwards to $1.
|
57
|
+
# @see ResponseCollection#values
|
58
|
+
def_delegators :@collection, *ResponseCollection.instance_methods(false)
|
59
|
+
# @!macro [attach] def_delegators
|
60
|
+
# @!method body
|
61
|
+
# Forwards to $1
|
62
|
+
# @see Net::HTTPResponse#body
|
63
|
+
# @!method code
|
64
|
+
# Forwards to $1
|
65
|
+
# @see Net::HTTPResponse#code
|
66
|
+
# @!method code_type
|
67
|
+
# Forwards to $1
|
68
|
+
# @see Net::HTTPResponse#code_type
|
69
|
+
# @!method decode_content
|
70
|
+
# Forwards to $1
|
71
|
+
# @see Net::HTTPResponse#decode_content
|
72
|
+
# @!method each_header
|
73
|
+
# Forwards to $1
|
74
|
+
# @see Net::HTTPHeader#each_header
|
75
|
+
# @!method entity
|
76
|
+
# Forwards to $1
|
77
|
+
# @see Net::HTTPResponse#entity
|
78
|
+
# @!method get_fields
|
79
|
+
# Forwards to $1
|
80
|
+
# @see Net::HTTPHeader#get_fields
|
81
|
+
# @!method header
|
82
|
+
# Forwards to $1
|
83
|
+
# @see Net::HTTPResponse#header
|
84
|
+
# @!method http_version
|
85
|
+
# Forwards to $1
|
86
|
+
# @see Net::HTTPResponse#http_version
|
87
|
+
# @!method message
|
88
|
+
# Forwards to $1
|
89
|
+
# @see Net::HTTPResponse#message
|
90
|
+
# @!method msg
|
91
|
+
# Forwards to $1
|
92
|
+
# @see Net::HTTPResponse#msg
|
93
|
+
# @!method read_body
|
94
|
+
# Forwards to $1
|
95
|
+
# @see Net::HTTPResponse#read_body
|
96
|
+
# @!method read_header
|
97
|
+
# Forwards to $1
|
98
|
+
# @see Net::HTTPResponse#read_header
|
99
|
+
# @!method reading_body
|
100
|
+
# Forwards to $1
|
101
|
+
# @see Net::HTTPResponse#reading_body
|
102
|
+
# @!method response
|
103
|
+
# Forwards to $1
|
104
|
+
# @see Net::HTTPResponse#response
|
105
|
+
# @!method uri
|
106
|
+
# Forwards to $1
|
107
|
+
# @see Net::HTTPResponse#uri
|
108
|
+
# @!method value
|
109
|
+
# Forwards to $1
|
110
|
+
# @see Net::HTTPResponse#value
|
111
|
+
def_delegators :@response, :body, :code, :code_type, :decode_content,
|
112
|
+
:each_header, :entity, :get_fields, :header, :http_version,
|
113
|
+
:message, :msg, :read_body, :read_header, :reading_body, :response,
|
114
|
+
:uri, :value
|
15
115
|
def initialize(travis, response)
|
16
116
|
@travis = travis
|
17
117
|
@response = response
|
@@ -23,14 +123,17 @@ module Trav3
|
|
23
123
|
end
|
24
124
|
end
|
25
125
|
|
126
|
+
# Class name and keys of response
|
26
127
|
def inspect
|
27
128
|
"<#{self.class} Response: keys = #{keys}>"
|
28
129
|
end
|
29
130
|
|
131
|
+
# @abstract
|
30
132
|
def success?
|
31
133
|
raise Unimplemented
|
32
134
|
end
|
33
135
|
|
136
|
+
# @abstract
|
34
137
|
def failure?
|
35
138
|
raise Unimplemented
|
36
139
|
end
|
@@ -3,12 +3,40 @@
|
|
3
3
|
module Trav3
|
4
4
|
class ResponseCollection
|
5
5
|
extend Forwardable
|
6
|
+
# @!macro [attach] def_delegators
|
7
|
+
# @!method count
|
8
|
+
# Forwards to $1.
|
9
|
+
# @see both Hash#count or Array#count
|
10
|
+
# @!method keys
|
11
|
+
# Forwards to $1.
|
12
|
+
# @see Hash#keys
|
13
|
+
# @!method values
|
14
|
+
# Forwards to $1.
|
15
|
+
# @see Hash#values
|
16
|
+
# @!method has_key?
|
17
|
+
# Forwards to $1.
|
18
|
+
# @see Hash#has_key?
|
19
|
+
# @!method key?
|
20
|
+
# Forwards to $1.
|
21
|
+
# @see Hash#key?
|
22
|
+
# @!method empty?
|
23
|
+
# Forwards to $1.
|
24
|
+
# @see both Hash#empty? or Array#empty?
|
6
25
|
def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty?
|
7
26
|
def initialize(travis, collection)
|
8
27
|
@travis = travis
|
9
28
|
@collection = collection
|
10
29
|
end
|
11
30
|
|
31
|
+
# Either the key or index of the item you wish to get depending on
|
32
|
+
# if this collection is a {#hash?} or an array.
|
33
|
+
#
|
34
|
+
# If the item retrieved is a Hash or Array then the returned item
|
35
|
+
# will be another instance of `ResponseCollection`. Otherwise it will
|
36
|
+
# be a `String` unless the target does not exist and then it will be `nil`.
|
37
|
+
#
|
38
|
+
# @param target [String, Integer]
|
39
|
+
# @return [ResponseCollection, String, nil]
|
12
40
|
def [](target)
|
13
41
|
result = collection[target]
|
14
42
|
return ResponseCollection.new(travis, result) if collection?(result)
|
@@ -16,15 +44,26 @@ module Trav3
|
|
16
44
|
result
|
17
45
|
end
|
18
46
|
|
47
|
+
# (see #[])
|
19
48
|
def dig(*target)
|
20
|
-
|
49
|
+
dug, *rest = target
|
21
50
|
|
22
|
-
result = collection.dig(
|
23
|
-
|
51
|
+
result = collection.dig(dug)
|
52
|
+
if collection?(result)
|
53
|
+
rc = ResponseCollection.new(travis, result)
|
54
|
+
return rest.empty? ? rc : rc.dig(*rest)
|
55
|
+
end
|
24
56
|
|
25
57
|
result
|
26
58
|
end
|
27
59
|
|
60
|
+
# When the inner collection is an Array every item iterated
|
61
|
+
# over is yielded to you as a `ResponseCollection`.
|
62
|
+
#
|
63
|
+
# If the inner collection is a {#hash?} then this method acts
|
64
|
+
# as though you've called `each` directly on that `Hash`.
|
65
|
+
#
|
66
|
+
# @yieldparam [Array, ResponseCollection]
|
28
67
|
def each(&block)
|
29
68
|
return collection.each(&block) if hash?
|
30
69
|
|
@@ -33,17 +72,37 @@ module Trav3
|
|
33
72
|
end
|
34
73
|
end
|
35
74
|
|
36
|
-
|
37
|
-
|
75
|
+
# Either the key or index of the item you wish to get depending on
|
76
|
+
# if this collection is a {#hash?} or an array.
|
77
|
+
#
|
78
|
+
# If the item retrieved is a Hash or Array then the returned item
|
79
|
+
# will be another instance of `ResponseCollection`. Otherwise it will
|
80
|
+
# be a `String`.
|
81
|
+
#
|
82
|
+
# If the target does not exist and no block was given this will raise
|
83
|
+
# an exception. If a block was given, then that block will be evaluated
|
84
|
+
# and that return value returned.
|
85
|
+
#
|
86
|
+
# @param target [String, Integer]
|
87
|
+
# @return [ResponseCollection, String, nil]
|
88
|
+
def fetch(target)
|
89
|
+
result = collection.fetch(target) { nil }
|
38
90
|
return ResponseCollection.new(travis, result) if collection?(result)
|
39
91
|
return result if result
|
40
92
|
|
41
93
|
# For error raising behavior
|
42
|
-
collection.fetch(
|
94
|
+
collection.fetch(target) unless block_given?
|
43
95
|
|
44
96
|
yield
|
45
97
|
end
|
46
98
|
|
99
|
+
# When the inner collection is an Array it returns the first
|
100
|
+
# item as either a `ResponseCollection` or a `String`. If the
|
101
|
+
# Array is empty it returns `nil`.
|
102
|
+
#
|
103
|
+
# If the inner collection is a {#hash?} then this simply returns `nil`.
|
104
|
+
#
|
105
|
+
# @return [ResponseCollection, String, nil]
|
47
106
|
def first
|
48
107
|
self[0]
|
49
108
|
end
|
@@ -65,10 +124,20 @@ module Trav3
|
|
65
124
|
result.follow
|
66
125
|
end
|
67
126
|
|
127
|
+
# Reveals if the inner collection is a Hash or not.
|
128
|
+
#
|
129
|
+
# @return [Boolean]
|
68
130
|
def hash?
|
69
131
|
collection.is_a? Hash
|
70
132
|
end
|
71
133
|
|
134
|
+
# When the inner collection is an Array it returns the last
|
135
|
+
# item as either a `ResponseCollection` or a `String`. If the
|
136
|
+
# Array is empty it returns `nil`.
|
137
|
+
#
|
138
|
+
# If the inner collection is a {#hash?} then this simply returns `nil`.
|
139
|
+
#
|
140
|
+
# @return [ResponseCollection, String, nil]
|
72
141
|
def last
|
73
142
|
self[-1]
|
74
143
|
end
|
@@ -1,15 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Trav3
|
4
|
+
# (see Response)
|
4
5
|
class Success < Response
|
6
|
+
# @return [Pagination]
|
5
7
|
def page
|
6
8
|
Trav3::Pagination.new(travis, self)
|
7
9
|
end
|
8
10
|
|
11
|
+
# @return [Boolean]
|
9
12
|
def success?
|
10
13
|
true
|
11
14
|
end
|
12
15
|
|
16
|
+
# @return [Boolean]
|
13
17
|
def failure?
|
14
18
|
false
|
15
19
|
end
|
data/lib/trav3/rest.rb
CHANGED
data/lib/trav3/version.rb
CHANGED
data/trav3.gemspec
CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
+
spec.required_ruby_version = '>= 2.3.0'
|
26
|
+
|
25
27
|
# Generate YARD DOC on install
|
26
28
|
spec.metadata['yard.run'] = 'yri'
|
27
29
|
|
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.
|
4
|
+
version: 0.5.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
|
11
|
+
date: 2019-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 2.3.0
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
requirements:
|
120
120
|
- - ">="
|