prest 0.1.7 → 0.1.8

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: abce07b88fce37bc8244e708a8b564499673c4de5391822387b5eb85219803a1
4
- data.tar.gz: 5afd7519510296c17262f61bebe61731fbba16dbfee484a7cfbbbc2a97c92ab5
3
+ metadata.gz: accb2149cb0dacc0ad3fee732fb7bff9fb552d1bbea197118b29525fed9e24fe
4
+ data.tar.gz: b6cf166232aca1d97372acc0efdb8b8e952d4025159a638d55f779ea608e2276
5
5
  SHA512:
6
- metadata.gz: efaadb1850294c22ce5cc28693017ba5302b44ec1ee944f12612cb4b55672fd96d2ef97ff30fba56c0653e8bd179c863b53aee88370110958a72eab7b7d75fee
7
- data.tar.gz: d6d1ce0b137b599c0a3964c2d13fdf8065ecd16446135402ebb1d30a65add4864a246bde27f9b1bbf5ca4af6d20af00169adb696d7094cb0049fa63bbe9df7e0
6
+ metadata.gz: a313c848c5bd26fd4210cdccfc0e6f79f07927dc59cca7417966ccac53c518b61f0d80c403cc6371c99c9453e21f8b08d1696af2774bbaba91945a7826c6ce48
7
+ data.tar.gz: 16364768bf1190a5bb884c44379873ef37841ae610d9d832b7fdc9742a13ca79601fd9442513342c8ceca4a92bfa1df27a61409488f3c6cdfe16a683a2815279
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prest (0.1.7)
4
+ prest (0.1.8)
5
5
  httparty (~> 0.20.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -54,6 +54,48 @@ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Be
54
54
  .post(body: { username: 'juan-apa' })
55
55
  ```
56
56
 
57
+ ### Using raw/custom/special query parameters
58
+
59
+ In ruby, duplicate keyword arguments on method calls are not accepted, so you **can not** do the following:
60
+
61
+ ```ruby
62
+ Prest::Client.new('https://example.com/api').example(key: 1, key: 2).get
63
+ # `key: 1` is overriden by `key: 2`;
64
+ # produces: GET https://example.com/api/one/two?key=2
65
+ ```
66
+
67
+ Because of this and other cases where formatting is very strict/unusual, you can pass a string which will not be formatted to the query parameters. To do this, use the following:
68
+
69
+ ```ruby
70
+ # GET https://example.com/api/example?key=1&key2&other=value
71
+ Prest::Client.new('https://example.com/api')
72
+ .example(__query_params: 'key=1&key=2', other: 'value')
73
+ .get
74
+ ```
75
+
76
+ The string passed to the keyword argument `__query_params` will not be formatted, and passed as is.
77
+
78
+ > **Warning**
79
+ > `__query_params` is the only keyword argument that can be repeated across method calls:
80
+
81
+ ```ruby
82
+ Prest::Client.new('https://example.com/api')
83
+ .one(key: '1')
84
+ .two(key: '2')
85
+ .get
86
+ # Produces: GET https://example.com/api/one/two?key=2
87
+ ```
88
+
89
+ However using `__query_params`:
90
+
91
+ ```ruby
92
+ Prest::Client.new('https://example.com/api')
93
+ .one(__query_params: 'key=1')
94
+ .two(__query_params: 'key=2')
95
+ .get
96
+ #Produces: GET https://example.com/api/one/two?key=1&key=2
97
+ ```
98
+
57
99
  ### Automatically adding the json headers
58
100
 
59
101
  Because some API's need a `Content-Type: application/json` and/or `Accept: application/json` headers, there's a built in option that can be passed to the client to add those for you:
data/lib/prest/client.rb CHANGED
@@ -56,7 +56,10 @@ module Prest
56
56
  def chain_fragment(fragment_name, *args, **kwargs)
57
57
  arguments = args.join('/')
58
58
  parsed_args = arguments.empty? ? '' : "/#{arguments}"
59
- @query_params.merge!(kwargs)
59
+ extract_raw_query_params!(kwargs)
60
+ dup_kwargs = kwargs.dup
61
+ dup_kwargs.delete(:__query_params)
62
+ @query_params.merge!(dup_kwargs)
60
63
  @fragments << "#{fragment_name.gsub("__", "-")}#{parsed_args}"
61
64
  end
62
65
 
@@ -69,13 +72,26 @@ module Prest
69
72
  def build_url
70
73
  path = @fragments.join('/')
71
74
 
72
- stringified_params = ''
73
- @query_params.to_a.each do |key_val|
74
- stringified_params += "#{key_val[0]}=#{key_val[1]}&"
75
- end
75
+ stringified_params = @query_params.to_a.map do |key_val|
76
+ key_val[0] == :__query_params ? key_val[1].join('&') : "#{key_val[0]}=#{key_val[1]}"
77
+ end.join('&')
76
78
 
77
- stringified_params = stringified_params.empty? ? '' : "?#{stringified_params[0..-2]}"
79
+ stringified_params = stringified_params.empty? ? '' : "?#{stringified_params}"
78
80
  "#{@base_uri}/#{path}#{stringified_params}"
79
81
  end
82
+
83
+ def array_wrap(obj)
84
+ obj.is_a?(::Array) ? obj : [obj]
85
+ end
86
+
87
+ def extract_raw_query_params!(kwargs)
88
+ return unless kwargs.key?(:__query_params)
89
+
90
+ @query_params[:__query_params] = if @query_params[:__query_params].is_a?(::Array)
91
+ @query_params[:__query_params] + array_wrap(kwargs[:__query_params])
92
+ else
93
+ array_wrap(kwargs[:__query_params])
94
+ end
95
+ end
80
96
  end
81
97
  end
data/lib/prest/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prest
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.8'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Aparicio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-16 00:00:00.000000000 Z
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty