bubbles-rest-client 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 3db39174193ea887659c8e1d859048526b0c5335
4
- data.tar.gz: 8254a56110041009e0e03c32aae4b1a1de08b914
3
+ metadata.gz: 9ddba0cef2c0e511b7352a6f0bfd2bc2add23a9f
4
+ data.tar.gz: a09dfa1bea98d9aef9e1e00afc7d5ebfb4c1dd27
5
5
  SHA512:
6
- metadata.gz: 8c0f6e722b868cf82d60f0a12e17b3ea24cc59f65ea756e93bf07d906670b49dd700a44f2f2631b0d1ef6ff264bfa0c13d1c74dfaf2e26e7944f1d3ae175924e
7
- data.tar.gz: 3211a7eda8805bdba2678c551f73dd8212d188ce371a4a3a526010b8a6507071f5ba41d41ef93a253afa37bf99b5a3f955f9098f963d0b3f83dfbc79078744a7
6
+ metadata.gz: 1518179880d79a69e0b55cc33c4e1001ed2d29c4b3ed662b2dbea46337f7f64dca3635e7243b98f129c80331c3597f7bff0a2e60bfaefd0c8ec77200b0509a84
7
+ data.tar.gz: b3f7724f3ba7a2a1e4ddc8af0a61c9692f8f6473132214d77fbbd782e19deaddfceaf55d4cbe30b1aba4e727343a6a6edb826a0f118ee0a2c69b89caad67e1ce
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bubbles-rest-client (0.1.0)
4
+ bubbles-rest-client (0.2.0)
5
5
  addressable (~> 2.5)
6
6
  rest-client (~> 2.0)
7
7
 
@@ -24,7 +24,7 @@ GEM
24
24
  json (2.0.2)
25
25
  mime-types (3.2.2)
26
26
  mime-types-data (~> 3.2015)
27
- mime-types-data (3.2018.0812)
27
+ mime-types-data (3.2019.0331)
28
28
  minitest (5.11.3)
29
29
  minitest-reporters (1.1.19)
30
30
  ansi
@@ -60,7 +60,7 @@ GEM
60
60
  simplecov-html (0.10.2)
61
61
  unf (0.1.4)
62
62
  unf_ext
63
- unf_ext (0.0.7.5)
63
+ unf_ext (0.0.7.6)
64
64
  vcr (3.0.3)
65
65
  webmock (3.0.1)
66
66
  addressable (>= 2.3.6)
@@ -72,7 +72,7 @@ PLATFORMS
72
72
 
73
73
  DEPENDENCIES
74
74
  bubbles-rest-client!
75
- bundler (~> 1.14)
75
+ bundler (~> 2.0.0)
76
76
  minitest (~> 5.0)
77
77
  minitest-reporters (~> 1.1)
78
78
  rake (~> 10.0)
@@ -82,4 +82,4 @@ DEPENDENCIES
82
82
  webmock (~> 3.0)
83
83
 
84
84
  BUNDLED WITH
