gitlab_ruby 0.1.1 → 0.2.1

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
  SHA1:
3
- metadata.gz: 90821189da268f6135e016d179b18e8a735193e5
4
- data.tar.gz: 873b28591624edb594704eb49656e958eac20a8b
3
+ metadata.gz: bb9367bda76771fed3eee0814db63776fcb1f94d
4
+ data.tar.gz: b18be4250cacebbbb8299e577b58755f9b3e6d22
5
5
  SHA512:
6
- metadata.gz: 483d4b7cf43cae4847b1a1dc6bf53023007df4ddfbd9b387f16090128f6c5bae200cb2624c3213c729b6a18071cfd507d4254df06e6a99df1c4f9a2b4572e01c
7
- data.tar.gz: cdf1b08e6cce35ea27578edf31d19e66c0bcc3d5dbe796e966ec3dcdd0b80add053ba555703bbbe86767c311804a6cdd507e5fa6a69abc588691bde10474996e
6
+ metadata.gz: 4da0c49448f9113357e201117b3359ed19a50599a837fe4b1c1c03578169915df6e966c7258a0644c41815ceeeadcc3386e3810ba5258576c644279e88f30309
7
+ data.tar.gz: 7bfeeb554cf07e7b607e8f9b09f1641bd057a71bfa7b34db946fe95fc9ec3839ec559396c289b47ff7051743154716eb461000f791d8f9f259735d0d2ee2358d
data/README.md CHANGED
@@ -118,6 +118,16 @@ Translates to
118
118
  ```
119
119
  Query parameters are passed into the `execute` method
120
120
 
121
+ #### Pagination
122
+ ```ruby
123
+ projects.next_page? # returns true or false
124
+ projects.prev_page? # returns true or false
125
+ projects.next_page # make API call to fetch next page
126
+ projects.prev_page # make API call to fetch prev page
127
+ projects.first_page # make API call to fetch first page
128
+ projects.last_page # make API call to fetch last page
129
+ ```
130
+
121
131
  #### Complete API
122
132
 
123
133
  Refer to the complete [https://docs.gitlab.com/ce/api/](documentation) endpoints.
@@ -135,9 +145,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
135
145
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gitlab_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
136
146
 
137
147
  ## TODO
138
- [] Script to automatically update API routes from Gitlab CE project
139
- [] Support sudo
140
- [] Support pagination
148
+
149
+ - [] Script to automatically update API routes from Gitlab CE project
150
+ - [] Support sudo
151
+ - [/] Support pagination
152
+ - [] Support more versions of Rubies (by dropping some gem dependencies)
153
+ - [] Refactor and reduce coupling within classes
141
154
 
142
155
  ## License
143
156
 
data/lib/gitlab_ruby.rb CHANGED
@@ -5,6 +5,7 @@ require 'gitlab_ruby/api_object'
5
5
  require 'gitlab_ruby/errors'
6
6
  require 'gitlab_ruby/api_errors'
7
7
  require 'gitlab_ruby/helpers/query_chain'
8
+ require 'gitlab_ruby/paginated_response'
8
9
  require 'faraday'
9
10
  require 'json'
10
11
  require 'csv'
@@ -39,7 +39,11 @@ module GitlabRuby
39
39
  puts response.inspect if self.class.debug
40
40
  result = parse_response(response)
41
41
  if result.is_a? Array
42
- result.map { |item| APIObject.new(item) }
42
+ GitlabRuby::PaginatedResponse.new(
43
+ client: self,
44
+ array: result.map { |item| APIObject.new(item) },
45
+ headers: response.headers
46
+ )
43
47
  elsif result.is_a? Hash
44
48
  APIObject.new(result)
45
49
  end
@@ -0,0 +1,99 @@
1
+ require 'strscan'
2
+
3
+ module GitlabRuby
4
+ class PaginatedResponse
5
+ def initialize(params = {})
6
+ @client = params[:client]
7
+ @array = params[:array]
8
+ @page_links = parse_page_links_from(params[:headers])
9
+ end
10
+
11
+ def next_page?
12
+ return nil unless @page_links['next']
13
+ true
14
+ end
15
+
16
+ def prev_page?
17
+ return nil unless @page_links['prev']
18
+ true
19
+ end
20
+
21
+ def next_page
22
+ return nil unless next_page?
23
+ fetch(@page_links['next'][:href])
24
+ end
25
+
26
+ def prev_page
27
+ return nil unless prev_page?
28
+ fetch(@page_links['prev'][:href])
29
+ end
30
+
31
+ def last_page
32
+ fetch(@page_links['last'][:href])
33
+ end
34
+
35
+ def first_page
36
+ fetch(@page_links['first'][:href])
37
+ end
38
+
39
+ ## Treat this like an Array (START)
40
+ #
41
+ def ==(other)
42
+ @array == other
43
+ end
44
+
45
+ def inspect
46
+ @array.inspect
47
+ end
48
+
49
+ def method_missing(name, *args, &block)
50
+ if @array.respond_to?(name)
51
+ @array.send(name, *args, &block)
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ def respond_to_missing?(method_name, include_private = false)
58
+ super || @array.respond_to?(method_name, include_private)
59
+ end
60
+ #
61
+ ## Treat this like an Array (ENDS)
62
+
63
+ private
64
+
65
+ HREF = / *< *([^>]*) *> *;? */ #:nodoc: note: no attempt to check URI validity
66
+ TOKEN = /([^()<>@,;:\"\[\]?={}\s]+)/ #:nodoc: non-empty sequence of non-separator characters
67
+ QUOTED = /"((?:[^"\\]|\\.)*)"/ #:nodoc: double-quoted strings with backslash-escaped double quotes
68
+ ATTR = /#{TOKEN} *= *(#{TOKEN}|#{QUOTED}) */ #:nodoc:
69
+ SEMI = /; */ #:nodoc:
70
+ COMMA = /, */ #:nodoc:
71
+
72
+ def fetch(href)
73
+ query_chain = GitlabRuby::QueryChain.new(client: @client, verb: :get)
74
+ query_chain.urlstring = href
75
+ query_chain.execute
76
+ end
77
+
78
+ def parse_page_links_from(headers)
79
+ # Example {"link"=>"<https://gitlab.com/api/v4/projects?archived=false&membership=false&order_by=created_at&owned=false&page=2&per_page=20&simple=false&sort=desc&starred=false>; rel=\"next\", <https://gitlab.com/api/v4/projects?archived=false&membership=false&order_by=created_at&owned=false&page=1&per_page=20&simple=false&sort=desc&starred=false>; rel=\"first\", <https://gitlab.com/api/v4/projects?archived=false&membership=false&order_by=created_at&owned=false&page=13086&per_page=20&simple=false&sort=desc&starred=false>; rel=\"last\"", "vary"=>"Origin", "x-next-page"=>"2", "x-page"=>"1", "x-per-page"=>"20", "x-prev-page"=>"", "x-request-id"=>"eb9bd5b4-5ca9-441b-ac8e-8a6bc99d1684", "x-runtime"=>"9.262252", "x-total"=>"261707", "x-total-pages"=>"13086", "strict-transport-security"=>"max-age=31536000"}
80
+ scanner = StringScanner.new(headers['link'])
81
+ links = {}
82
+ while scanner.scan(HREF)
83
+ href = scanner[1]
84
+ attrs = []
85
+ while scanner.scan(ATTR)
86
+ attr_name = scanner[1]
87
+ token = scanner[3]
88
+ quoted = scanner[4]
89
+ attrs.push([attr_name, token || quoted.gsub(/\\"/, '"')])
90
+ puts attrs
91
+ break unless scanner.scan(SEMI)
92
+ end
93
+ links[attrs[0].last] = { href: href, attrs: attrs }
94
+ break unless scanner.scan(COMMA)
95
+ end
96
+ links
97
+ end
98
+ end
99
+ end
@@ -1,3 +1,3 @@
1
1
  module GitlabRuby
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TM Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-19 00:00:00.000000000 Z
11
+ date: 2017-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -146,6 +146,7 @@ files:
146
146
  - lib/gitlab_ruby/client.rb
147
147
  - lib/gitlab_ruby/errors.rb
148
148
  - lib/gitlab_ruby/helpers/query_chain.rb
149
+ - lib/gitlab_ruby/paginated_response.rb
149
150
  - lib/gitlab_ruby/query_chain.rb
150
151
  - lib/gitlab_ruby/routes_table_no_verb.csv
151
152
  - lib/gitlab_ruby/version.rb