ruby_api_pack_cloudways 0.1.0.pre.4 → 0.1.0.pre.5

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: 0751f30764c25914fc3e6b08872881efe1e67c34dbbb88f2f578862d94206007
4
- data.tar.gz: fc3593e06a5172ef3c3cf50e660e1377420a5eb061de25ef090830162e49f4bc
3
+ metadata.gz: 7b502c4da2d913ded7dcfca1bc55ec230870752a73f5a80b5a2d519101da7a1a
4
+ data.tar.gz: eef95f114ac2467f27174534007f4f04804ca47c82c47496bbb327368eb63fb6
5
5
  SHA512:
6
- metadata.gz: 744c1519f26a70691f11028171db1af66ba7e03931dd29a1a8383ddd53e8ce2116365c365475c2ef988d2b9badf121b25982b012e27e4607a3c3d4b996c7c48d
7
- data.tar.gz: bc87eae838b78b2f0f6f3bf5c1ead4ab387a8422d0db3b9282d776449cf416d585af0fe2cd33597931be32fd98034b8af84d6f792544cb63e19bf8147e2babbb
6
+ metadata.gz: 6d8c0706ff2b775a0172fa13fe6d9d836f4a3d57354c47e6c0b7e1f50f66a2c57245bab98563d4e33a9beb9f68fa018a86c89bc386858779f1be8d9699e83770
7
+ data.tar.gz: 0c40f6a4bf8f8fa2bbdaf23fa5b104cac84450cbd280b0db3d86780a41c860135a95b8199dbe6f2a3f15e6fe8877c0c260c3ec4a5ff7e774b5002ed35f3b1fb9
data/README.md CHANGED
@@ -1,35 +1,61 @@
1
- ### Ruby API Wrapper - Cloudways
2
-
3
- Easily connect to your Cloudways account through their API.
4
-
5
- * Secure OAuth Cloudways API Authentication
6
-
7
- #### Step 1 - Add to your application
8
-
9
- gem 'ruby_api_pack_cloudways'
10
- bundle install
11
-
12
- #### Controller Example
13
-
14
- # Load Gems & Files
15
- require "ruby_api_pack_cloudways"
16
-
17
- def index
18
- @provider_list = RubyApiPackCloudways::Api::CwLists.cw_provider_list
19
- end
20
-
21
- #### Example on Index File
22
-
23
- <% @provider_list.each do |provider| %>
24
- <%= provider["name"] %>
25
- <% end %>
26
-
27
- #### Variables
28
-
29
- Set the following environment variables in your application:
30
-
31
- export PHCDEV_API_CLOUDWAYS_EMAIL=your_cloudways_email_login
32
- export PHCDEV_API_CLOUDWAYS_KEY=api_key_provided_by_cloudways
33
-
34
- These variables should be configured with your Cloudways email login and API key provided by Cloudways.
35
-
1
+ ### Ruby API Wrapper - Cloudways
2
+
3
+ Easily connect to your Cloudways account through their API.
4
+
5
+ * Secure OAuth Cloudways API Authentication
6
+
7
+ #### Step 1 - Add to your application
8
+
9
+ gem 'ruby_api_pack_cloudways'
10
+ bundle install
11
+
12
+ #### Controller Example
13
+
14
+ def index
15
+ @servers = RubyApiPackCloudways::Api::CwServer.cw_server_list
16
+ end
17
+
18
+ #### Example on Index File
19
+
20
+ <% @servers.each do |server| %>
21
+ <p><%= server %></p>
22
+ <% end %>
23
+
24
+ #### Intializer
25
+
26
+ Set the following initalizer in your application:
27
+
28
+ RubyApiPackCloudways.configure do |config|
29
+ config.api_url = Rails.application.credentials.dig(:cloudways, :api_url)
30
+ config.api_path_token = Rails.application.credentials.dig(:cloudways, :api_path_token)
31
+ config.api_email = Rails.application.credentials.dig(:cloudways, :api_email)
32
+ config.api_key = Rails.application.credentials.dig(:cloudways, :api_key)
33
+ end
34
+
35
+ Open your credentials.yml.enc with your editor of choice in this case using VSCode:
36
+
37
+ EDITOR="code --wait" bin/rails credentials:edit
38
+
39
+ Add the following to your credentials.yml:
40
+
41
+ cloudways:
42
+ api_url: 'https://api.cloudways.com/api/v1'
43
+ api_path_token: '/oauth/access_token'
44
+ api_email: 'Your Cloudways Email'
45
+ api_key: 'Your Cloudways API Key'
46
+
47
+ Some examples using tour editor of choice to open your credentials.yml.enc:
48
+
49
+ EDITOR="atom --wait" bin/rails credentials:edit
50
+
51
+ EDITOR="gedit --wait" bin/rails credentials:edit
52
+
53
+ EDITOR="vim" bin/rails credentials:edit
54
+
55
+ EDITOR="nano" bin/rails credentials:edit
56
+
57
+ EDITOR="emacs" bin/rails credentials:edit
58
+
59
+ EDITOR="subl -n -w" bin/rails credentials:edit
60
+
61
+ EDITOR="mate -w" bin/rails credentials:edit
@@ -1,36 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../constants'
4
-
5
3
  module RubyApiPackCloudways
