Skip to content

Commit

Permalink
Merge pull request #169 from philreindl/ruby3_json2
Browse files Browse the repository at this point in the history
Ruby3, json2
  • Loading branch information
philreindl authored Dec 31, 2020
2 parents c04e228 + f4f7176 commit a43d161
Show file tree
Hide file tree
Showing 62 changed files with 877 additions and 870 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: [ '2.5', '2.6', '2.7' ]
ruby: [ '2.5', '2.6', '2.7', '3.0' ]

steps:
- name: repo checkout
uses: actions/checkout@v2

- name: Set up Ruby ${{ matrix.ruby }}
uses: actions/setup-ruby@v1
# https://github.com/ruby/setup-ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v1.5.0
Why upgrade?
- Adding support for Ruby 3
- Relaxed version requirement on json

Why wait?
- Minimum version of Faraday brought up to v0.17.3

v1.4.34
- Version update for releasing v1.4.33 as the tags were set wrong for v1.4.32 release

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.34
1.5.0
2 changes: 1 addition & 1 deletion lib/spark_api/authentication/api_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def sign_token(path, params = {}, post_data="")

# Perform an HTTP request (no data)
def request(method, path, body, options)
escaped_path = URI.escape(path)
escaped_path = Addressable::URI.escape(path)
request_opts = {
:AuthToken => @session.auth_token
}
Expand Down
2 changes: 1 addition & 1 deletion lib/spark_api/authentication/oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def authenticate

# Perform an HTTP request (no data)
def request(method, path, body, options={})
escaped_path = URI.escape(path)
escaped_path = Addressable::URI.escape(path)
connection = @client.connection(true) # SSL Only!
connection.headers.merge!(self.auth_header)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create_session(token_params)
response.expires_in = provider.session_timeout if response.expires_in.nil?
SparkApi.logger.debug { "[oauth2] New session created #{response}" }
response
rescue Faraday::Error::ConnectionFailed => e
rescue Faraday::ConnectionFailed => e
if @client.ssl_verify && e.message =~ /certificate verify failed/
SparkApi.logger.error { SparkApi::Errors.ssl_verification_error }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/spark_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SparkApi
# =API Client
# Main class to setup and run requests on the API. A default client is accessible globally as
# SparkApi::client if the global configuration has been set as well. Otherwise, this class may
# be instanciated separately with the configuration information.
# be instantiated separately with the configuration information.
class Client
include Connection
include Authentication
Expand All @@ -21,7 +21,7 @@ def initialize(options={})
Configuration::VALID_OPTION_KEYS.each do |key|
send("#{key}=", options[key])
end
# Instanciate the authenication class passed in.
# Instantiate the authentication class passed in.
@authenticator = authentication_mode.send("new", self)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/spark_api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def request(method, path, body, options)
else
return response.body
end
rescue Faraday::Error::ConnectionFailed => e
rescue Faraday::ConnectionFailed => e
if self.ssl_verify && e.message =~ /certificate verify failed/
SparkApi.logger.error { SparkApi::Errors.ssl_verification_error }
end
Expand Down
33 changes: 13 additions & 20 deletions spark_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,26 @@ Gem::Specification.new do |s|
s.files = Dir["{History.txt,LICENSE,Rakefile,README.md,VERSION}", "{bin,lib,script}/**/*"]
s.test_files = Dir["spec/{fixtures,unit}/**/*", "spec/*.rb"]
s.executables = ["spark_api"]
s.default_executable = %q{spark_api}
s.require_paths = ["lib"]
s.add_dependency 'faraday', '~> 0.9.0'

s.add_dependency 'faraday', '>= 0.17.3'
s.add_dependency 'multi_json', '~> 1.0'
s.add_dependency 'json', '~> 1.7'
s.add_dependency 'json', '>= 1.7'
s.add_dependency 'builder', '>= 2.1.2', '< 4.0.0'
s.add_dependency 'will_paginate', '>= 3.0.pre2', '< 4.0.0'
s.add_dependency 'highline', '>= 1.0'

# spark_api doesn't use public_suffix directly. spark_api uses Webmock, which
# uses addressable, which uses public_suffix. Bundler has suddenly started
# trying to install public_suffix 2.0.4, which requires Ruby 2. When spark_api
# starts to require Ruby 2, this dependency can be removed.
s.add_development_dependency 'public_suffix', '~> 1.4.6'

