Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Services resource #4

Merged
merged 9 commits into from
Feb 26, 2022
6 changes: 6 additions & 0 deletions lib/render_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ module RenderRuby

# resources
autoload :OwnerResource, 'render_ruby/resources/owners'
autoload :ServiceResource, 'render_ruby/resources/services'

# objects
autoload :Owner, 'render_ruby/objects/owner'
autoload :Service, 'render_ruby/objects/service'
autoload :EnvironmentVariable, 'render_ruby/objects/environment_variable'
autoload :Header, 'render_ruby/objects/header'
autoload :Rule, 'render_ruby/objects/rule'
autoload :Scale, 'render_ruby/objects/scale'
end
4 changes: 4 additions & 0 deletions lib/render_ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def owners
OwnerResource.new(self)
end

def services
ServiceResource.new(self)
end

def connection
@connection ||= Faraday.new(BASE_URL) do |conn|
conn.request :authorization, :Bearer, api_key
Expand Down
3 changes: 3 additions & 0 deletions lib/render_ruby/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Collection

def self.from_response(response, type:)
body = response.body

return new(data: [], total: 0, next_cursor: '', prev_cursor: '') if body.empty?

new(
data: body.map { |attrs| type.new(attrs) },
total: body.count,
Expand Down
6 changes: 6 additions & 0 deletions lib/render_ruby/objects/environment_variable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module RenderRuby
class EnvironmentVariable < Object
end
end
6 changes: 6 additions & 0 deletions lib/render_ruby/objects/header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module RenderRuby
class Header < Object
end
end
13 changes: 13 additions & 0 deletions lib/render_ruby/objects/rule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module RenderRuby
class Rule < Object
TYPES = %w[redirect rewrite].freeze

TYPES.each do |type|
define_method "#{type}?" do
type == self.type
end
end
end
end
6 changes: 6 additions & 0 deletions lib/render_ruby/objects/scale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module RenderRuby
class Scale < Object
end
end
15 changes: 15 additions & 0 deletions lib/render_ruby/objects/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module RenderRuby
class Service < Object
TYPES = %w[static_site web_service private_service background_worker cron_job].freeze

def auto_deploy_enabled?
autoDeploy.equal?('yes')
end

def suspended?
suspended.equal?('suspended')
end
end
end
19 changes: 1 addition & 18 deletions lib/render_ruby/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,10 @@ def delete_request(url, params: {}, headers: {})
handle_response client.connection.delete(url, params, headers)
end

# rubocop:disable Metrics/CyclomaticComplexity
def handle_response(response)
case response.status
when 400
raise Error, "Your request was malformed. #{response.body}"
when 401
raise Error, "You did not supply valid authentication credentials. #{response.body}"
when 403
raise Error, "You are not allowed to perform that action. #{response.body}"
when 404
raise Error, "No results were found for your request. #{response.body}"
when 429
raise Error, "Your request exceeded the API rate limit. #{response.body}"
when 500
raise Error, "We were unable to perform the request due to server-side problems. #{response.body}"
when 503
raise Error, "You have been rate limited for sending more than 30 requests per minute. #{response.body}"
end
raise Error, response.body['message'] unless [200, 201, 202, 204].include?(response.status)

response
end
# rubocop:enable Metrics/CyclomaticComplexity
end
end
63 changes: 63 additions & 0 deletions lib/render_ruby/resources/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module RenderRuby
class ServiceResource < Resource
def list(**params)
response = get_request('services', params: params)

Collection.from_response(response, type: Service)
end

def retrieve(service_id:)
Service.new get_request("services/#{service_id}").body
end

def create(**attributes)
Service.new post_request('services', body: attributes).body
end

def update(service_id:, **attributes)
Service.new patch_request("services/#{service_id}", body: attributes).body
end

def delete(service_id:)
delete_request("services/#{service_id}")
end

def suspend(service_id:)
post_request("services/#{service_id}/suspend", body: {})
end

def resume(service_id:)
post_request("services/#{service_id}/resume", body: {})
end

def retrieve_env_vars(service_id:, **params)
response = get_request("services/#{service_id}/env-vars", params: params)

Collection.from_response(response, type: EnvironmentVariable)
end

def update_env_var(service_id:, env_vars:)
response = put_request("services/#{service_id}/env-vars", body: env_vars)

Collection.from_response(response, type: EnvironmentVariable)
end

def retrieve_headers(service_id:, **params)
response = get_request("services/#{service_id}/headers", params: params)

Collection.from_response(response, type: Header)
end

def retrieve_redirect_and_rewrite_rules(service_id:, **params)
response = get_request("services/#{service_id}/routes", params: params)

Collection.from_response(response, type: Rule)
end

def scale(service_id:, num_instances:)
Scale.new post_request("services/#{service_id}/scale", body: { numInstances: num_instances }).body
end
end
end