6
4
  module Api
7
5
  class CwLists
8
- # PHCDEVONE - Include the RubyApiPackCloudways::Constants module
9
- include RubyApiPackCloudways::Constants
10
-
11
- # PHCDEVONE - Fetch the list of providers from the Cloudways API
6
+ # Fetch the list of providers from the Cloudways API
12
7
  def self.cw_provider_list
13
8
  fetch_list('/providers')['providers']
14
9
  end
15
10
 
16
- # PHCDEVONE - Fetch the list of server sizes from the Cloudways API
11
+ # Fetch the list of server sizes from the Cloudways API
17
12
  def self.cw_server_size_list
18
13
  fetch_list('/server_sizes')['sizes']
19
14
  end
20
15
 
21
- # PHCDEVONE - Fetch the list of apps from the Cloudways API
16
+ # Fetch the list of apps from the Cloudways API
22
17
  def self.cw_app_list
23
18
  fetch_list('/apps')['apps']
24
19
  end
25
20
 
26
- # PHCDEVONE - Fetch the list of packages from the Cloudways API
21
+ # Fetch the list of packages from the Cloudways API
27
22
  def self.cw_package_list
28
23
  fetch_list('/packages')['packages']
29
24
  end
30
25
 
31
- # PHCDEVONE - Fetch the list from the Cloudways API based on the given endpoint
26
+ # Fetch the list from the Cloudways API based on the given endpoint
32
27
  def self.fetch_list(endpoint)
33
- Connection::CwConnect.new(CW_API_URL, endpoint).cloudways_api_connection
28
+ Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, endpoint).cloudways_api_connection
34
29
  end
35
30
  end
36
31
  end
@@ -1,17 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../constants'
4
-
5
3
  module RubyApiPackCloudways
6
4
  module Api
7
5
  class CwServer
8
- # PHCDEVONE - Include the RubyApiPackCloudways::Constants module
9
- include RubyApiPackCloudways::Constants
10
-
11
- # PHCDEVONE - Fetch the list of servers from the Cloudways API
6
+ # Fetch the list of servers from the Cloudways API
12
7
  def self.cw_server_list
13
- servers = Connection::CwConnect.new(CW_API_URL, '/server').cloudways_api_connection
14
- servers['servers']
8
+ fetch_list('/server')['servers']
9
+ end
10
+
11
+ def self.fetch_list(endpoint)
12
+ Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, endpoint).cloudways_api_connection
15
13
  end
16
14
  end
17
15
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyApiPackCloudways
4
+ class Configuration
5
+ attr_accessor :api_url, :api_path_token, :api_email, :api_key
6
+
7
+ def initialize
8
+ @api_url = 'https://api.cloudways.com/api/v1'
9
+ @api_path_token = '/oauth/access_token'
10
+ @api_email = nil
11
+ @api_key = nil
12
+ end
13
+ end
14
+ end
@@ -1,57 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../constants'
3
+ require 'httparty'
4
4
 
5
5
  module RubyApiPackCloudways
6
6
  module Connection
7
7
  class CwConnect
8
- # PHCDEVONE - Include the RubyApiPackCloudways::Constants module
9
- include RubyApiPackCloudways::Constants
8
+ attr_reader :cw_api_url_base, :cw_api_path
10
9
 