# TEST GEMS
s.add_development_dependency 'rake', '~> 0.9.2'
s.add_development_dependency 'rspec', '~> 2.14.0'
s.add_development_dependency 'webmock', '~> 1.9'
s.add_development_dependency 'typhoeus', '~> 0.3'
s.add_development_dependency 'ci_reporter', '~> 1.7.0'
s.add_development_dependency 'rb-readline'
s.add_development_dependency 'rb-fsevent'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'webmock'
s.add_development_dependency 'rexml' #needed for ruby 3
s.add_development_dependency 'typhoeus'
s.add_development_dependency 'ci_reporter_rspec'
# s.add_development_dependency 'rb-readline'
# s.add_development_dependency 'rb-fsevent'
# s.add_development_dependency 'simplecov'
s.add_development_dependency 'simplecov-rcov'
s.add_development_dependency 'listen', '~> 3.0.8' # for guard-rspec with ruby 1.9.3
s.add_development_dependency 'guard-rspec'
# s.add_development_dependency 'guard-rspec'
end

13 changes: 9 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

require "rubygems"
require "rspec"
require 'rspec/autorun'
require 'webmock/rspec'
require "json"
require 'multi_json'
Expand All @@ -22,6 +21,13 @@

require 'spark_api'

if ENV['COVERAGE'] == "on"
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start { add_filter %w(/vendor /spec /test) }
end

FileUtils.mkdir 'log' unless File.exists? 'log'

module SparkApi
Expand All @@ -48,17 +54,16 @@ def reset_config
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}

RSpec.configure do |config|

config.include WebMock::API
config.include StubApiRequests

config.treat_symbols_as_metadata_keys_with_true_values = true
config.alias_example_to :on_get_it, :method => 'GET'
config.alias_example_to :on_put_it, :method => 'PUT'
config.alias_example_to :on_post_it, :method => 'POST'
config.alias_example_to :on_delete_it, :method => 'DELETE'
config.before(:all) { reset_config }
config.color_enabled = true
config.color = true
end

def jruby?
Expand Down
43 changes: 21 additions & 22 deletions spec/unit/spark_api/authentication/api_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
subject {SparkApi::Authentication::ApiAuth.new(nil) }
describe "build_param_hash" do
it "should return a blank string when passed nil" do
subject.build_param_string(nil).should be_empty
expect(subject.build_param_string(nil)).to be_empty
end
it "should return a correct param string for one item" do
subject.build_param_string({:foo => "bar"}).should match("foobar")
expect(subject.build_param_string({:foo => "bar"})).to match("foobar")
end
it "should alphabatize the param names by key first, then by value" do
subject.build_param_string({:zoo => "zar", :ooo => "car"}).should match("ooocarzoozar")
subject.build_param_string({:Akey => "aValue", :aNotherkey => "AnotherValue"}).should
match "AkeyaValueaNotherkeyAnotherValue"
expect(subject.build_param_string({:zoo => "zar", :ooo => "car"})).to match("ooocarzoozar")
expect(subject.build_param_string({:Akey => "aValue", :aNotherkey => "AnotherValue"})).to match("AkeyaValueaNotherkeyAnotherValue")
end
end

Expand All @@ -33,24 +32,24 @@
stub_request(:post, "https://api.sparkapi.com/#{SparkApi.version}/session").
with(:query => {:ApiKey => "my_key", :ApiSig => "c731cf2455fbc7a4ef937b2301108d7a"}).
to_return(:body => fixture("authentication_failure.json"), :status=>401)
expect {subject.authenticate()}.to raise_error(SparkApi::ClientError){ |e| e.status.should == 401 }
expect {subject.authenticate()}.to raise_error(SparkApi::ClientError){ |e| expect(e.status).to eq(401) }
end
end

describe "authenticated?" do
let(:session) { Object.new }
it "should return true when session is active" do
subject.session = session
session.stub(:expired?) { false }
subject.authenticated?.should eq(true)
allow(session).to receive(:expired?) { false }
expect(subject.authenticated?).to eq(true)
end
it "should return false when session is expired" do
subject.session = session
session.stub(:expired?) { true }
subject.authenticated?.should eq(false)
allow(session).to receive(:expired?) { true }
expect(subject.authenticated?).to eq(false)
end
it "should return false when session is uninitialized" do
subject.authenticated?.should eq(false)
expect(subject.authenticated?).to eq(false)
end
end

