cloudally 0.1.0 → 0.1.2

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: 7316687a69f5efc3a9644196d14ab51d8e5fe3a435fbe392d633fef1cd6c5995
4
- data.tar.gz: 83b8bc9d624b50f7d9e06899af6f44dce0f0c052a40700d64392c56dd7b56ef8
3
+ metadata.gz: 26ff0cec39018a19e5f5f42cbb64628f060a19702ade85a67a9fa760b7edf0d4
4
+ data.tar.gz: 7d02ca3eb358d908ac2dec13b6f2b449d0e1213e97255414b7401dc826343799
5
5
  SHA512:
6
- metadata.gz: aec4098aaa3dd430ff2a18f03fbb59f6d140297377899c4a350f399e0e80d6db7f619e30acda36d3515729eca23fa2cafc8ca5b9d27484ebce557f4a6997aecf
7
- data.tar.gz: 5ab2fd0b3e793e9e7ad39d64a894f79668217c2c712005da420a218608c794f9996385d9f6414eeac6ec23717ae9c769656a18079060feaab39b40162ec1c770
6
+ metadata.gz: f50593cbf3f41eaf514a54a821eeb3f94f7360de5823ca066e13725faf1c58fe35e577852987eb20a7ecf402742c91e2a794a89d016d62230c725dcd31b7c815
7
+ data.tar.gz: d5757a6b9737d8558abfdf977e90972fbd1e3b47457d1a7f99592942726947bc62a80cb608b9da4a4b19473da9d07c71e024beac8845ab3d62967bdd87013fa8
data/.env.template CHANGED
@@ -1,4 +1,4 @@
1
1
  CLOUDALLY_CLIENT_ID=<client id>
2
2
  CLOUDALLY_CLIENT_SECRET=<client secret>
3
- CLOUDALLY_USER=<uername/mail>
3
+ CLOUDALLY_USER=<username/mail>
4
4
  CLOUDALLY_PASSWORD=<password>
data/.gitignore CHANGED
@@ -41,3 +41,4 @@
41
41
 
42
42
  # Used by RuboCop. Remote config files pulled in from inherit_from directive.
43
43
  # .rubocop-https?--*
44
+ Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
- ## [Unreleased]
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-01-25
4
+ - Initial release
2
5
 
3
- ## [0.1.0] - 2024-01-25
4
-
5
- - Initial release
6
+ ## [0.1.1] - 2024-01-25
7
+ - Missing files added to gem
8
+
9
+ ## [0.1.2] - 2024-01-30
10
+ - Correct version faraday library
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cloudally.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rubocop", "~> 1.7"
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Cloudally API
2
+
3
+
4
+ This is a wrapper for the CloudAlly portal API v1. You can see the API endpoints here https://api.cloudally.com/documentation
5
+
6
+ Currently only the GET requests for the Partner Portal API are implemented.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'cloudally'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install cloudally
23
+
24
+ ## Usage
25
+
26
+ Before you start making the requests to API provide the client id and client secret and email/password using the configuration wrapping.
27
+
28
+ ```
29
+ CloudAlly.configure do |config|
30
+ config.client_id = ENV["CLOUDALLY_CLIENT_ID"]
31
+ config.client_secret = ENV["CLOUDALLY_CLIENT_SECRET"]
32
+ config.username = ENV["CLOUDALLY_USER"]
33
+ config.password = ENV["CLOUDALLY_PASSWORD"]
34
+ config.logger = Logger.new( "./cloudally-http.log" )
35
+ end
36
+ CloudAlly.partner_login
37
+ client = CloudAlly.client
38
+ ```
39
+
40
+ ## Resources
41
+ ### Authentication
42
+ ```
43
+ client.partner_login
44
+ ```
45
+ |Resource|API endpoint|Description|
46
+ |:--|:--|:--|
47
+ |.auth_partner or .partner_login|/auth/partner|Authenticate partner|
48
+ |.auth or .login|/auth|Authenticate portal user|
49
+ |.auth_refresh|/auth/refresh|Refresh authentication token|
50
+
51
+ ### Partner Portal
52
+ Endpoint for partner related requests https://api.cloudally.com/documentation#/Partner%20Portal
53
+ ```
54
+ partner = client.get_partner
55
+ puts partner.email
56
+ ```
57
+
58
+ |Resource|API endpoint|
59
+ |:--|:--|
60
+ |.partners or .get_partner|/v1/partners|
61
+ |.partner_bills|/v1/partners/bills
62
+ |.partner_status or .get_status_by_partner|/v1/partners/status|
63
+ |.partner_tasks|/v1/partners/tasks|
64
+ |.partner_resellers or .get_resellers_list |/v1/partners/resellers|
65
+ |.partner_resellers( partner_id ) or .get_reseller_by_partner_id( partner_id )|/v1/partners/resellers/{partner_id}|
66
+ |.partner_users or .get_users_by_partner|/v1/partners/users|
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/cloudally.
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+ RuboCop::RakeTask.new
14
+ task default: %i[test rubocop]
data/cloudally.gemspec CHANGED
@@ -1,37 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/cloudally/version"
3
+ require_relative 'lib/cloudally/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "cloudally"
6
+ s.name = 'cloudally'
7
7
  s.version = CloudAlly::VERSION
