cloudally 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5780ac88974b8f9bd7fe6d49e500340da172ad2565be04fc469462bcf9a13d53
4
- data.tar.gz: dc651c10d423ca8e3da8b3fc0db36678cd90a789598ed2bd402534da62d6d6c6
3
+ metadata.gz: f2f46745c7981091945d40c86bd68e6004dff4ea39ecaa436225c9d5df00a535
4
+ data.tar.gz: 2bf09895b3b8df0b1741f87b41c1f8d2a16c4b5fbb53c0047b9eaa5adcc7e399
5
5
  SHA512:
6
- metadata.gz: 6bee671a9cef8053845f521d84c1bc0f135b6d19c2936088343e0c7c6c7927a7be439be30d317ec571104788425c6bc2114a80f98fdea21b075bfd6df88e4883
7
- data.tar.gz: 5337ea5ef199d840a1febdf9eb7ce401984d90c25ecab0e67c7a9f69dd8b3d8c62b5afb59c1fdbe2b0d430e394b7ce60af050447e07ad3e76091bc4cfd0367c1
6
+ metadata.gz: '068f074ab511b4f91073553debfb77f96a81a0e7e82f1c3f02d02618be89c50217acb19fa18f964ee73270e9cb740654b426a159d60d84b846bed9eadceabc2e'
7
+ data.tar.gz: 78b7f883e82d2e0bd2e25654d74223c3409ace9bc1f64d6f374b3ca8e5b565bfece1de7bd822e3ec6d0891ec4102939358d56e91c270380288461d120dd0735a
data/CHANGELOG.md CHANGED
@@ -14,3 +14,6 @@
14
14
 
15
15
  ## [0.2.0] - 2024-01-30
16
16
  - json return for entities
17
+
18
+ ## [0.2.1] - 2024-01-30
19
+ - use wrapi gem
data/README.md CHANGED
@@ -33,8 +33,9 @@ CloudAlly.configure do |config|
33
33
  config.password = ENV["CLOUDALLY_PASSWORD"]
34
34
  config.logger = Logger.new( "./cloudally-http.log" )
35
35
  end
36
- CloudAlly.partner_login
36
+
37
37
  client = CloudAlly.client
