tp_client 0.2.4 → 0.2.6

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: 8b2f4a9b278c3d0435ba587384e32a51cf16fbd22152c20237770f880686540b
4
- data.tar.gz: 5e80eff590a3d995e059f19343cedfb7ff86fa2164d8b32122f86cdc4e2b1dd5
3
+ metadata.gz: 45d071ddd6ff993e8ccaec2877044dfaad106f5dc462dfb631081a07cb970d84
4
+ data.tar.gz: 830446aaa112228e6380c3226839e5cbf17fbdc1c31c6921ad37ea2b3e455d38
5
5
  SHA512:
6
- metadata.gz: 1c8b8ca1690c74b86f134e408b0c9a293211ee2c53a7739279e44e9a1373e074cff31393ea5bcc77cfc80832b62e187e0a5d4b831a6bd6fc300564b34d4a5675
7
- data.tar.gz: 3fbfd5cda721137b30b81767b19ccfd1599f1d8b71992afbcb0342fae98cd1b59743d66afb17c088d875e76354dc7a8137b1596d45aa52208377fe2f747014f8
6
+ metadata.gz: 73d4cfa097d479f970e191ab323d82acc5f451ab3753c0e7fb143b3f83001e97d92930218c85fce4c4bd96b681f98cb2473dd94737efd23e3e18c35422fe62dc
7
+ data.tar.gz: 71f71cd5f623af3dbae81064d268443055cc0fa34b399387c8840454a8469794282642d46fb7ab72d791b81f7c966e64664f9f3f86063d8df06a038e6394e917
data/README.md CHANGED
@@ -16,7 +16,7 @@ gem install tp_client
16
16
 
17
17
  ```ruby
18
18
  # As gem main class is different from gem name, we must require file name explicitly
19
- gem 'tp_client', '~> 0.1', require: 'tiny_client'
19
+ gem 'tp_client', '~> 0.2.6', require: 'tiny_client'
20
20
  ```
21
21
 
22
22
  Please notice, we have 2 similar gems:
@@ -34,7 +34,6 @@ You can initialize your API by extending the `TinyClient::Configuration`
34
34
 
35
35
  ```ruby
36
36
  class MyConf < TinyClient::Configuration
37
-
38
37
  def initialize
39
38
  @url = 'http://localhost:3000/api/1.0'
40
39
  @headers = { 'Authorization' => 'token asdfasf4ffsafasdf@12rfsdfa' }
@@ -197,7 +196,7 @@ toto.save! # POST { author: {} }
197
196
  Pagination, buffer is achieve through `limit` and `offset` params.
198
197
 
199
198
  ```
200
- Author.index_all({limit: 100}) # Will queries the server by batch of 100, until all authors has been retrieved through the enumerator.
199
+ Author.index_all(limit: 100) # Will queries the server by batch of 100, until all authors has been retrieved through the enumerator.
201
200
 