8
- s.authors = ["Janco Tanis"]
9
- s.email = "gems@jancology.com"
10
- s.license = "MIT"
8
+ s.authors = ['Janco Tanis']
9
+ s.email = 'gems@jancology.com'
10
+ s.license = 'MIT'
11
11
 
12
+ s.summary = 'A Ruby wrapper for the CloudAlly Partner Portal REST APIs (readonly)'
13
+ s.homepage = 'https://rubygems.org/gems/cloudally'
12
14
 
13
- s.summary = %q{A Ruby wrapper for the CloudAlly Partner Portal REST APIs (readonly)}
14
- s.homepage = "https://rubygems.org/gems/cloudally"
15
+ s.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
15
16
 
16
- s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
17
-
18
- s.metadata["homepage_uri"] = s.homepage
19
- s.metadata["source_code_uri"] = "https://github.com/jancotanis/cloudally"
17
+ s.metadata['homepage_uri'] = s.homepage
18
+ s.metadata['source_code_uri'] = 'https://github.com/jancotanis/cloudally'
20
19
 
21
20
  # Specify which files should be added to the gem when it is released.
22
21
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
22
  s.files = Dir.chdir(File.expand_path(__dir__)) do
24
23
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
24
  end
26
- s.bindir = "exe"
25
+ s.bindir = 'exe'
27
26
  s.executables = s.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
- s.require_paths = ["lib"]
27
+ s.require_paths = ['lib']
29
28
 
30
29
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
31
30
  s.platform = Gem::Platform::RUBY
32
- s.add_runtime_dependency('faraday', ['>= 0.7', '< 0.10'])
33
- s.add_development_dependency "dotenv"
34
- s.add_development_dependency "minitest"
35
- s.add_development_dependency "rubocop"
36
-
31
+ s.add_runtime_dependency 'faraday'
32
+ s.add_development_dependency 'dotenv'
33
+ s.add_development_dependency 'minitest'
34
+ s.add_development_dependency 'rubocop'
37
35
  end
