Sha256: 4cad2722a48c391f893bf07aa768c3335505f5776d186d4ce6bc7471fc1b7cc5

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module RubyApiPackCloudways
  module Connection
    class CwConnect
      # Initialize Cloudways API URL and path
      attr_reader :cw_api_url_base, :cw_api_path

      # Initialize Cloudways API URL and path
      def initialize(cw_api_url_base, cw_api_path)
        @cw_api_url_base = cw_api_url_base
        @cw_api_path = cw_api_path
      end

      # GET request to Cloudways API
      def cloudways_api_connection
        token = CwToken.new.cw_api_token
        response = HTTParty.get(
          "#{@cw_api_url_base}#{@cw_api_path}",
          headers: { 'Authorization' => "Bearer #{token}" }
        )
        handle_response(response)
      end

      # POST request to Cloudways API
      def cloudways_api_post_connection(params)
        token = CwToken.new.cw_api_token
        response = HTTParty.post(
          "#{@cw_api_url_base}#{@cw_api_path}",
          headers: { 'Authorization' => "Bearer #{token}", 'Content-Type' => 'application/json' },
          body: params.to_json
        )
        handle_response(response)
      end

      private

      # Handle response from Cloudways API
      def handle_response(response)
        case response.code
        when 200
          parse_response(response)
        else
          raise "Error: Received status #{response.code}"
        end
      end

      # Parse response from Cloudways API
      def parse_response(response)
        Oj.load(response.body, mode: :strict)
      rescue Oj::ParseError => e
        raise "Error parsing response: #{e.message}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_api_pack_cloudways-0.3.0 lib/ruby_api_pack_cloudways/connection/cw_connect.rb