38
+ client.partner_login
38
39
  ```
39
40
 
40
41
  ## Resources
data/cloudally.gemspec CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
30
  s.platform = Gem::Platform::RUBY
31
31
  s.add_runtime_dependency 'faraday'
32
+ s.add_runtime_dependency 'wrapi', ">= 0.1.3"
32
33
  s.add_development_dependency 'dotenv'
33
34
  s.add_development_dependency 'minitest'
34
35
  s.add_development_dependency 'rubocop'
data/lib/cloudally/api.rb CHANGED
@@ -1,17 +1,16 @@
1
- require File.expand_path('connection', __dir__)
2
- require File.expand_path('request', __dir__)
1
+ require "wrapi"
3
2
  require File.expand_path('authentication', __dir__)
4
3
 
5
4
  module CloudAlly
6
5
  # @private
7
6
  class API
8
7
  # @private
9
- attr_accessor *Configuration::VALID_OPTIONS_KEYS
8
+ attr_accessor *WrAPI::Configuration::VALID_OPTIONS_KEYS
10
9
 
11
10
  # Creates a new API
12
11
  def initialize(options = {})
13
12
  options = CloudAlly.options.merge(options)
14
- Configuration::VALID_OPTIONS_KEYS.each do |key|
13
+ WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
15
14
  send("#{key}=", options[key])
16
15
  end
17
16
  end
@@ -24,8 +23,9 @@ module CloudAlly
24
23
  conf
25
24
  end
26
25
 
27
- include Connection
28
- include Request
26
+ include WrAPI::Connection
27
+ include WrAPI::Request
28
+ include WrAPI::Authentication
29
29
  include Authentication
30
30
  end
31
31
  end
@@ -1,53 +1,29 @@
1
+
1
2
  module CloudAlly
2
3
  # Deals with authentication flow and stores it within global configuration
3
4
  module Authentication
4
5
  # Authorize to the CloudAlly portal and return access_token
5
6
  def auth(options = {})
6
- params = access_token_params.merge(options)
7
- response = post("/auth", params)
8
- # return access_token
9
- process_token(response.body)
7
+ api_auth('/auth', options)
10
8
  end
11
9
  alias login auth
12
10
 
13
11
  # Return an access token from authorization
14
12
  def auth_refresh(token)
15
- params = { refreshToken: token }
16
-
17
- response = post("/auth/refresh", params)
18
- # return access_token
19
- process_token(response.body)
13
+ api_refresh('/auth/refresh', token)
20
14
  end
21
15
 
22
16
  # Authorize to the partner portal and return access_token
23
17
  def auth_partner(options = {})
24
- params = access_token_params.merge(options)
25
- response = post("/auth/partner", params)
26
- # return access_token
27
- process_token(response.body)
18
+ api_auth('/auth/partner', options)
28
19
  end
29
20
  alias partner_login auth_partner
30
-
31
- private
32
-
33
- def access_token_params
21
+ private
22
+ def api_access_token_params
34
23
  {
35
24
  email: username,
36
25
  password: password
37
26
  }
38
27
  end
39
-
40
- def process_token(response)
41
- at = nil
42
- CloudAlly.configure do |config|
43
- at = config.access_token = response["accessToken"]
44
- config.token_type = response["tokenType"]
45
- config.refresh_token = response["refreshToken"]
46
- config.token_expires = response["expiresIn"]
47
- end
48
- raise StandardError.new 'Could not find valid accessToken; response ' + response.to_s if at == '' || at.nil?
49
-
50
- at
51
- end
52
28
  end
53
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudAlly
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/cloudally.rb CHANGED
@@ -1,27 +1,29 @@
1
- require File.expand_path('cloudally/configuration', __dir__)
1
+ require "wrapi"
2
+
2
3
  require File.expand_path('cloudally/api', __dir__)
3
4
  require File.expand_path('cloudally/client', __dir__)
4
5
  require File.expand_path('cloudally/version', __dir__)
5
6
 
6
7
  module CloudAlly
7
- extend Configuration
8
+ extend WrAPI::Configuration
9
+ extend WrAPI::RespondTo
10
+
11
+ DEFAULT_ENDPOINT = 'https://api.cloudally.com/v1/'.freeze
12
+ DEFAULT_USERAGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
8
13
 
9
14
  # Alias for CloudAlly::Client.new
10
15
  #
11
16
  # @return [CloudAlly::Client]
12
17
  def self.client(options = {})
13
- CloudAlly::Client.new(options)
14
- end
15
-
16
- # Delegate to CloudAlly::Client
17
- def self.method_missing(method, *args, &block)
18
- return super unless client.respond_to?(method)
19
-
20
- client.send(method, *args, &block)
18
+ CloudAlly::Client.new({
19
+ endpoint: DEFAULT_ENDPOINT,
20
+ user_agent: DEFAULT_USERAGENT
21
+ }.merge(options))
21
22
  end
22
-
23
- # Delegate to CloudAlly::Client
24
- def self.respond_to?(method, include_all = false)
25
- client.respond_to?(method, include_all) || super
23
+
24
+ def self.reset
25
+ super
26
+ self.endpoint = DEFAULT_ENDPOINT
27
+ self.user_agent = DEFAULT_USERAGENT
26
28
  end
27
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudally
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-31 00:00:00.000000000 Z
11
+ date: 2024-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wrapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: dotenv
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -84,9 +98,6 @@ files:
84
98
  - lib/cloudally/authentication.rb
85
99
  - lib/cloudally/client.rb
86
100
  - lib/cloudally/client/partners.rb
87
- - lib/cloudally/configuration.rb
88
- - lib/cloudally/connection.rb
89
- - lib/cloudally/request.rb
90
101
  - lib/cloudally/version.rb
91
102
  homepage: https://rubygems.org/gems/cloudally
92
103
  licenses:
@@ -1,86 +0,0 @@
1
- require_relative "./version"
2
- module CloudAlly
3
- # Defines constants and methods related to configuration
4
- module Configuration
5
- # An array of valid keys in the options hash when configuring a {CloudAlly::API}
6
-
7
- VALID_OPTIONS_KEYS = [
8
- :access_token,
9
- :token_type,
10
- :refresh_token,
11
- :token_expires,
12
- :client_id,
13
- :client_secret,
14
- :connection_options,
15
- :username,
16
- :password,
17
- :endpoint,
18
- :logger,
19
- :format,
20
- :page_size,
21
- :user_agent
22
- ].freeze
23
-
24
-
25
- # By default, don't set any connection options
26
- DEFAULT_CONNECTION_OPTIONS = {}
27
-
28
- # The endpoint that will be used to connect if none is set
29
- #
30
- # @note There is no reason to use any other endpoint at this time
31
- DEFAULT_ENDPOINT = "https://api.cloudally.com/v1/".freeze
32
-
33
- # The response format appended to the path and sent in the 'Accept' header if none is set
34
- #
35
- # @note JSON is the only available format at this time
36
- DEFAULT_FORMAT = :json
37
-
38
- # The page size for paged rest responses
39
- #
40
- # @note default JSON is the only available format at this time
41
- DEFAULT_PAGE_SIZE = 500
42
-
43
- # The user agent that will be sent to the API endpoint if none is set
44
- DEFAULT_USER_AGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
45
-
46
- # @private
47
- attr_accessor *VALID_OPTIONS_KEYS
48
-
49
- # When this module is extended, set all configuration options to their default values
50
- def self.extended(base)
51
- base.reset
52
- end
53
-
54
- # Convenience method to allow configuration options to be set in a block
55
- def configure
56
- yield self
57
- end
58
-
59
- # Create a hash of options and their values
60
- def options
61
- VALID_OPTIONS_KEYS.inject({}) do |option, key|
62
- option.merge!(key => send(key))
63
- end
64
- end
65
-
66
- # Reset all configuration options to defaults
67
- def reset
68
- self.access_token = nil
69
- self.token_type = nil
70
- self.refresh_token = nil
71
- self.token_expires = nil
72
- self.client_id = nil
73
- self.client_secret = nil
74
- self.username = nil
75
- self.password = nil
76
-
77
- self.logger = nil
78
- self.connection_options = DEFAULT_CONNECTION_OPTIONS
79
- self.endpoint = DEFAULT_ENDPOINT
80
- self.format = DEFAULT_FORMAT
81
- self.page_size = DEFAULT_PAGE_SIZE
82
- self.user_agent = DEFAULT_USER_AGENT
83
-
84
- end
85
- end
86
- end
@@ -1,42 +0,0 @@
1
- require 'faraday'
2
-
3
- module CloudAlly
4
- # @private
5
- module Connection
6
- private
7
-
8
- def connection
9
- options = {
10
- headers: {
11
- 'Accept': "application/#{format}; charset=utf-8",
12
- 'User-Agent': user_agent
13
- },
14
- url: endpoint
15
- }.merge(connection_options)
16
-
17
- Faraday::Connection.new(options) do |connection|
18
- connection.use Faraday::Response::RaiseError
19
- connection.adapter Faraday.default_adapter
20
-
21
- connection.authorization :Bearer, access_token if access_token
22
- connection.headers['client-id'] = client_id
23
- connection.headers['client-secret'] = client_secret
24
- connection.response :json, content_type: /\bjson$/
25
- connection.use Faraday::Request::UrlEncoded
26
-
27
- setup_logger_filtering(connection,logger) if logger
28
- end
29
- end
30
-
31
- def setup_logger_filtering(connection,logger)
32
- connection.response :logger, logger, { headers: true, bodies: true } do |l|
33
- # filter json content
34
- l.filter(/("password":")(.+?)(".*)/, '\1[REMOVED]\3')
35
- l.filter(/("accessToken":")(.+?)(".*)/, '\1[REMOVED]\3')
36
- # filter header content
37
- l.filter(/(client-secret\:.)([^&]+)/, '\1[REMOVED]')
38
- l.filter(/(Authorization\:.)([^&]+)/, '\1[REMOVED]')
39
- end
40
- end
41
- end
42
- end
@@ -1,126 +0,0 @@
1
- require 'uri'
2
- require 'json'
3
-
4
- module CloudAlly
5
- # Defines HTTP request methods
6
- module Request
7
- class Entity
8
- attr_reader :attributes
9
-
10
- def initialize attributes
11
- @_raw = attributes
12
- @attributes = attributes.clone.transform_keys(&:to_s)
13
- end
14
-
15
- def method_missing(method_sym, *arguments, &block)
16
- len = arguments.length
17
- if method = method_sym[/.*(?==\z)/m]
18
- # assignment
19
- if len != 1
20
- raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
21
- end
22
- @attributes[method] = arguments[0]
23
- elsif @attributes.include? method_sym.to_s
24
- r = @attributes[method_sym.to_s]
25
- case r
26
- when Hash
27
- r = @attributes[method_sym.to_s] = self.class.new(r)
28
- when Array
29
- # make deep copy
30
- @attributes[method_sym.to_s] = r = r.map { |item|
31
- self.class.new(item)
32
- } if r.length > 0 && r[0].is_a?(Hash)
33
- r
34
- else
35
- r
36
- end
37
- else
38
- super
39
- end
40
- end
41
-
42
- def respond_to?(method_sym, include_private = false)
43
- if @attributes.include? method_sym.to_s
44
- true
45
- else
46
- super
47
- end
48
- end
49
-
50
- def to_json options={}
51
- @_raw.to_json
52
- end
53
- end
54
-
55
- # Perform an HTTP GET request and return entity
56
- def get(path, options = {})
57
- response = request(:get, path, options)
58
- :json.eql?(format) ? Entity.new(response.body) : response.body
59
- end
60
-
61
- # Perform an HTTP GET request for paged date sets responsind to
62
- # Name Description
63
- # pageSize The number of records to display per page
64
- # page The page number
65
- # nextPageToken Next page token
66
- def get_paged(path, options = {}, &block)
67
- raise! ArgumentError,
68
- "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
69
- result = []
70
- page = 1
71
- total = page + 1
72
- nextPage = ""
73
- while page <= total
74
- # https://api.cloudally.com/v3/api-docs/v1
75
- followingPage = { pageSize: page_size }
76
- followingPage.merge!({ page: page, nextPageToken: nextPage }) unless nextPage.empty?
77
-
78
- response = request(:get, path, options.merge(followingPage))
79
- data = response.body
80
- d = data["data"].map { |e| Entity.new(e) }
81
- if block_given?
82
- yield(d)
83
- else
84
- result += d
85
- end
86
- page += 1
87
- total = data["totalPages"].to_i
88
- nextPage = data["nextPageToken"]
89
- end
90
- result unless block_given?
91
- end
92
-
93
- # Perform an HTTP POST request
94
- def post(path, options = {})
95
- request(:post, path, options)
96
- end
97
-
98
- # Perform an HTTP PUT request
99
- def put(path, options = {})
100
- request(:put, path, options)
101
- end
102
-
103
- # Perform an HTTP DELETE request
104
- def delete(path, options = {})
105
- request(:delete, path, options)
106
- end
107
-
108
- private
109
-
110
- # Perform an HTTP request
111
- def request(method, path, options)
112
- response = connection().send(method) do |request|
113
- uri = URI::Parser.new
114
- case method
115
- when :get, :delete
116
- request.url(uri.escape(path), options)
117
- when :post, :put
118
- request.headers['Content-Type'] = "application/#{format}"
119
- request.path = uri.escape(path)
120
- request.body = options.to_json unless options.empty?
121
- end
122
- end
123
- response
124
- end
125
- end
126
- end