202
201
  ```
203
202
 
@@ -6,6 +6,7 @@ module TinyClient
6
6
  # @attr_reader [Integer] limit default limit used as a query param
7
7
  class Configuration
8
8
  include Singleton
9
+
9
10
  attr_reader :url, :limit
10
11
 
11
12
  # You need to initialize the api {#url}, default {#headers}, and default limit.
@@ -28,6 +29,18 @@ module TinyClient
28
29
  @verbose ||= false
29
30
  end
30
31
 
32
+ # @return [String] url using `TinyClient::UrlBuilder` to build url
33
+ def url_for(*args)
34
+ query = args.extract_options!
35
+ url_builder.path(*args).query(query).build
36
+ end
37
+
38
+ # @return [TinyClient::UrlBuilder] url_builder
39
+ def url_builder
40
+ TinyClient::UrlBuilder.url(url)
41
+ end
42
+
43
+ # @return [TinyClient::RemoteClient] requestor
31
44
  def requestor
32
45
  @requestor ||= TinyClient::RemoteClient.new(self)
33
46
  end
@@ -9,7 +9,7 @@ module TinyClient
9
9
  # @raise [ResponseError] if the server respond with an error status (i.e 404, 500..)
10
10
  # @return [Response]
11
11
  def get(path, params, id, name)
12
- url = build_url(path, id, name).query(params).build!
12
+ url = @config.url_for(path, id, name, params)
13
13
  CurbRequestor.perform_get(url, {
14
14
  'Accept' => 'application/json',
15
15
  'Content-Type' => 'application/x-www-form-urlencoded'
@@ -21,7 +21,7 @@ module TinyClient
21
21
  # @raise [ResponseError] if the server respond with an error status (i.e 404, 500..)
22
22
  # @return [Response]
23
23
  def post(data, path, id, name)
24
- url = build_url(path, id, name).build!
24
+ url = @config.url_for(path, id, name)
25
25
  verify_json(data)
26
26
  CurbRequestor.perform_post(url, {
27
27
  'Accept' => 'application/json',
@@ -34,7 +34,7 @@ module TinyClient
34
34
  # @raise [ResponseError] if the server respond with an error status (i.e 404, 500..)
35
35
  # @return [Response]
36
36
  def put(data, path, id, name)
37
- url = build_url(path, id, name).build!
37
+ url = @config.url_for(path, id, name)
38
38
  verify_json(data)
39
39
  CurbRequestor.perform_put(url, {
40
40
  'Accept' => 'application/json',
@@ -46,7 +46,7 @@ module TinyClient
46
46
  # @raise [ResponseError] if the server respond with an error status (i.e 404, 500..)
47
47
  # @return [Response]
48
48
  def delete(path, id, name)
49
- url = build_url(path, id, name).build!
49
+ url = @config.url_for(path, id, name)
50
50
  CurbRequestor.perform_delete(url, {
51
51
  'Accept' => 'application/json',
52
52
  'Content-Type' => 'application/x-www-form-urlencoded'
@@ -58,9 +58,5 @@ module TinyClient
58
58
  def verify_json(data)
59
59
  raise ArgumentError, 'data must respond to .to_json' unless data.respond_to? :to_json
60
60
  end
61
-
62
- def build_url(path, id, name)
63
- UrlBuilder.url(@config.url).path(path).path(id).path(name)
64
- end
65
61
  end
66
62
  end
@@ -4,14 +4,14 @@ require 'active_support/gzip'
4
4
  module TinyClient
5
5
  # Wrap the curl request response.
6
6
  class Response
7
- attr_reader :status, :body_str, :header_str, :url, :code
7
+ attr_reader :url, :body_str, :header_str, :status, :code
8
8
 
9
9
  def initialize(curb)
10
- @status = curb.status
10
+ @url = curb.url
11
11
  @body_str = curb.body_str
12
12
  @header_str = curb.header_str
13
+ @status = curb.status
13
14
  @code = @status.to_i
14
- @url = curb.url
15
15
  end
16
16
 
17
17
  # Convert the response json body into an hash.
@@ -4,14 +4,16 @@ module TinyClient
4
4
  # Convenient class used to build a request URL.
5
5
  class UrlBuilder
6
6
  SEPARATOR = '/'.freeze
7
- attr_writer :query
8
7
 
9
8
  def self.url(url)
10
9
  new(url)
11
10
  end
12
11
 
13
- def path(path)
14
- @path << fix_path(path) unless path.blank?
12
+ def path(*paths)
13
+ paths.each do |path|
14
+ new_path = fix_path(path)
15
+ @path << new_path if new_path.present?
16
+ end
15
17
  self
16
18
  end
17
19
 
@@ -20,21 +22,43 @@ module TinyClient
20
22
  self
21
23
  end
22
24
 
25
+ # @return [String] url with all paths and query params
26
+ def build
27
+ url = "#{[@url, @path].join(SEPARATOR)}.json"
28
+ url = "#{url}?#{@query.to_query}" unless @query.empty?
29
+ url
30
+ end
31
+
32
+ # @deprecated Please use {#build} instead
23
33
  def build!
24
- query_s = "?#{@query.to_query}" unless @query.empty?
25
- "#{@path.join(SEPARATOR)}.json#{query_s}"
34
+ ActiveSupport::Deprecation.warn('`build!` is deprecated. Please use `build` instead')
35
+ build
26
36
  end
27
37
 
28
38
  private
29
39
 
30
40
  def initialize(url)
31
- @path = [url]
41
+ @url = parse_url(url)
42
+ @path = []
32
43
  @query = {}
33
44
  end
34
45
 
46
+ def parse_url(url)
47
+ if url.blank? || url == SEPARATOR
48
+ ''
49
+ else
50
+ url = url[0..-2] if url.end_with?(SEPARATOR)
51
+ url
52
+ end
53
+ end
54
+
35
55
  def fix_path(path)
36
- if path.respond_to?(:gsub)
37
- path.gsub(/\.json$/, '')
56
+ case path
57
+ when String
58
+ path = path.gsub(/\.json$/, '')
59
+ path = path[1..-1] if path.start_with?('/')
60
+ path = path[0..-2] if path.end_with?('/')
61
+ path
38
62
  else
39
63
  path
40
64
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.name = 'tp_client'
4
- s.version = '0.2.4'
4
+ s.version = '0.2.6'
5
5
  s.authors = ['TINYpulse Devops']
6
6
  s.email = 'devops@tinypulse.com'
7
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - TINYpulse Devops
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curb
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  requirements: []
133
- rubygems_version: 3.0.6
133
+ rubygems_version: 3.0.3
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: TINYclient, an HTTP/JSON crud client toolkit.