data/lib/cloudally/api.rb CHANGED
@@ -1,32 +1,31 @@
1
- require File.expand_path('../connection', __FILE__)
2
- require File.expand_path('../request', __FILE__)
3
- require File.expand_path('../authentication', __FILE__)
4
-
5
-
6
- module CloudAlly
7
- # @private
8
- class API
9
- # @private
10
- attr_accessor *Configuration::VALID_OPTIONS_KEYS
11
-
12
- # Creates a new API
13
- def initialize(options={})
14
- options = CloudAlly.options.merge(options)
15
- Configuration::VALID_OPTIONS_KEYS.each do |key|
16
- send("#{key}=", options[key])
17
- end
18
- end
19
-
20
- def config
21
- conf = {}
22
- Configuration::VALID_OPTIONS_KEYS.each do |key|
23
- conf[key] = send key
24
- end
25
- conf
26
- end
27
-
28
- include Connection
29
- include Request
30
- include Authentication
31
- end
32
- end
1
+ require File.expand_path('connection', __dir__)
2
+ require File.expand_path('request', __dir__)
3
+ require File.expand_path('authentication', __dir__)
4
+
5
+ module CloudAlly
6
+ # @private
7
+ class API
8
+ # @private
9
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
10
+
11
+ # Creates a new API
12
+ def initialize(options = {})
13
+ options = CloudAlly.options.merge(options)
14
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
15
+ send("#{key}=", options[key])
16
+ end
17
+ end
18
+
19
+ def config
20
+ conf = {}
21
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
22
+ conf[key] = send key
23
+ end
24
+ conf
25
+ end
26
+
27
+ include Connection
28
+ include Request
29
+ include Authentication
30
+ end
31
+ end
@@ -1,65 +1,53 @@
1
- module CloudAlly
2
- # Defines HTTP request methods
3
- module Authentication
4
-
5
- # Return an access token from authorization
6
- def get_access_token(options={})
7
- params = access_token_params.merge(options)
8
-
9
- response = post("/auth/partner", params)
10
- res = JSON.parse( response.body )
11
-
12
- access_token = res[ "accessToken" ]
13
-
14
- raise Exception.new 'Could not find valid accessToken' if access_token == '' || access_token.nil?
15
- access_token
16
- end
17
-
18
- # Authorize to the CloudAlly portal and return access_token
19
- def auth( options={} )
20
- params = access_token_params.merge(options)
21
- response = post("/auth", params)
22
- # return access_token
23
- process_token( response.body )
24
- end
25
- alias login auth
26
-
27
- # Return an access token from authorization
28
- def auth_refresh( token )
29
- params = { refreshToken: token}
30
-
31
- response = post( "/auth/refresh", params )
32
- # return access_token
33
- process_token( response.body )
34
- end
35
-
36
- # Authorize to the partner portal and return access_token
37
- def auth_partner( options={} )
38
- params = access_token_params.merge(options)
39
- response = post("/auth/partner", params)
40
- # return access_token
41
- process_token( response.body )
42
- end
43
- alias partner_login auth_partner
44
-
45
- private
46
-
47
- def access_token_params
48
- {
49
- email: username,
50
- password: password
51
- }
52
- end
53
- def process_token( response )
54
- at = nil
55
- CloudAlly.configure do |config|
56
- at = config.access_token = response[ "accessToken" ]
57
- config.token_type = response[ "tokenType" ]
58
- config.refresh_token = response[ "refreshToken" ]
59
- config.token_expires = response[ "expiresIn" ]
60
- end
61
- raise Exception.new 'Could not find valid accessToken; response '+response.to_s if at == '' || at.nil?
62
- at
63
- end
64
- end
65
- end
1
+ module CloudAlly
2
+ # Deals with authentication flow and stores it within global configuration
3
+ module Authentication
4
+ # Authorize to the CloudAlly portal and return access_token
5
+ def auth(options = {})
6
+ params = access_token_params.merge(options)
7
+ response = post("/auth", params)
8
+ # return access_token
9
+ process_token(response.body)
10
+ end
11
+ alias login auth
12
+
13
+ # Return an access token from authorization
14
+ def auth_refresh(token)
15
+ params = { refreshToken: token }
16
+
17
+ response = post("/auth/refresh", params)
18
+ # return access_token
19
+ process_token(response.body)
20
+ end
21
+
22
+ # Authorize to the partner portal and return access_token
23
+ 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)
28
+ end
29
+ alias partner_login auth_partner
30
+
31
+ private
32
+
33
+ def access_token_params
34
+ {
35
+ email: username,
36
+ password: password
37
+ }
38
+ 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
+ end
53
+ end
@@ -1,66 +1,69 @@
1
-
2
1
  module CloudAlly
3
- class Client
4
- # Defines methods related to users
5
- module PartnerPortal
2
+ class Client
3
+ # Defines methods related to partners
4
+ module PartnerPortal
5
+ # Get CloudAlly Partner settings.
6
+ #
7
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
8
+ def partners
9
+ get("partners")
10
+ end
11
+ alias get_partner partners
12
+
13
+ # Get Partner bills.
14
+ #
15
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
16
+ def partner_bills
17
+ get_paged("partners/bills")
18
+ end
19
+
20
+ # Get Partner bills.
21
+ #
22
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
23
+ def partner_status
24
+ get_paged("partners/status")
25
+ end
26
+ alias get_status_by_partner partner_status
27
+
28
+ # Get Partner tasks.
29
+ #
30
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
31
+ def partner_tasks
32
+ get_paged("partners/tasks")
33
+ end
34
+
35
+ # Get Partner resellers.
36
+ #
37
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
38
+ def partner_resellers(partner_id = nil)
39
+ if partner_id
40
+ get_paged("partners/resellers/#{partner_id}")
41
+ else
42
+ get_paged("partners/resellers")
43
+ end
44
+ end
6
45
 