Expand All @@ -61,14 +60,14 @@
it "should logout when there is an active session" do
logged_out = false
subject.session = session
client.stub(:delete).with("/session/1234") { logged_out = true }
allow(client).to receive(:delete).with("/session/1234") { logged_out = true }
subject.logout
subject.session.should eq(nil)
logged_out.should eq(true)
expect(subject.session).to eq(nil)
expect(logged_out).to eq(true)
end
it "should skip logging out when there is no active session information" do
client.stub(:delete) { raise "Should not be called" }
subject.logout.should eq(nil)
allow(client).to receive(:delete) { raise "Should not be called" }
expect(subject.logout).to eq(nil)
end
end

Expand Down Expand Up @@ -98,7 +97,7 @@
:AuthToken => "1234"}.merge(args)).
to_return(:body => fixture("listings/no_subresources.json"))
subject.session = session
subject.request(:get, "/#{SparkApi.version}/listings", nil, args).status.should eq(200)
expect(subject.request(:get, "/#{SparkApi.version}/listings", nil, args).status).to eq(200)
end
it "should handle a post request" do
stub_auth_request
Expand All @@ -118,14 +117,14 @@
}]}
}',
:status=>201)
subject.request(:post, "/#{SparkApi.version}/contacts", contact, args).status.should eq(201)
expect(subject.request(:post, "/#{SparkApi.version}/contacts", contact, args).status).to eq(201)
end
end

describe "sign" do
it "should sign the auth parameters correctly" do
sign_token = "my_secretApiKeymy_key"
subject.sign(sign_token).should eq("c731cf2455fbc7a4ef937b2301108d7a")
expect(subject.sign(sign_token)).to eq("c731cf2455fbc7a4ef937b2301108d7a")
end
end

Expand All @@ -134,7 +133,7 @@
subject {SparkApi::Authentication::ApiAuth.new(client) }
it "should fully sign the token" do
parms = {:AuthToken => "1234", :ApiUser => "CoolAsIce"}
subject.sign_token("/test", parms).should eq("7bbe3384a8b64368357f8551cab271e3")
expect(subject.sign_token("/test", parms)).to eq("7bbe3384a8b64368357f8551cab271e3")
end
end

Expand All @@ -160,8 +159,8 @@
to_return(:body => fixture('listings/with_documents.json'))
l = Listing.find('1234', :_expand => "Documents")

count.should eq(2)
SparkApi.client.session.expired?.should eq(false)
expect(count).to eq(2)
expect(SparkApi.client.session.expired?).to eq(false)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/spark_api/authentication/base_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe SparkApi::Authentication::BaseAuth do
subject {SparkApi::Authentication::BaseAuth.new(nil) }
it "should raise an error" do
expect {subject.authenticate()}.to raise_error(){ |e| e.message.should == "Implement me!"}
expect {subject.logout()}.to raise_error(){ |e| e.message.should == "Implement me!"}
expect {subject.request(nil, nil, nil, nil)}.to raise_error(){ |e| e.message.should == "Implement me!"}
expect {subject.authenticate()}.to raise_error(){ |e| expect(e.message).to eq("Implement me!")}
expect {subject.logout()}.to raise_error(){ |e| expect(e.message).to eq("Implement me!")}
expect {subject.request(nil, nil, nil, nil)}.to raise_error(){ |e| expect(e.message).to eq("Implement me!")}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
:status => 201
}
subject.on_complete env
env[:body]["token"].should eq("sp4rkb4rt0k3n")
expect(env[:body]["token"]).to eq("sp4rkb4rt0k3n")
end

it "should raise error on unsuccessful response" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
subject { SparkApi::Authentication::OAuth2Impl::GrantTypeBase }
# Make sure the client boostraps the right plugin based on configuration.
it "create should " do
expect {subject.create(nil, InvalidAuth2Provider.new())}.to raise_error(SparkApi::ClientError){ |e| e.message.should == "Unsupported grant type [not_a_real_type]" }
expect {subject.create(nil, InvalidAuth2Provider.new())}.to raise_error(SparkApi::ClientError){ |e| expect(e.message).to eq("Unsupported grant type [not_a_real_type]") }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe SparkApi::Authentication::SingleSessionProvider do
subject { SparkApi::Authentication::SingleSessionProvider.new({ :access_token => "the_token" }) }
it "should initialize a new session with access_token" do
subject.load_session.should respond_to(:access_token)
subject.load_session.access_token.should eq("the_token")
expect(subject.load_session).to respond_to(:access_token)
expect(subject.load_session.access_token).to eq("the_token")
end
end
Loading

0 comments on commit a43d161

Please sign in to comment.