85
- 1.14.3
85
+ 2.0.1
data/README.md CHANGED
@@ -32,7 +32,6 @@ Currently, bubbles has a number of limitations that make it likely not suitable
32
32
  - Some request methods (`PATCH`, `PUT`, `DELETE`) do not currently allow unauthenticated access. In other words, it is not possible to perform a `DELETE` request on your API without passing an authorization token. (#9)
33
33
  - Only three types of environment are currently allowed. Only `local`, `staging` and `production` are recognized as possible environments to connect to for the purposes of using bubbles. You can add any scheme, host, and port to any of these, but the names are currently hardcoded, as is the number of possible hosts. (#4)
34
34
  - Not all possible combinations of `has_uri_params`, `authenticated`, and `api_key_required` are tested. In some cases, such as with `GET` requests, there aren't any tests for possible configuration cases that might cause issues when combined. (#12)
35
- - Usage of the `expect_json` configuration parameter is not intuitive. You should think of this as more of a "I want an object back from the API", rather than how it's worded. (#11)
36
35
 
37
36
  If you're interested in working on any of the issues above, please feel free to submit a pull request and a member of our team will review that pull request within a couple of days.
38
37
 
@@ -164,7 +163,7 @@ Each _endpoint_ object can have the following attributes:
164
163
  | `name` | The name to give the method created to make this REST call. | No | The value of the `location` parameter, with slashes (`/`) replaced with underscores (`_`). |
165
164
  | `authorization` | Whether or not this endpoint requires authentication prior to executing the call. If true, then an `authorization_token` will be added to the method as a parameter to be passed when the method is called. This parameter will be placed in an `Authorization` header when the REST call is executed. | No | `false` |
166
165
  | `api_key_required` | Whether or not an API key is required. If `true`, a parameter will be added to the method created to execute the REST API call named `api_key`. The value of this parameter will be set as the value of the `X-Api-Key` header when making the REST API call. | No | `false` |
167
- | `expect_json` | Whether or not JSON is expected as the format of the `body` of both the Request and Response. Additionally, specifying `true` for this option will cause methods executing REST requests to return `OpenStruct` objects representing the fully-parsed JSON response (on success). See note under "Limitations", above, on using this parameter. | No | `false` |
166
+ | `return_type` | Must be one of: `[full_response, body_as_object, body_as_string]`. This specifies what type of response is expected from the `Endpoint`. A value of `full_response` will return the full `RestClient::Response` object to the client. A value of `body_as_string` will return the `RestClient::Response.body` value as a `String`. A value of `body_as_object` will take the `RestClient::Response.body` parameter and parse it as an `OpenStruct` object, and return the result of this parsing operation. | No | `body_as_string` |
168
167
  | `encode_authorization` | Whether the `data` passed as part of the request should be re-encoded as an `Authorization: Basic` header (and Base64 encoded). Typically, this is only used for initial username/password authentication. | No | `false` |
169
168
 
170
169
  ### Examples
data/bubbles.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = Bubbles::VersionInformation.package_name
8
8
  spec.version = Bubbles::VersionInformation.version_name
9
9
  spec.date = Date.today.strftime("%Y-%m-%d")
10
- spec.summary = 'FoamFactory REST Client'
10
+ spec.summary = 'Bubbles REST Client'
11
11
  spec.homepage = 'https://github.com/FoamFactory/bubbles'
12
12
  spec.authors = ['Scott Johnson']
13
13
  spec.email = 'jaywir3@gmail.com'
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
- spec.add_development_dependency "bundler", "~> 1.14"
35
+ spec.add_development_dependency "bundler", "~> 2.0.0"
36
36
  spec.add_development_dependency "rake", "~> 10.0"
37
37
  spec.add_development_dependency "minitest", "~> 5.0"
38
38
  spec.add_development_dependency "minitest-reporters", "~> 1.1"
@@ -39,106 +39,42 @@ module Bubbles
39
39
  #
40
40
  class Configuration
41
41
  def initialize
42
- @local_environment_scheme = 'http'
43
- @local_environment_host = '127.0.0.1'
44
- @local_environment_port = '1234'
45
- @local_environment_api_key = nil
46
-
47
- @staging_environment_scheme = 'http'
48
- @staging_environment_host = '127.0.0.1'
49
- @staging_environment_port = '1234'
50
- @staging_environment_api_key = nil
51
-
52
- @production_environment_scheme = 'http'
53
- @production_environment_host = '127.0.0.1'
54
- @production_environment_port = '1234'
55
- @production_environment_api_key = nil
42
+ @environment_scheme = 'http'
43
+ @environment_host = '127.0.0.1'
44
+ @environment_port = '1234'
45
+ @environment_api_key = nil
56
46
 
57
47
  @endpoints = Hash.new
58
48
  end
59
49
 
60
50
  ##
61
- # Retrieve the local {RestEnvironment} object defined as part of this Configuration.
62
- #
63
- def local_environment
64
- RestEnvironment.new(@local_environment_scheme, @local_environment_host, @local_environment_port, @local_environment_api_key)
65
- end
66
-
67
- ##
68
- # Set the local environment.
69
- #
70
- # @param [Object] env The environment, as a generic Ruby Object.
51
+ # Retrieve the {RestEnvironment} object defined as part of this Configuration.
71
52
  #
72
- # @example In app/config/initializers/bubbles.rb
73
- # Bubbles.configure do |config|
74
- # config.local_environment = {
75
- # :scheme => 'https',
76
- # :host => 'api.somehost.com',
77
- # :port => '443'
78
- # }
79
- # end
80
- #
81
- def local_environment=(env)
82
- @local_environment_scheme = env[:scheme]
83
- @local_environment_host = env[:host]
84
- @local_environment_port = env[:port]
85
- @local_environment_api_key = env[:api_key]
86
- end
87
-
88
- ##
89
- # Retrieve the staging {RestEnvironment} object defined as part of this Configuration.
53
+ # Note that this constructs a new +RestEnvironment+ and returns it, rather than returning an existing object.
90
54
  #
91
- def staging_environment
92
- RestEnvironment.new(@staging_environment_scheme, @staging_environment_host, @staging_environment_port, @staging_environment_api_key)
55
+ def environment
56
+ RestEnvironment.new(@environment_scheme, @environment_host, @environment_port, @environment_api_key)
93
57
  end
94
58
 
95
59
  ##
96
- # Set the staging environment.
60
+ # Set the current environment.
97
61
  #
98
62
  # @param [Object] env The environment, as a generic Ruby Object.
99
63
  #
100
- # @example In app/config/initializers/bubbles.rb
64
+ # @example In app/config/environments/staging.rb:
101
65
  # Bubbles.configure do |config|
102
- # config.staging_environment = {
66
+ # config.environment = {
103
67
  # :scheme => 'https',
104
- # :host => 'api.somehost.com',
68
+ # :host => 'stage.api.somehost.com',
105
69
  # :port => '443'
106
70
  # }
107
71
  # end
108
72
  #
109
- def staging_environment=(env)
110
- @staging_environment_scheme = env[:scheme]
111
- @staging_environment_host = env[:host]
112
- @staging_environment_port = env[:port]
113
- @staging_environment_api_key = env[:api_key]
114
- end
115
-
116
- ##
117
- # Retrieve the production {RestEnvironment} object defined as part of this Configuration.
118
- #
119
- def production_environment
120
- RestEnvironment.new(@production_environment_scheme, @production_environment_host, @production_environment_port, @production_environment_api_key)
121
- end
122
-
123
- ##
124
- # Set the production environment.
125
- #
126
- # @param [Object] env The environment, as a generic Ruby Object.
127
- #
128
- # @example In app/config/initializers/bubbles.rb
129
- # Bubbles.configure do |config|
130
- # config.production_environment = {
131
- # :scheme => 'https',
132
- # :host => 'api.somehost.com',
133
- # :port => '443',
134
- # }
135
- # end
136
- #
137
- def production_environment=(env)
138
- @production_environment_scheme = env[:scheme]
139
- @production_environment_host = env[:host]
140
- @production_environment_port = env[:port]
141
- @production_environment_api_key = env[:api_key]
73
+ def environment=(env)
74
+ @environment_scheme = env[:scheme]
75
+ @environment_host = env[:host]
76
+ @environment_port = env[:port]
77
+ @environment_api_key = env[:api_key]
142
78
  end
143
79
 
144
80
  ##
@@ -167,7 +103,7 @@ module Bubbles
167
103
  def endpoints=(endpoints)
168
104
  new_endpoints = Hash.new
169
105
  endpoints.each do |ep|
170
- endpoint_object = Endpoint.new ep[:method], ep[:location].to_s, ep[:authenticated], ep[:api_key_required], ep[:name], ep[:expect_json], ep[:encode_authorization]
106
+ endpoint_object = Endpoint.new ep[:method], ep[:location].to_s, ep[:authenticated], ep[:api_key_required], ep[:name], ep[:return_type], ep[:encode_authorization]
171
107
 
172
108
  new_endpoints[endpoint_object.get_key_string] = endpoint_object
173
109
  end
@@ -182,7 +118,7 @@ module Bubbles
182
118
  endpoint_name_as_sym = endpoint.get_location_string.to_sym
183
119
  end
184
120
 
185
- if Bubbles::RestEnvironment.instance_methods(false).include? (endpoint_name_as_sym)
121
+ if Bubbles::RestEnvironment.instance_methods(false).include?(endpoint_name_as_sym)
186
122
  Bubbles::RestEnvironment.class_exec do
187
123
  remove_method endpoint_name_as_sym
188
124
  end
@@ -11,7 +11,8 @@ module Bubbles
11
11
  class Endpoint
12
12
  ##
13
13
  # Controls the method used to access the endpoint. Must be one of {Endpoint::Methods}.
14
- # @return [Symbol] the method used to access the endpoint. Will always be one of the symbols defined in {Endpoint::METHODS}.
14
+ # @return [Symbol] the method used to access the endpoint. Will always be one of the symbols defined in
15
+ # {Endpoint::METHODS}.
15
16
  attr_accessor :method
16
17
 
17
18
  ##
@@ -30,9 +31,10 @@ module Bubbles
30
31
  attr_accessor :api_key_required
31
32
 
32
33
  ##
33
- # Controls whether JSON is the expected form of output from this +Endpoint+.
34
- # @return [Boolean] true, if JSON is the expected form of output from this +Endpoint+; false, otherwise.
35
- attr_accessor :expect_json
34
+ # Controls what type of object will be returned from the REST API call on success. Must be one of the symbols
35
+ # defined in {Endpoint::RETURN_TYPES}.
36
+ #
37
+ attr_accessor :return_type
36
38
 
37
39
  ##
38
40
  # Controls which data values should be encoded as part of an Authorization header. They will be separated with a
@@ -51,14 +53,17 @@ module Bubbles
51
53
  ## A template for specifying the complete URL for endpoints, with a port attached to the host.
52
54
  API_URL_WITH_PORT = ::Addressable::Template.new("{scheme}://{host}:{port}/{endpoint}")
53
55
 
54
-
55
56
  ## The HTTP methods supported by a rest client utilizing Bubbles.
56
57
  METHODS = %w[get post patch put delete head].freeze
57
58
 
59
+ ## The possible return types for successful REST calls. Defaults to :body_as_string.
60
+ RETURN_TYPES = %w[full_response body_as_string body_as_object].freeze
61
+
58
62
  ##
59
63
  # Construct a new instance of an Endpoint.
60
64
  #
61
- # @param [Symbol] method The type of the new Endpoint to create. Must be one of the methods in {Endpoint::METHODS}.
65
+ # @param [Symbol] method The type of the new Endpoint to create. Must be one of the methods in
66
+ # {Endpoint::METHODS}.
62
67
  # @param [String] location The location, relative to the root of the host, at which the endpoint resides.
63
68
  # @param [Boolean] auth_required If true, then authorization/authentication is required to access this endpoint.
64
69
  # Defaults to +false+.
@@ -66,16 +71,23 @@ module Bubbles
66
71
  # +false+.
67
72
  # @param [String] name An optional name which will be given to the method that will execute this {Endpoint} within
68
73
  # the context of a {RestClientResources} object.
69
- # @param [Boolean] expect_json Whether or not to expect a JSON response from this +Endpoint+. Defaults to +false+.
74
+ # @param [Array<Symbol>] encode_authorization Parameters that should be treated as authorization parameters and
75
+ # encoded using a Base64 encoding.
70
76
  #
71
- def initialize(method, location, auth_required = false, api_key_required = false, name = nil, expect_json = false, encode_authorization = {})
77
+ def initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, encode_authorization = {})
72
78
  @method = method
73
79
  @location = location
74
80
  @auth_required = auth_required
75
81
  @api_key_required = api_key_required
76
82
  @name = name
77
- @expect_json = expect_json
78
83
  @encode_authorization = encode_authorization
84
+
85
+ unless Endpoint::RETURN_TYPES.include? return_type.to_s
86
+ return_type = :body_as_string
87
+ end
88
+
89
+ @return_type = return_type
90
+
79
91
  @uri_params = []
80
92
 
81
93
  # Strip the leading slash from the endpoint location, if it's there
@@ -233,7 +245,24 @@ module Bubbles
233
245
  # Authorization header; false, otherwise.
234
246
  #
235
247
  def encode_authorization_header?
236
- @encode_authorization.length > 0
248
+ !@encode_authorization.nil? and @encode_authorization.length > 0
249
+ end
250
+
251
+ ##
252
+ # Retrieve the return type of this REST endpoint.
253
+ #
254
+ # This will always be one of:
255
+ #
256
+ # - +full_response+ : Indicates that the full +Response+ object should be returned so that headers and
257
+ # return code can be used.
258
+ # - +body_as_object+ : Indicates that the body of the +Response+ should be parsed as a full +OpenStruct+
259
+ # object and returned.
260
+ # - +body_as_string+ : Indicates that only the body of the +Response+ object should be returned, as a +String+.
261
+ #
262
+ # By default, if this is not specified, it will be +body_as+string+.
263
+ #
264
+ def return_type
265
+ @return_type
237
266
  end
238
267
 
239
268
  def has_uri_params?
@@ -3,16 +3,8 @@ require 'bubbles/rest_environment'
3
3
 
4
4
  module Bubbles
5
5
  class RestClientResources
6
- def local_environment
7
- Bubbles.configuration.local_environment
8
- end
9
-
10
- def staging_environment
11
- Bubbles.configuration.staging_environment
12
- end
13
-
14
- def production_environment
15
- Bubbles.configuration.production_environment
6
+ def environment
7
+ Bubbles.configuration.environment
16
8
  end
17
9
 
18
10
  ##
@@ -256,16 +248,16 @@ module Bubbles
256
248
  #
257
249
  # @return [RestEnvironment] The {RestEnvironment} corresponding to the given {Symbol}.
258
250
  #
259
- def get_environment(environment)
260
- if !environment || environment == :production
261
- return self.production_environment
262
- elsif environment == :staging
263
- return self.staging_environment
264
- end
265
-
266
-
267
- self.local_environment
268
- end
251
+ # def get_environment(environment)
252
+ # if !environment || environment == :production
253
+ # return self.production_environment
254
+ # elsif environment == :staging
255
+ # return self.staging_environment
256
+ # end
257
+ #
258
+ #
259
+ # self.local_environment
260
+ # end
269
261
 
270
262
  private
271
263
 
@@ -311,20 +303,20 @@ module Bubbles
311
303
  headers[:authorization] = 'Bearer ' + auth_token
312
304
  end
313
305
 
314
- if endpoint.expect_json
315
- headers[:accept] = :json
316
- end
306
+ headers[:accept] = :json
317
307
 
318
308
  response = block.call(env, url, data, headers)
319
309
  rescue Errno::ECONNREFUSED
320
310
  response = { :error => 'Unable to connect to host ' + env.host.to_s + ':' + env.port.to_s }.to_json
321
311
  end
322
312
 
323
- unless endpoint.expect_json or endpoint.method != :head
324
- return response
313
+ if endpoint.return_type == :body_as_object and endpoint.method != :head
314
+ return JSON.parse(response, object_class: OpenStruct)
315
+ elsif endpoint.return_type == :body_as_string and endpoint.method != :head
316
+ return response.body
325
317
  end
326
318
 
327
- JSON.parse(response, object_class: OpenStruct)
319
+ response
328
320
  end
329
321
  end
330
322
  end
@@ -1,5 +1,3 @@
1
- require 'addressable/template'
2
-
3
1
  module Bubbles
4
2
  class RestEnvironment
5
3
  attr_accessor :scheme, :host, :port, :api_key
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module Bubbles
2
4
  class VersionInformation
3
5
  def self.package_name
@@ -5,7 +7,7 @@ module Bubbles
5
7
  end
6
8
 
7
9
  def self.version_name
8
- '0.1.0'
10
+ '0.2.0'
9
11
  end
10
12
 
11
13
  def self.version_code
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bubbles-rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Johnson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-21 00:00:00.000000000 Z
11
+ date: 2019-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.14'
19
+ version: 2.0.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.14'
26
+ version: 2.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  requirements: []
194
194
  rubyforge_project:
195
- rubygems_version: 2.6.8
195
+ rubygems_version: 2.6.11
196
196
  signing_key:
197
197
  specification_version: 4
198
198
  summary: A gem for easily defining client REST interfaces in Ruby