11
- # PHCDEVONE - Define attribute readers for the base URL, path, and Faraday connection
12
- attr_reader :cw_api_url_base, :cw_api_path, :faraday_connection
13
-
14
- # PHCDEVONE - Initialize the connection with the given URL, path, and optional Faraday connection
15
- def initialize(cw_api_url_base, cw_api_path, faraday_connection = nil)
10
+ def initialize(cw_api_url_base, cw_api_path)
16
11
  @cw_api_url_base = cw_api_url_base
17
12
  @cw_api_path = cw_api_path
18
- @faraday_connection = faraday_connection || Faraday
19
13
  end
20
14
 
21
- # PHCDEVONE - Establish a connection to the Cloudways API and handle the response
22
15
  def cloudways_api_connection
23
- token = fetch_token
24
- response = create_connection(token).get(@cw_api_path)
16
+ token = CwToken.new.cw_api_token
17
+ response = HTTParty.get(
18
+ "#{@cw_api_url_base}#{@cw_api_path}",
19
+ headers: { 'Authorization' => "Bearer #{token}" }
20
+ )
25
21
  handle_response(response)
26
22
  end
27
23
 
28
24
  private
29
25
 
30
- # PHCDEVONE - Fetch the API token using the CwToken class
31
- def fetch_token
32
- CwToken.new(CW_API_URL, CW_API_PATH_TOKEN, CW_API_EMAIL, CW_API_KEY).cw_api_token
33
- end
34
-
35
- # PHCDEVONE - Create a Faraday connection with the given token
36
- def create_connection(token)
37
- faraday_connection.new(url: @cw_api_url_base) do |conn|
38
- conn.request :oauth2, token, token_type: :bearer
39
- conn.response :logger
40
- conn.adapter Faraday.default_adapter
41
- end
42
- end
43
-
44
- # PHCDEVONE - Handle the response from the API call
45
26
  def handle_response(response)
46
- case response.status
27
+ case response.code
47
28
  when 200
48
29
  parse_response(response)
49
30
  else
50
- raise "Error: Received status #{response.status}"
31
+ raise "Error: Received status #{response.code}"
51
32
  end
52
33
  end
53
34
 
54
- # PHCDEVONE - Parse the API response using Oj
55
35
  def parse_response(response)
56
36
  Oj.load(response.body)
57
37
  rescue Oj::ParseError => e
@@ -1,44 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../constants'
3
+ require 'httparty'
4
4
 
5
5
  module RubyApiPackCloudways
6
6
  module Connection
7
7
  class CwToken
8
- # PHCDEVONE - Define attribute readers for API URL base, auth path, user email, user key, and Faraday connection
9
- attr_reader :cw_api_url_base, :cw_url_path_auth, :cw_user_email, :cw_user_key, :faraday_connection
8
+ attr_reader :cw_api_url_base, :cw_url_path_auth, :cw_user_email, :cw_user_key
10
9
 
11
- # PHCDEVONE - Initialize the token connection with the given parameters and optional Faraday connection
12
- def initialize(cw_api_url_base, cw_url_path_auth, cw_user_email, cw_user_key, faraday_connection = Faraday)
13
- @cw_api_url_base = cw_api_url_base
14
- @cw_url_path_auth = cw_url_path_auth
15
- @cw_user_email = cw_user_email
16
- @cw_user_key = cw_user_key
17
- @faraday_connection = faraday_connection
10
+ def initialize
11
+ @cw_api_url_base = RubyApiPackCloudways.configuration.api_url
12
+ @cw_url_path_auth = RubyApiPackCloudways.configuration.api_path_token
13
+ @cw_user_email = RubyApiPackCloudways.configuration.api_email
14
+ @cw_user_key = RubyApiPackCloudways.configuration.api_key
18
15
  end
19
16
 
20
- # PHCDEVONE - Create a Faraday connection for the API token
21
- def cw_api_token_connection
22
- faraday_connection.new(url: "#{@cw_api_url_base}#{@cw_url_path_auth}") do |conn|
23
- conn.request :url_encoded
24
- conn.response :logger
25
- conn.adapter Faraday.default_adapter
26
- end
27
- end
28
-
29
- # PHCDEVONE - Retrieve the API token from the Cloudways API
30
17
  def cw_api_token