7
- # Get CloudAlly Partner settings.
8
- #
9
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
10
- def partners
11
- get( "partners" )
12
- end
13
- alias :get_partner :partners
46
+ # Get Partner resellers.
47
+ #
48
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
49
+ def get_resellers_list
50
+ partner_resellers()
51
+ end
14
52
 
15
- # Get Partner bills.
16
- #
17
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
18
- def partner_bills
19
- get_paged( "partners/bills" )
20
- end
21
- # Get Partner bills.
22
- #
23
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
24
- def partner_status
25
- get_paged( "partners/status" )
26
- end
27
- alias :get_status_by_partner :partner_status
28
- # Get Partner tasks.
29
- #
30
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
31
- def partner_tasks
32
- get_paged( "partners/tasks" )
33
- end
34
- # Get Partner resellers.
35
- #
36
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
37
- def partner_resellers( partner_id=nil )
38
- if partner_id
39
- get_paged( "partners/resellers/#{partner_id}" )
40
- else
41
- get_paged( "partners/resellers" )
42
- end
43
- end
44
- # Get Partner resellers.
45
- #
46
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
47
- def get_resellers_list
48
- partner_resellers()
49
- end
50
- # Get Partner resellers.
51
- #
52
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
53
- def get_reseller_by_partner_id partner_id
54
- partner_resellers( partner_id )
55
- end
56
- # Get Partner users.
57
- #
58
- # @see https://api.cloudally.com/documentation#/Partner%20Portal
59
- def partner_users
60
- get_paged( "partners/users" )
61
- end
62
- alias :get_users_by_partner :partner_users
53
+ # Get Partner resellers.
54
+ #
55
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
56
+ def get_reseller_by_partner_id partner_id
57
+ partner_resellers(partner_id)
58
+ end
63
59
 
