diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 62b49d8..fcacc67 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ["2.4", "2.5", "2.6", "2.7", "3.0"] + ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"] + gemfile: ["faraday-1", "faraday-2"] + + env: + BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile + BUNDLE_PATH_RELATIVE_TO_CWD: true steps: - uses: actions/checkout@master diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..c19ce03 --- /dev/null +++ b/.standard.yml @@ -0,0 +1 @@ +ruby_version: 2.7 diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..4e23081 --- /dev/null +++ b/Appraisals @@ -0,0 +1,8 @@ +appraise "faraday-1" do + gem "faraday", "~> 1.10" + gem "base64", "~> 0.2.0" +end + +appraise "faraday-2" do + gem "faraday", "~> 2.0" +end diff --git a/Gemfile b/Gemfile index 76d84c8..9fa49db 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gemspec gem "rake" gem "minitest", ">= 5.0.5" gem "standard" +gem "appraisal" diff --git a/gemfiles/faraday_1.gemfile b/gemfiles/faraday_1.gemfile new file mode 100644 index 0000000..5af0144 --- /dev/null +++ b/gemfiles/faraday_1.gemfile @@ -0,0 +1,12 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rake" +gem "minitest", ">= 5.0.5" +gem "standard" +gem "appraisal" +gem "faraday", "~> 1.10" +gem "base64", "~> 0.2.0" + +gemspec path: "../" diff --git a/gemfiles/faraday_1.gemfile.lock b/gemfiles/faraday_1.gemfile.lock new file mode 100644 index 0000000..4dee6d4 --- /dev/null +++ b/gemfiles/faraday_1.gemfile.lock @@ -0,0 +1,102 @@ +PATH + remote: .. + specs: + vultr (1.0.0) + faraday (>= 1.0, < 3.0) + +GEM + remote: https://rubygems.org/ + specs: + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + ast (2.4.2) + base64 (0.2.0) + faraday (1.10.3) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0) + faraday-multipart (~> 1.0) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.0) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + faraday-retry (~> 1.0) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-multipart (1.0.4) + multipart-post (~> 2) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + faraday-retry (1.0.3) + json (2.7.2) + language_server-protocol (3.17.0.3) + lint_roller (1.1.0) + minitest (5.23.1) + multipart-post (2.4.1) + parallel (1.24.0) + parser (3.3.2.0) + ast (~> 2.4.1) + racc + racc (1.8.0) + rainbow (3.1.1) + rake (13.2.1) + regexp_parser (2.9.2) + rexml (3.2.8) + strscan (>= 3.0.9) + rubocop (1.63.5) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + rubocop-performance (1.21.0) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + standard (1.36.0) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.63.0) + standard-custom (~> 1.0.0) + standard-performance (~> 1.4) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.4.0) + lint_roller (~> 1.1) + rubocop-performance (~> 1.21.0) + strscan (3.1.0) + thor (1.3.1) + unicode-display_width (2.5.0) + +PLATFORMS + ruby + x86_64-darwin-23 + +DEPENDENCIES + appraisal + base64 (~> 0.2.0) + faraday (~> 1.10) + minitest (>= 5.0.5) + rake + standard + vultr! + +BUNDLED WITH + 2.5.10 diff --git a/gemfiles/faraday_2.gemfile b/gemfiles/faraday_2.gemfile new file mode 100644 index 0000000..304e3f2 --- /dev/null +++ b/gemfiles/faraday_2.gemfile @@ -0,0 +1,11 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rake" +gem "minitest", ">= 5.0.5" +gem "standard" +gem "appraisal" +gem "faraday", "~> 2.0" + +gemspec path: "../" diff --git a/gemfiles/faraday_2.gemfile.lock b/gemfiles/faraday_2.gemfile.lock new file mode 100644 index 0000000..55488f1 --- /dev/null +++ b/gemfiles/faraday_2.gemfile.lock @@ -0,0 +1,82 @@ +PATH + remote: .. + specs: + vultr (1.0.0) + faraday (>= 1.0, < 3.0) + +GEM + remote: https://rubygems.org/ + specs: + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + ast (2.4.2) + faraday (2.9.0) + faraday-net_http (>= 2.0, < 3.2) + faraday-net_http (3.1.0) + net-http + json (2.7.2) + language_server-protocol (3.17.0.3) + lint_roller (1.1.0) + minitest (5.23.1) + net-http (0.4.1) + uri + parallel (1.24.0) + parser (3.3.2.0) + ast (~> 2.4.1) + racc + racc (1.8.0) + rainbow (3.1.1) + rake (13.2.1) + regexp_parser (2.9.2) + rexml (3.2.8) + strscan (>= 3.0.9) + rubocop (1.63.5) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + rubocop-performance (1.21.0) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (1.13.0) + standard (1.36.0) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.63.0) + standard-custom (~> 1.0.0) + standard-performance (~> 1.4) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.4.0) + lint_roller (~> 1.1) + rubocop-performance (~> 1.21.0) + strscan (3.1.0) + thor (1.3.1) + unicode-display_width (2.5.0) + uri (0.13.0) + +PLATFORMS + ruby + x86_64-darwin-23 + +DEPENDENCIES + appraisal + faraday (~> 2.0) + minitest (>= 5.0.5) + rake + standard + vultr! + +BUNDLED WITH + 2.5.10 diff --git a/lib/vultr.rb b/lib/vultr.rb index 36c25c5..35ef52d 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -1,11 +1,11 @@ -require "faraday" -require "faraday_middleware" require "vultr/version" +require "faraday" module Vultr autoload :Client, "vultr/client" autoload :Collection, "vultr/collection" autoload :Error, "vultr/error" + autoload :Middleware, "vultr/middleware" autoload :Resource, "vultr/resource" autoload :Object, "vultr/object" @@ -54,4 +54,6 @@ module Vultr autoload :SshKey, "vultr/objects/ssh_key" autoload :StartupScript, "vultr/objects/startup_script" autoload :User, "vultr/objects/user" + + Faraday::Response.register_middleware vultr: Middleware end diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index e494f7b..b9b9811 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -101,7 +101,7 @@ def connection conn.request :authorization, :Bearer, api_key conn.request :json - conn.response :dates + conn.response :vultr conn.response :json, content_type: "application/json" conn.adapter adapter, @stubs diff --git a/lib/vultr/middleware.rb b/lib/vultr/middleware.rb new file mode 100644 index 0000000..a9712f3 --- /dev/null +++ b/lib/vultr/middleware.rb @@ -0,0 +1,32 @@ +require "time" + +module Vultr + class Middleware < Faraday::Middleware + ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|((\+|-)\d{2}:?\d{2}))\Z/xm + + # This method will be called when the response is being processed. + # You can alter it as you like, accessing things like response_body, response_headers, and more. + # Refer to Faraday::Env for a list of accessible fields: + # https://github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb + # + # @param env [Faraday::Env] the environment of the response being processed. + def on_complete(env) + parse_dates! env[:body] + end + + private + + def parse_dates!(value) + case value + when Hash + value.each { |key, element| value[key] = parse_dates!(element) } + when Array + value.each_with_index { |element, index| value[index] = parse_dates!(element) } + when ISO_DATE_FORMAT + Time.parse(value) + else + value + end + end + end +end diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index 4ec9d36..6905f7a 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -3,7 +3,7 @@ module Vultr class Object < OpenStruct def initialize(attributes) - super to_ostruct(attributes) + super(to_ostruct(attributes)) end def to_ostruct(obj) diff --git a/vultr.gemspec b/vultr.gemspec index 297b4c3..544b903 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -7,8 +7,8 @@ require "vultr/version" Gem::Specification.new do |s| s.name = "vultr" s.version = Vultr::VERSION - s.authors = ["Gao Jiangmiao"] - s.email = ["tolbkni@gmail.com"] + s.authors = ["Gao Jiangmiao", "Chris Oliver"] + s.email = ["tolbkni@gmail.com", "excid3@gmail.com"] s.summary = "Ruby bindings for VULTR API." s.description = "Ruby bindings for VULTR API. VULTR APIs can be found here: https://www.vultr.com/api/" s.homepage = "https://github.com/tolbkni/vultr.rb" @@ -16,11 +16,9 @@ Gem::Specification.new do |s| s.files = `git ls-files -z`.split("\x0") s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } - s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = ["lib"] s.required_ruby_version = ">= 2.4" - s.add_dependency "faraday", "~> 1.7" - s.add_dependency "faraday_middleware", "~> 1.1" + s.add_dependency "faraday", ">= 1.0", "< 3.0" end