31
- response = cw_api_token_connection.post do |req|
32
- req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
33
- req.body = { email: @cw_user_email, api_key: @cw_user_key }
34
- end
35
-
18
+ response = HTTParty.post(
19
+ "#{@cw_api_url_base}#{@cw_url_path_auth}",
20
+ headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
21
+ body: { email: @cw_user_email, api_key: @cw_user_key }
22
+ )
36
23
  parse_response(response)['access_token']
37
24
  end
38
25
 
39
26
  private
40
27
 
41
- # PHCDEVONE - Parse the response from the API token request
42
28
  def parse_response(response)
43
29
  Oj.load(response.body)
44
30
  rescue Oj::ParseError => e
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyApiPackCloudways
4
- VERSION = '0.1.0.pre.4'
4
+ VERSION = '0.1.0.pre.5'
5
5
  end
@@ -1,9 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ruby_api_pack_cloudways/version'
4
- require 'ruby_api_pack_cloudways/cw_client'
3
+ require 'faraday'
4
+ require 'oj'
5
+
6
+ # Load Configuration and API Files
7
+ require_relative 'ruby_api_pack_cloudways/configuration'
8
+ require_relative 'ruby_api_pack_cloudways/connection/cw_token'
9
+ require_relative 'ruby_api_pack_cloudways/connection/cw_connect'
10
+ require_relative 'ruby_api_pack_cloudways/api/cw_lists'
11
+ require_relative 'ruby_api_pack_cloudways/api/cw_server'
5
12
 
6
13
  module RubyApiPackCloudways
7
- # PHCDEVONE - Define a custom error class for the RubyApiPackCloudways module
8
- class Error < StandardError; end
14
+ class << self
15
+ attr_accessor :configuration
16
+
17
+ def configure
18
+ self.configuration ||= Configuration.new
19
+ yield(configuration)
20
+ end
21
+ end
9
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_api_pack_cloudways
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.4
4
+ version: 0.1.0.pre.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - PHCDevworks
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-07-05 00:00:00.000000000 Z
12
+ date: 2024-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -66,10 +66,9 @@ files:
66
66
  - lib/ruby_api_pack_cloudways.rb
67
67
  - lib/ruby_api_pack_cloudways/api/cw_lists.rb
68
68
  - lib/ruby_api_pack_cloudways/api/cw_server.rb
69
+ - lib/ruby_api_pack_cloudways/configuration.rb
69
70
  - lib/ruby_api_pack_cloudways/connection/cw_connect.rb
70
71
  - lib/ruby_api_pack_cloudways/connection/cw_token.rb
71
- - lib/ruby_api_pack_cloudways/constants.rb
72
- - lib/ruby_api_pack_cloudways/cw_client.rb
73
72
  - lib/ruby_api_pack_cloudways/version.rb
74
73
  homepage: https://phcdevworks.com/
75
74
  licenses:
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyApiPackCloudways
4
- module Constants
5
- # PHCDEVONE - Define the base URL for the Cloudways API
6
- CW_API_URL = 'https://api.cloudways.com/api/v1'
7
-
8
- # PHCDEVONE - Define the authentication path for the Cloudways API
9
- CW_API_PATH_TOKEN = '/oauth/access_token'
10
-
11
- # PHCDEVONE - Retrieve the Cloudways API email from environment variables
12
- CW_API_EMAIL = ENV['PHCDEV_API_CLOUDWAYS_EMAIL']
13
-
14
- # PHCDEVONE - Retrieve the Cloudways API key from environment variables
15
- CW_API_KEY = ENV['PHCDEV_API_CLOUDWAYS_KEY']
16
- end
17
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'faraday'
4
- require 'oj'
5
-
6
- # PHCDEVONE - Load API Files
7
- require_relative 'connection/cw_token'
8
- require_relative 'connection/cw_connect'
9
- require_relative 'api/cw_lists'
10
- require_relative 'api/cw_server'
11
- require_relative 'constants'
12
-
13
- module RubyApiPackCloudways
14
- class CwClient
15
- # PHCDEVONE - Include the RubyApiPackCloudways::Constants module
16
- include RubyApiPackCloudways::Constants
17
- end
18
- end