64
- end
65
- end
66
- end
60
+ # Get Partner users.
61
+ #
62
+ # @see https://api.cloudally.com/documentation#/Partner%20Portal
63
+ def partner_users
64
+ get_paged("partners/users")
65
+ end
66
+ alias get_users_by_partner partner_users
67
+ end
68
+ end
69
+ end
@@ -1,11 +1,11 @@
1
- module CloudAlly
2
- # Wrapper for the CloudAlly REST API
3
- #
4
- # @note All methods have been separated into modules and follow the same grouping used in api docs
5
- # @see https://api.cloudally.com/documentation
6
- class Client < API
7
- Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
8
-
9
- include CloudAlly::Client::PartnerPortal
10
- end
11
- end
1
+ module CloudAlly
2
+ # Wrapper for the CloudAlly REST API
3
+ #
4
+ # @note All methods have been separated into modules and follow the same grouping used in api docs
5
+ # @see https://api.cloudally.com/documentation
6
+ class Client < API
7
+ Dir[File.expand_path('client/*.rb', __dir__)].each { |f| require f }
8
+
9
+ include CloudAlly::Client::PartnerPortal
10
+ end
11
+ end
@@ -1,103 +1,86 @@
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
- # By default, don't set a user access token
25
- DEFAULT_ACCESS_TOKEN = nil
26
- DEFAULT_TOKEN_TYPE = nil
27
-
28
- # By default, don't set an application ID
29
- DEFAULT_CLIENT_ID = nil
30
-
31
- # By default, don't set an application secret
32
- DEFAULT_CLIENT_SECRET = nil
33
-
34
- # By default, don't set application username
35
- DEFAULT_USERNAME = nil
36
-
37
- # By default, don't set application password
38
- DEFAULT_PASSWORD = nil
39
-
40
- # By default, don't set any connection options
41
- DEFAULT_CONNECTION_OPTIONS = {}
42
-
43
- # The endpoint that will be used to connect if none is set
44
- #
45
- # @note There is no reason to use any other endpoint at this time
46
- DEFAULT_ENDPOINT = "https://api.cloudally.com/v1/".freeze
47
-
48
- # By default, don't turn on logging
49
- DEFAULT_LOGGER = nil
50
-
51
- # The response format appended to the path and sent in the 'Accept' header if none is set
52
- #
53
- # @note JSON is the only available format at this time
54
- DEFAULT_FORMAT = :json
55
-
56
- # The page size for paged rest responses
57
- #
58
- # @note default JSON is the only available format at this time
59
- DEFAULT_PAGE_SIZE = 500
60
-
61
- # The user agent that will be sent to the API endpoint if none is set
62
- DEFAULT_USER_AGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
63
-
64
- # @private
65
- attr_accessor *VALID_OPTIONS_KEYS
66
-
67
- # When this module is extended, set all configuration options to their default values
68
- def self.extended(base)
69
- base.reset
70
- end
71
-
72
- # Convenience method to allow configuration options to be set in a block
73
- def configure
74
- yield self
75
- end
76
-
77
- # Create a hash of options and their values
78
- def options
79
- VALID_OPTIONS_KEYS.inject({}) do |option, key|
80
- option.merge!(key => send(key))
81
- end
82
- end
83
-
84
- # Reset all configuration options to defaults
85
- def reset
86
- self.access_token = DEFAULT_ACCESS_TOKEN
87
- self.token_type = DEFAULT_TOKEN_TYPE
88
- self.client_id = DEFAULT_CLIENT_ID
89
- self.client_secret = DEFAULT_CLIENT_SECRET
90
- self.username = DEFAULT_USERNAME
91
- self.password = DEFAULT_PASSWORD
92
- self.connection_options = DEFAULT_CONNECTION_OPTIONS
93
- self.endpoint = DEFAULT_ENDPOINT
94
- self.logger = DEFAULT_LOGGER
95
- self.format = DEFAULT_FORMAT
96
- self.page_size = DEFAULT_PAGE_SIZE
97
- self.user_agent = DEFAULT_USER_AGENT
98
-
99
- self.refresh_token = nil
100
- self.token_expires = nil
101
- end
102
- end
103
- end
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,45 +1,42 @@
1
- require 'faraday'
2
- #require 'faraday_middleware'
3
- #Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
4
-
5
- module CloudAlly
6
- # @private
7
- module Connection
8
- private
9
-
10
- def connection
11
- options = {
12
- :headers => {
13
- 'Accept' => "application/#{format}; charset=utf-8",
14
- 'User-Agent' => user_agent
15
- },
16
- :url => endpoint
17
- }.merge( connection_options )
18
-
19
- Faraday::Connection.new(options) do |connection|
20
- connection.use Faraday::Response::RaiseError
21
- #connection.use FaradayMiddleware::RaiseHttpException
22
- connection.adapter Faraday.default_adapter
23
-
24
- #connection.use FaradayMiddleware::InstagramOAuth2, client_id, access_token
25
- connection.authorization :Bearer, access_token if access_token
26
- connection.headers['client-id'] = client_id
27
- connection.headers['client-secret'] = client_secret
28
- connection.response :json, :content_type => /\bjson$/
29
- connection.use Faraday::Request::UrlEncoded
30
-
31
- #connection.use FaradayMiddleware::LoudLogger if loud_logger
32
- if logger
33
- connection.response :logger, logger, { headers: true, bodies: true } do |l|
34
- # filter json content
35
- l.filter(/(\"password\"\:\")(.+?)(\".*)/, '\1[REMOVED]\3')
36
- l.filter(/(\"accessToken\"\:\")(.+?)(\".*)/, '\1[REMOVED]\3')
37
- # filter header content
38
- l.filter(/(client-secret\:.)([^&]+)/, '\1[REMOVED]')
39
- l.filter(/(Authorization\:.)([^&]+)/, '\1[REMOVED]')
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
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,117 +1,121 @@
1
- require 'uri'
2
- require 'json'
3
-
4
- module CloudAlly
5
-
6
- # Defines HTTP request methods
7
- module Request
8
- class Entity
9
- attr_reader :attributes
10
- def initialize attributes
11
- @attributes = attributes.clone.transform_keys( &:to_s )
12
- end
13
-
14
- def method_missing(method_sym, *arguments, &block)
15
- len = arguments.length
16
- if method = method_sym[/.*(?==\z)/m]
17
- # assignment
18
- if len != 1
19
- raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
20
- end
21
- @attributes[method] = arguments[ 0 ]
22
- elsif @attributes.include? method_sym.to_s
23
- r = @attributes[method_sym.to_s]
24
- case r
25
- when Hash
26
- r = @attributes[method_sym.to_s] = self.class.new( r )
27
- when Array
28
- # make deep copy
29
- @attributes[method_sym.to_s] = r = r.map{ |item| self.class.new( item ) } if r.length > 0 && r[0].is_a?( Hash )
30
- r
31
- else
32
- r
33
- end
34
- else
35
- super
36
- end
37
- end
38
-
39
- def respond_to?(method_sym, include_private = false)
40
- if @attributes.include? method_sym.to_s
41
- true
42
- else
43
- super
44
- end
45
- end
46
- end
47
- # Perform an HTTP GET request and return entity
48
- def get(path, options={})
49
- response = request(:get, path, options)
50
- :json.eql?( format ) ? Entity.new( response.body ) : response.body
51
- end
52
-
53
- # Perform an HTTP GET request for paged date sets responsind to
54
- # Name Description
55
- # pageSize The number of records to display per page
56
- # page The page number
57
- # nextPageToken Next page token
58
- def get_paged( path, options={}, &block )
59
- raise! ArgumentError, "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
60
- result = []
61
- page = 1
62
- total = page + 1
63
- nextPage = ""
64
- while page <= total
65
- # https://api.cloudally.com/v3/api-docs/v1
66
- followingPage = { pageSize: page_size }
67
- followingPage.merge!( { page: page, nextPageToken: nextPage } ) unless nextPage.empty?
68
-
69
- response = request(:get, path, options.merge( followingPage ) )
70
- data = response.body
71
- d = data[ "data" ].map{ |e| Entity.new(e) }
72
- if block_given?
73
- yield( d )
74
- else
75
- result += d
76
- end
77
- page += 1
78
- total = data["totalPages"].to_i
79
- nextPage = data["nextPageToken"]
80
- end
81
- d unless block_given?
82
- end
83
-
84
- # Perform an HTTP POST request
85
- def post(path, options={})
86
- request(:post, path, options)
87
- end
88
-
89
- # Perform an HTTP PUT request
90
- def put(path, options={})
91
- request(:put, path, options)
92
- end
93
-
94
- # Perform an HTTP DELETE request
95
- def delete(path, options={})
96
- request(:delete, path, options)
97
- end
98
-
99
- private
100
-
101
- # Perform an HTTP request
102
- def request( method, path, options )
103
- response = connection().send( method ) do |request|
104
- uri = URI::Parser.new
105
- case method
106
- when :get, :delete
107
- request.url( uri.escape( path ), options )
108
- when :post, :put
109
- request.headers['Content-Type'] = "application/#{format}"
110
- request.path = uri.escape( path )
111
- request.body = options.to_json unless options.empty?
112
- end
113
- end
114
- response
115
- end
116
- end
117
- end
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
+ @attributes = attributes.clone.transform_keys(&:to_s)
12
+ end
13
+
14
+ def method_missing(method_sym, *arguments, &block)
15
+ len = arguments.length
16
+ if method = method_sym[/.*(?==\z)/m]
17
+ # assignment
18
+ if len != 1
19
+ raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
20
+ end
21
+ @attributes[method] = arguments[0]
22
+ elsif @attributes.include? method_sym.to_s
23
+ r = @attributes[method_sym.to_s]
24
+ case r
25
+ when Hash
26
+ r = @attributes[method_sym.to_s] = self.class.new(r)
27
+ when Array
28
+ # make deep copy
29
+ @attributes[method_sym.to_s] = r = r.map { |item|
30
+ self.class.new(item)
31
+ } if r.length > 0 && r[0].is_a?(Hash)
32
+ r
33
+ else
34
+ r
35
+ end
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def respond_to?(method_sym, include_private = false)
42
+ if @attributes.include? method_sym.to_s
43
+ true
44
+ else
45
+ super
46
+ end
47
+ end
48
+ end
49
+
50
+ # Perform an HTTP GET request and return entity
51
+ def get(path, options = {})
52
+ response = request(:get, path, options)
53
+ :json.eql?(format) ? Entity.new(response.body) : response.body
54
+ end
55
+
56
+ # Perform an HTTP GET request for paged date sets responsind to
57
+ # Name Description
58
+ # pageSize The number of records to display per page
59
+ # page The page number
60
+ # nextPageToken Next page token
61
+ def get_paged(path, options = {}, &block)
62
+ raise! ArgumentError,
63
+ "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
64
+ result = []
65
+ page = 1
66
+ total = page + 1
67
+ nextPage = ""
68
+ while page <= total
69
+ # https://api.cloudally.com/v3/api-docs/v1
70
+ followingPage = { pageSize: page_size }
71
+ followingPage.merge!({ page: page, nextPageToken: nextPage }) unless nextPage.empty?
72
+
73
+ response = request(:get, path, options.merge(followingPage))
74
+ data = response.body
75
+ d = data["data"].map { |e| Entity.new(e) }
76
+ if block_given?
77
+ yield(d)
78
+ else
79
+ result += d
80
+ end
81
+ page += 1
82
+ total = data["totalPages"].to_i
83
+ nextPage = data["nextPageToken"]
84
+ end
85
+ d unless block_given?
86
+ end
87
+
88
+ # Perform an HTTP POST request
89
+ def post(path, options = {})
90
+ request(:post, path, options)
91
+ end
92
+
93
+ # Perform an HTTP PUT request
94
+ def put(path, options = {})
95
+ request(:put, path, options)
96
+ end
97
+
98
+ # Perform an HTTP DELETE request
99
+ def delete(path, options = {})
100
+ request(:delete, path, options)
101
+ end
102
+
103
+ private
104
+
105
+ # Perform an HTTP request
106
+ def request(method, path, options)
107
+ response = connection().send(method) do |request|
108
+ uri = URI::Parser.new
109
+ case method
110
+ when :get, :delete
111
+ request.url(uri.escape(path), options)
112
+ when :post, :put
113
+ request.headers['Content-Type'] = "application/#{format}"
114
+ request.path = uri.escape(path)
115
+ request.body = options.to_json unless options.empty?
116
+ end
117
+ end
118
+ response
119
+ end
120
+ end
121
+ end
@@ -1,4 +1,5 @@
1
- # frozen_string_literal: true
2
- module CloudAlly
3
- VERSION = '0.1.0'
4
- end
1
+ # frozen_string_literal: true
2
+
3
+ module CloudAlly
4
+ VERSION = '0.1.2'
5
+ end
data/lib/cloudally.rb CHANGED
@@ -1,26 +1,27 @@
1
- require File.expand_path('../cloudally/configuration', __FILE__)
2
- require File.expand_path('../cloudally/api', __FILE__)
3
- require File.expand_path('../cloudally/client', __FILE__)
4
- require File.expand_path('../cloudally/version', __FILE__)
5
-
6
- module CloudAlly
7
- extend Configuration
8
-
9
- # Alias for CloudAlly::Client.new
10
- #
11
- # @return [CloudAlly::Client]
12
- 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
- client.send(method, *args, &block)
20
- end
21
-
22
- # Delegate to CloudAlly::Client
23
- def self.respond_to?(method, include_all=false)
24
- return client.respond_to?(method, include_all) || super
25
- end
26
- end
1
+ require File.expand_path('cloudally/configuration', __dir__)
2
+ require File.expand_path('cloudally/api', __dir__)
3
+ require File.expand_path('cloudally/client', __dir__)
4
+ require File.expand_path('cloudally/version', __dir__)
5
+
6
+ module CloudAlly
7
+ extend Configuration
8
+
9
+ # Alias for CloudAlly::Client.new
10
+ #
11
+ # @return [CloudAlly::Client]
12
+ 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)
21
+ end
22
+
23
+ # Delegate to CloudAlly::Client
24
+ def self.respond_to?(method, include_all = false)
25
+ client.respond_to?(method, include_all) || super
26
+ end
27
+ 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.1.0
4
+ version: 0.1.2
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-29 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.7'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '0.10'
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '0.7'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '0.10'
26
+ version: '0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: dotenv
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +75,9 @@ files:
81
75
  - ".env.template"
82
76
  - ".gitignore"
83
77
  - CHANGELOG.md
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
84
81
  - cloudally.gemspec
85
82
  - lib/cloudally.rb
86
83
  - lib/cloudally/api.rb