Skip to content

Commit 281ef8f

Browse files
authored
Ruby 3 upgrade (#14)
* feat: upgrades deps and bumps gem version * chore: remove pry * chore: remove some comments * fix: adds all platforms to Gemfile * chore: updates readme * chore: add Gemfile.lock to .gitignore
1 parent 40af7d6 commit 281ef8f

File tree

10 files changed

+62
-79
lines changed

10 files changed

+62
-79
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
examples
22
*.gem
3+
.ruby-version
4+
.idea
35
Deployfile
6+
Gemfile.lock

Gemfile.lock

Lines changed: 0 additions & 31 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
## Installation
44

5-
You'll need Ruby installed on your system. We've tested on 2.2.3, but other
6-
versions may work fine.
5+
You'll need Ruby installed on your system. We've tested on `2.7.8` and later.
76

87
```
98
gem install deployhq

deployhq.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Gem::Specification.new do |s|
1616
s.bindir = "bin"
1717
s.executables << "deployhq"
1818

19-
s.add_dependency('json', '~> 1.8')
20-
s.add_dependency('highline', '~> 2.0')
19+
s.add_dependency('faraday', '~> 2.7')
20+
s.add_dependency('json', '~> 2.6')
21+
s.add_dependency('highline', '~> 2.1')
2122
s.add_dependency('websocket-eventmachine-client', '~> 1.2')
2223

2324
s.author = "Dan Wentworth"

lib/deploy/cli.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def invoke(args)
6969
exit 1
7070
end
7171

72-
@project = Deploy::Project.find(project_permalink)
72+
begin
73+
@project = Deploy::Project.find(project_permalink)
74+
rescue Deploy::Errors::TimeoutError => e
75+
STDERR.puts e
76+
exit 1
77+
end
7378
end
7479

7580
case command
@@ -135,7 +140,7 @@ def configure
135140
}
136141

137142
confirmation = true
138-
if File.exists?(@options.config_file)
143+
if File.exist?(@options.config_file)
139144
confirmation = agree("File already exists at #{@options.config_file}. Overwrite? ")
140145
end
141146

lib/deploy/errors.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ class ServiceUnavailable < Error; end
1313
## Access was denied to the remote service
1414
class AccessDenied < Error; end
1515

16-
## A communication error occured while talking to the Deploy API
16+
## A communication error occurred while talking to the Deploy API
1717
class CommunicationError < Error; end
1818

19+
# A timeout error
20+
class TimeoutError < Error; end
21+
1922
# Raised from the websocket client when we receive an error event
2023
class WebsocketError < Error; end
2124
end

lib/deploy/request.rb

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'faraday'
2+
require 'json'
3+
14
module Deploy
25
class Request
36

@@ -20,47 +23,41 @@ def output
2023
## Make a request to the Deploy API using net/http. Data passed can be a hash or a string
2124
## Hashes will be converted to JSON before being sent to the remote service.
2225
def make
23-
uri = URI.parse([Deploy.configuration.account, @path].join('/'))
24-
http_request = http_class.new(uri.request_uri)
25-
http_request.basic_auth(Deploy.configuration.username, Deploy.configuration.api_key)
26-
http_request["Accept"] = "application/json"
27-
http_request["Content-type"] = "application/json"
26+
uri = URI.parse(Deploy.configuration.account)
27+
connection_url = "#{uri.scheme}://#{uri.host}:#{uri.port}"
2828

29-
http = Net::HTTP.new(uri.host, uri.port)
30-
if uri.scheme == 'https'
31-
http.use_ssl = true
29+
connection = Faraday.new(url: connection_url, request: { timeout: 3 }) do |client|
30+
client.request :authorization, :basic, Deploy.configuration.username, Deploy.configuration.api_key
31+
client.headers['Accept'] = 'application/json'
32+
client.headers['Content-Type'] = 'application/json'
3233
end
3334

3435
data = self.data.to_json if self.data.is_a?(Hash) && self.data.respond_to?(:to_json)
35-
http_result = http.request(http_request, data)
36-
@output = http_result.body
37-
@success = case http_result
38-
when Net::HTTPSuccess
39-
true
40-
when Net::HTTPServiceUnavailable
41-
raise Deploy::Errors::ServiceUnavailable
42-
when Net::HTTPForbidden, Net::HTTPUnauthorized
43-
raise Deploy::Errors::AccessDenied, "Access Denied for '#{Deploy.configuration.username}'"
44-
when Net::HTTPNotFound
45-
raise Deploy::Errors::CommunicationError, "Not Found at #{uri.to_s}"
46-
when Net::HTTPClientError
47-
false
48-
else
49-
raise Deploy::Errors::CommunicationError, http_result.body
36+
37+
response = connection.send(@method) do |req|
38+
req.url @path
39+
req.body = data
5040
end
51-
self
52-
end
5341

54-
private
42+
@output = response.body
43+
@success = case response.status
44+
when 200..299
45+
true
46+
when 503
47+
raise Deploy::Errors::ServiceUnavailable
48+
when 401, 403
49+
raise Deploy::Errors::AccessDenied, "Access Denied for '#{Deploy.configuration.username}'"
50+
when 404
51+
raise Deploy::Errors::CommunicationError, "Not Found at #{uri.to_s}"
52+
when 400..499
53+
false
54+
else
55+
raise Deploy::Errors::CommunicationError, response.body
56+
end
5557

56-
def http_class
57-
case @method
58-
when :post then Net::HTTP::Post
59-
when :put then Net::HTTP::Put
60-
when :delete then Net::HTTP::Delete
61-
else
62-
Net::HTTP::Get
63-
end
58+
self
59+
rescue Faraday::TimeoutError
60+
raise Deploy::Errors::TimeoutError, "Your request timed out, please try again in a few seconds"
6461
end
6562

6663
end

lib/deploy/resource.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def find(type, params = {})
3030

3131
## Find all objects and return an array of objects with the attributes set.
3232
def find_all(params)
33-
output = JSON.parse(Request.new(collection_path(params)).make.output)
33+
request = Request.new(collection_path(params))
34+
output = request.make.output
35+
output = JSON.parse(output)
36+
3437
if output.is_a?(Hash) && output['records'] && output['pagination']
3538
output = output['records']
3639
end
@@ -42,9 +45,12 @@ def find_all(params)
4245

4346
## Find a single object and return an object for it.
4447
def find_single(id, params = {})
45-
o = JSON.parse(Request.new(member_path(id, params)).make.output)
46-
if o.is_a?(Hash)
47-
create_object(o, params)
48+
request = Request.new(member_path(id, params))
49+
output = request.make.output
50+
output = JSON.parse(output)
51+
52+
if output.is_a?(Hash)
53+
create_object(output, params)
4854
else
4955
raise Deploy::Errors::NotFound, "Record not found"
5056
end

lib/deploy/resources/project.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def latest_revision(branch = '')
1818
parsed['ref']
1919
end
2020

21-
#Create a deployment in this project (and queue it to run)
21+
# Create a deployment in this project (and queue it to run)
2222
def deploy(server, start_revision, end_revision)
2323
run_deployment(server, start_revision, end_revision) do |d|
2424
d.mode = 'queue'
2525
end
2626
end
2727

28-
##
28+
# Create a deployment preview
2929
def preview(server, start_revision, end_revision)
3030
run_deployment(server, start_revision, end_revision) do |d|
3131
d.mode = 'preview'

lib/deploy/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Deploy
2-
VERSION = "2.0.4".freeze
2+
VERSION = "2.1.0".freeze
33
end

0 commit comments

Comments
 (0)