Class: RubyApiPackCloudways::Connection::CwConnect

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_api_pack_cloudways/connection/cw_connect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cw_api_url_base, cw_api_path) ⇒ CwConnect

Initialize Cloudways API URL and path



14
15
16
17
# File 'lib/ruby_api_pack_cloudways/connection/cw_connect.rb', line 14

def initialize(cw_api_url_base, cw_api_path)
  @cw_api_url_base = cw_api_url_base
  @cw_api_path = cw_api_path
end

Instance Attribute Details

#cw_api_pathObject (readonly)

Initialize Cloudways API URL and path



11
12
13
# File 'lib/ruby_api_pack_cloudways/connection/cw_connect.rb', line 11

def cw_api_path
  @cw_api_path
end

#cw_api_url_baseObject (readonly)

Initialize Cloudways API URL and path



11
12
13
# File 'lib/ruby_api_pack_cloudways/connection/cw_connect.rb', line 11

def cw_api_url_base
  @cw_api_url_base
end

Instance Method Details

#cloudways_api_connectionObject

GET request to Cloudways API



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_api_pack_cloudways/connection/cw_connect.rb', line 20

def cloudways_api_connection
  token = CwToken.new.cw_api_token
  response = HTTParty.get(
    "#{@cw_api_url_base}#{@cw_api_path}",
    headers: { 'Authorization' => "Bearer #{token}" },
    ssl_version: :TLSv1_2,
    debug_output: $stdout
  )
  handle_response(response)
rescue RuntimeError => e
  raise unless e.message.include?('Rate limit exceeded')

  sleep(60)
  retry
end

#cloudways_api_post_connection(params) ⇒ Object

POST request to Cloudways API



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_api_pack_cloudways/connection/cw_connect.rb', line 37

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,
    ssl_version: :TLSv1_2,
    debug_output: $stdout
  )
  handle_response(response)
end