From 356d43d06288fb4025ffa2bfe3b625bb0a775947 Mon Sep 17 00:00:00 2001 From: Vignesh Kennadi Date: Thu, 5 Sep 2024 14:12:31 +0530 Subject: [PATCH 1/2] Fix Linting Issues in Ruby SDK --- .rubocop.yml | 18 ++---- lib/xero-ruby/api_client.rb | 28 ++++----- lib/xero-ruby/where.rb | 74 ++++++++++++------------ spec/api_client_spec.rb | 112 ++++++++++++++++++------------------ spec/api_error_spec.rb | 2 +- spec/configuration_spec.rb | 4 +- spec/helper_methods_spec.rb | 6 +- spec/where_spec.rb | 4 +- 8 files changed, 121 insertions(+), 127 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 790f0299..5dc960b5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,12 +14,6 @@ AllCops: Style/AndOr: Enabled: true -# Do not use braces for hash literals when they are the last argument of a -# method call. -Style/BracesAroundHashParameters: - Enabled: true - EnforcedStyle: context_dependent - # Align `when` with `case`. Layout/CaseIndentation: Enabled: true @@ -36,7 +30,7 @@ Layout/EmptyLineAfterMagicComment: # In a regular class definition, no empty lines around the body. Layout/EmptyLinesAroundClassBody: - Enabled: true + Enabled: false # In a regular method definition, no empty lines around the body. Layout/EmptyLinesAroundMethodBody: @@ -46,7 +40,7 @@ Layout/EmptyLinesAroundMethodBody: Layout/EmptyLinesAroundModuleBody: Enabled: true -Layout/IndentFirstArgument: +Layout/FirstArgumentIndentation: Enabled: true # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }. @@ -57,7 +51,7 @@ Style/HashSyntax: # extra level of indentation. Layout/IndentationConsistency: Enabled: true - EnforcedStyle: rails + EnforcedStyle: indented_internal_methods # Two spaces, no tabs (for indentation). Layout/IndentationWidth: @@ -119,11 +113,11 @@ Layout/SpaceInsideParens: # EnforcedStyle: single_quotes # Detect hard tabs, no hard tabs. -Layout/Tab: +Layout/IndentationStyle: Enabled: true # Blank lines should not have any spaces. -Layout/TrailingBlankLines: +Layout/TrailingEmptyLines: Enabled: true # No trailing whitespace. @@ -131,7 +125,7 @@ Layout/TrailingWhitespace: Enabled: false # Use quotes for string literals when they are enough. -Style/UnneededPercentQ: +Style/RedundantPercentQ: Enabled: true # Align `end` with the matching keyword or starting expression except for diff --git a/lib/xero-ruby/api_client.rb b/lib/xero-ruby/api_client.rb index 8dc16d4b..8a836527 100644 --- a/lib/xero-ruby/api_client.rb +++ b/lib/xero-ruby/api_client.rb @@ -52,7 +52,7 @@ def initialize(config: {}, credentials: {}) def append_to_default_config(default_config, user_config) config = default_config - user_config.each{|k,v| config.send("#{k}=", v)} + user_config.each { |k, v| config.send("#{k}=", v) } config end @@ -143,7 +143,7 @@ def set_token_set(token_set) set_access_token(token_set[:access_token]) if token_set[:access_token] set_id_token(token_set[:id_token]) if token_set[:id_token] - return true + true end def set_access_token(access_token) @@ -160,7 +160,7 @@ def get_client_credentials_token } token_set = token_request(data, '/token') - return token_set + token_set end def get_token_set_from_callback(params) @@ -173,7 +173,7 @@ def get_token_set_from_callback(params) validate_tokens(token_set) validate_state(params) - return token_set + token_set end def validate_tokens(token_set) @@ -184,17 +184,17 @@ def validate_tokens(token_set) decode_jwt(access_token) if access_token decode_jwt(id_token) if id_token end - return true + true end def validate_state(params) if params['state'] != @state raise StandardError.new "WARNING: @config.state: #{@state} and OAuth callback state: #{params['state']} do not match!" end - return true + true end - def decode_jwt(tkn, verify=true) + def decode_jwt(tkn, verify = true) if verify == true response = Faraday.get('https://identity.xero.com/.well-known/openid-configuration/jwks') do |req| @@ -220,7 +220,7 @@ def refresh_token_set(token_set) grant_type: 'refresh_token', refresh_token: token_set[:refresh_token] } - return token_request(data, '/token') + token_request(data, '/token') end def revoke_token(token_set) @@ -228,7 +228,7 @@ def revoke_token(token_set) data = { token: token_set[:refresh_token] } - return token_request(data, '/revocation') + token_request(data, '/revocation') end def token_request(data, path) @@ -245,24 +245,24 @@ def token_request(data, path) else body = {} end - return body + body end # Connection heplers def connections @config.base_url = 'https://api.xero.com' - opts = { :header_params => {'Content-Type': 'application/json'}, :auth_names => ['OAuth2'] } + opts = { :header_params => { 'Content-Type': 'application/json' }, :auth_names => ['OAuth2'] } response = call_api(:GET, "/connections/", nil, opts) response[0] end def last_connection - connections.sort { |a,b| DateTime.parse(a['updatedDateUtc']) <=> DateTime.parse(b['updatedDateUtc'])}.last + connections.sort { |a, b| DateTime.parse(a['updatedDateUtc']) <=> DateTime.parse(b['updatedDateUtc']) }.last end def disconnect(connection_id) @config.base_url = 'https://api.xero.com' - opts = { :header_params => {'Content-Type': 'application/json'}, :auth_names => ['OAuth2'] } + opts = { :header_params => { 'Content-Type': 'application/json' }, :auth_names => ['OAuth2'] } call_api(:DELETE, "/connections/#{connection_id}", nil, opts) connections end @@ -286,7 +286,7 @@ def call_api(http_method, path, api_client, opts = {}) when "AssetApi" method_base_url = @config.asset_url when "FilesApi" - method_base_url = @config.files_url + method_base_url = @config.files_url when "PayrollAuApi" method_base_url = @config.payroll_au_url when "PayrollNzApi" diff --git a/lib/xero-ruby/where.rb b/lib/xero-ruby/where.rb index 4f26054a..77ae6cbd 100644 --- a/lib/xero-ruby/where.rb +++ b/lib/xero-ruby/where.rb @@ -17,51 +17,51 @@ def to_param private - def parameterize_option(key, value) - quoted_key = quote_key(key) + def parameterize_option(key, value) + quoted_key = quote_key(key) - case value - when Array - operator, query = value + case value + when Array + operator, query = value - if STRING_FUNCTIONS.include?(camelize_key(operator)) - "#{quoted_key}.#{camelize_key(operator)}(#{quote_value(query)})" + if STRING_FUNCTIONS.include?(camelize_key(operator)) + "#{quoted_key}.#{camelize_key(operator)}(#{quote_value(query)})" + else + "#{quoted_key} #{operator} #{quote_value(query)}" + end + when Range + "#{quoted_key} >= #{quote_value(value.first)} AND #{quoted_key} <= #{quote_value(value.last)}" + when /^\./ + "#{quoted_key}#{value}" else - "#{quoted_key} #{operator} #{quote_value(query)}" + "#{quoted_key} #{value}" end - when Range - "#{quoted_key} >= #{quote_value(value.first)} AND #{quoted_key} <= #{quote_value(value.last)}" - when /^\./ - "#{quoted_key}#{value}" - else - "#{quoted_key} #{value}" end - end - def quote_key(key) - case key - when :contact_id - 'Contact.ContactID' - when :contact_number - 'Contact.ContactNumber' - when :invoice_id - 'Invoice.InvoiceId' - else - camelize_key(key) + def quote_key(key) + case key + when :contact_id + 'Contact.ContactID' + when :contact_number + 'Contact.ContactNumber' + when :invoice_id + 'Invoice.InvoiceId' + else + camelize_key(key) + end end - end - def quote_value(value) - case value - when DateTime, Date, Time - "DateTime(#{value.strftime("%Y,%m,%d")})" - when Numeric, false, true - value.to_s - when UUID_REGEXP - %{guid("#{value}")} - else - %{"#{value.to_s.gsub('"', '""')}"} + def quote_value(value) + case value + when DateTime, Date, Time + "DateTime(#{value.strftime("%Y,%m,%d")})" + when Numeric, false, true + value.to_s + when UUID_REGEXP + %{guid("#{value}")} + else + %{"#{value.to_s.gsub('"', '""')}"} + end end - end end end diff --git a/spec/api_client_spec.rb b/spec/api_client_spec.rb index 976f66ea..617433b7 100644 --- a/spec/api_client_spec.rb +++ b/spec/api_client_spec.rb @@ -70,8 +70,8 @@ state: "custom-state" } api_client = XeroRuby::ApiClient.new(credentials: creds) - altered_state = {'state': 'not-original-state'} - expect{api_client.validate_state(altered_state)}.to raise_error(StandardError, 'WARNING: @config.state: custom-state and OAuth callback state: do not match!') + altered_state = { 'state': 'not-original-state' } + expect { api_client.validate_state(altered_state) }.to raise_error(StandardError, 'WARNING: @config.state: custom-state and OAuth callback state: do not match!') end end @@ -102,16 +102,16 @@ describe 'api_client helper functions' do let(:api_client) { XeroRuby::ApiClient.new } - let(:token_set) { {'access_token': 'eyx.authorization.data', 'id_token': 'eyx.authentication.data', 'refresh_token': 'REFRESHMENTS'} } + let(:token_set) { { 'access_token': 'eyx.authorization.data', 'id_token': 'eyx.authentication.data', 'refresh_token': 'REFRESHMENTS' } } let(:connections) { - [{ - "id" => "xxx-yyy-zzz", - "tenantId" => "xxx-yyy-zzz", - "tenantType" => "ORGANISATION", - "tenantName" => "Demo Company (US)", - "createdDateUtc" => "2019-11-01T20:08:03.0766400", - "updatedDateUtc" => "2020-04-15T22:37:10.4943410" - }] + [{ + "id" => "xxx-yyy-zzz", + "tenantId" => "xxx-yyy-zzz", + "tenantType" => "ORGANISATION", + "tenantName" => "Demo Company (US)", + "createdDateUtc" => "2019-11-01T20:08:03.0766400", + "updatedDateUtc" => "2020-04-15T22:37:10.4943410" + }] } before do @@ -190,7 +190,7 @@ } } api_client.accounting_api.get_invoices('active_tenant_id', opts) - expect(opts).to eq({:where=>{:invoice_number=>["=", "INV-0060"]}}) + expect(opts).to eq({ :where => { :invoice_number => ["=", "INV-0060"] } }) end end @@ -224,10 +224,10 @@ end describe "#object_to_hash modifies a hash from snake_case to PascalCase" do - contact_after = {:Contacts=>[{:Name=>"Bruce Banner", :EmailAddress=>"hulk@avengers.com", :Phones=>[{:PhoneType=>"MOBILE", :PhoneNumber=>"555-1212", :PhoneAreaCode=>"415"}], :PaymentTerms=>{:Bills=>{:Day=>15, :Type=>"OFCURRENTMONTH"}, :Sales=>{:Day=>10, :Type=>"DAYSAFTERBILLMONTH"}}}]} + contact_after = { :Contacts => [{ :Name => "Bruce Banner", :EmailAddress => "hulk@avengers.com", :Phones => [{ :PhoneType => "MOBILE", :PhoneNumber => "555-1212", :PhoneAreaCode => "415" }], :PaymentTerms => { :Bills => { :Day => 15, :Type => "OFCURRENTMONTH" }, :Sales => { :Day => 10, :Type => "DAYSAFTERBILLMONTH" } } }] } it 'Serializes snake_case object correctly' do - contact_before = {:contacts=>[{:name=>"Bruce Banner", :email_address=>"hulk@avengers.com", :phones=>[{:phone_type=>"MOBILE", :phone_number=>"555-1212", :phone_area_code=>"415"}], :payment_terms=>{:bills=>{:day=>15, :type=>"OFCURRENTMONTH"}, :sales=>{:day=>10, :type=>"DAYSAFTERBILLMONTH"}}}]} + contact_before = { :contacts => [{ :name => "Bruce Banner", :email_address => "hulk@avengers.com", :phones => [{ :phone_type => "MOBILE", :phone_number => "555-1212", :phone_area_code => "415" }], :payment_terms => { :bills => { :day => 15, :type => "OFCURRENTMONTH" }, :sales => { :day => 10, :type => "DAYSAFTERBILLMONTH" } } }] } api_client = XeroRuby::ApiClient.new expect(api_client.object_to_hash(contact_before)).to eq(contact_after) end @@ -255,26 +255,26 @@ it 'Serializes json with multiple nested objects correctly' do json_before = { - "line_Items":[ + "line_Items": [ { - "quantity":1.0, - "unit_amount":20, - "sub_Items":[ + "quantity": 1.0, + "unit_amount": 20, + "sub_Items": [ { - "quantity":1.0, - "unit_amount":20 + "quantity": 1.0, + "unit_amount": 20 }, { - "quantity":1.0, - "Unit_amount":20, - "Deep_Items":[ + "quantity": 1.0, + "Unit_amount": 20, + "Deep_Items": [ { - "quantity":1.0, - "unit_amount":20 + "quantity": 1.0, + "unit_amount": 20 }, { - "quantity":1.0, - "unit_Amount":20 + "quantity": 1.0, + "unit_Amount": 20 } ] } @@ -284,26 +284,26 @@ } json_after = { - "LineItems":[ + "LineItems": [ { - "UnitAmount":20, - "Quantity":1.0, - "SubItems":[ + "UnitAmount": 20, + "Quantity": 1.0, + "SubItems": [ { - "UnitAmount":20, - "Quantity":1.0 + "UnitAmount": 20, + "Quantity": 1.0 }, { - "UnitAmount":20, - "Quantity":1.0, - "DeepItems":[ + "UnitAmount": 20, + "Quantity": 1.0, + "DeepItems": [ { - "UnitAmount":20, - "Quantity":1.0 + "UnitAmount": 20, + "Quantity": 1.0 }, { - "UnitAmount":20, - "Quantity":1.0 + "UnitAmount": 20, + "Quantity": 1.0 } ] } @@ -316,8 +316,8 @@ end it 'Serializes mixed cased keys correctly' do - contact_after = { :Contacts=>[{:Name=>"Bruce Banner", :EmailAddress=>"hulk@avengers.com", :Phones=>[{:PhoneType=>"MOBILE", :PhoneNumber=>"555-1212", :PhoneAreaCode=>"415"}], :PaymentTerms=>{:Bills=>{:Day=>15, :Type=>"OFCURRENTMONTH"}, :Sales=>{:Day=>10, :Type=>"DAYSAFTERBILLMONTH"}}}]} - contact_before = {:contacts=>[{:name=>"Bruce Banner", :emailAddress=>"hulk@avengers.com", :phones=>[{:phoneType=>"MOBILE", :phone_number=>"555-1212", :phone_areaCode=>"415"}], :Payment_terms=>{:bills=>{:day=>15, :type=>"OFCURRENTMONTH"}, :sales=>{:day=>10, :type=>"DAYSAFTERBILLMONTH"}}}]} + contact_after = { :Contacts => [{ :Name => "Bruce Banner", :EmailAddress => "hulk@avengers.com", :Phones => [{ :PhoneType => "MOBILE", :PhoneNumber => "555-1212", :PhoneAreaCode => "415" }], :PaymentTerms => { :Bills => { :Day => 15, :Type => "OFCURRENTMONTH" }, :Sales => { :Day => 10, :Type => "DAYSAFTERBILLMONTH" } } }] } + contact_before = { :contacts => [{ :name => "Bruce Banner", :emailAddress => "hulk@avengers.com", :phones => [{ :phoneType => "MOBILE", :phone_number => "555-1212", :phone_areaCode => "415" }], :Payment_terms => { :bills => { :day => 15, :type => "OFCURRENTMONTH" }, :sales => { :day => 10, :type => "DAYSAFTERBILLMONTH" } } }] } api_client = XeroRuby::ApiClient.new expect(api_client.object_to_hash(contact_before)).to eq(contact_after) end @@ -418,9 +418,9 @@ describe 'token helper methods' do let(:api_client) { XeroRuby::ApiClient.new } - let(:id_token){'eyJhbGciOiJSUzI1NiIsImtpZCI6IjFDQUY4RTY2NzcyRDZEQzAyOEQ2NzI2RkQwMjYxNTgxNTcwRUZDMTkiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJISy1PWm5jdGJjQW8xbkp2MENZVmdWY09fQmsifQ.eyJuYmYiOjE2MTk3MTQwNDMsImV4cCI6MTYxOTcxNDM0MywiaXNzIjoiaHR0cHM6Ly9pZGVudGl0eS54ZXJvLmNvbSIsImF1ZCI6IkFEQjVBNzdEQTZCNjRFOTI4RDg0MDkwOTlBMzlDQTdCIiwiaWF0IjoxNjE5NzE0MDQzLCJhdF9oYXNoIjoiMXJNamVvUTJiOUxUNFU0ZlBXbEZJZyIsInNpZCI6ImY0YTY4ZDc0ZmM3OTQzMjc4YTgzMTg0NGM5ZWRmNzFiIiwic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jU3LWExMGI5ZTI0ZGI1NSIsInNjb3BlIjpbImVtYWlsIiwicHJvZmlsZSIsIm9wZW5pZCIsImFjY291bnRpbmcucmVwb3J0cy5yZWFkIiwiZmlsZXMiLCJwYXlyb2xsLmVtcGxveWVlcyIsInBheXJvbGwucGF5cnVucyIsInBheXJvbGwucGF5c2xpcCIsInBheXJvbGwudGltZXNoZWV0cyIsInByb2plY3RzLnJlYWQiLCJwcm9qZWN0cyIsImFjY291bnRpbmcuc2V0dGluZ3MiLCJhY2NvdW50aW5nLmF0dGFjaG1lbnRzIiwiYWNjb3VudGluZy50cmFuc2FjdGlvbnMiLCJhY2NvdW50aW5nLmpvdXJuYWxzLnJlYWQiLCJhc3NldHMucmVhZCIsImFzc2V0cyIsImFjY291bnRpbmcuY29udGFjdHMiLCJwYXlyb2xsLnNldHRpbmdzIiwib2ZmbGluZV9hY2Nlc3MiXX0.vNV-YsgHFWKFBmyYdhg7tztdsPc9ykObadQcGFoFXJ8qCBerR3h7XXKzWAP3KzFzhOCcIpWU8Q081zuYBNxahPeeLRLUuc_3MwgwE72esE5vGuxa2_-_QidtNvMCgsX-ie_LcX7FE_KI-sXB_EZ8fDk6WAMIPC9d3GejgeuH5Uh6rZkhowN2jm5pZjEOEy_QE7PScBO0XEbiZNUsarvBUSdKuSTvVVLHzHzs0bHMRfgKEkqZySNtZlac-oyaL3PVba1S7A_vbRcNWpYR_VrKGf2g9LHSI3EA5j3Beto4pKukU-bk6rLBGul37u4tM17U-wyJLsFmt6ZC_SEJKgmluQ' } + let(:tkn_set) { { 'id_token': id_token, 'access_token': access_token, 'refresh_token': 'abc123xyz' } } before do api_client.set_token_set(tkn_set) @@ -431,17 +431,17 @@ end it '#token_expired? for a just expired token' do - allow(api_client).to receive(:decoded_access_token).and_return({"exp"=>Time.now.to_i}) + allow(api_client).to receive(:decoded_access_token).and_return({ "exp" => Time.now.to_i }) expect(api_client.token_expired?).to eq(true) end it '#token_expired? for a non-expired token' do - allow(api_client).to receive(:decoded_access_token).and_return({"exp"=>(Time.now + 30.minutes).to_i}) + allow(api_client).to receive(:decoded_access_token).and_return({ "exp" => (Time.now + 30.minutes).to_i }) expect(api_client.token_expired?).to eq(false) end it '#token_expired? for an almost expired token' do - allow(api_client).to receive(:decoded_access_token).and_return({"exp"=>(Time.now + 30.seconds).to_i}) + allow(api_client).to receive(:decoded_access_token).and_return({ "exp" => (Time.now + 30.seconds).to_i }) expect(api_client.token_expired?).to eq(false) end @@ -463,29 +463,29 @@ it 'decoding an invalid access_token' do api_client.set_access_token("#{access_token}.NotAValidJWTstring") - expect{api_client.decoded_access_token}.to raise_error(JSON::JWT::InvalidFormat) + expect { api_client.decoded_access_token }.to raise_error(JSON::JWT::InvalidFormat) end it 'decoding an invalid id_token' do api_client.set_id_token("#{id_token}.NotAValidJWTstring") - expect{api_client.decoded_id_token}.to raise_error(JSON::JWT::InvalidFormat) + expect { api_client.decoded_id_token }.to raise_error(JSON::JWT::InvalidFormat) end end describe 'thread safety in the XeroClient' do - let(:creds) {{ + let(:creds) { { client_id: 'abc', client_secret: '123', redirect_uri: 'https://mydomain.com/callback', scopes: 'openid profile email accounting.transactions' }} - let(:api_client_1) {XeroRuby::ApiClient.new(credentials: creds)} - let(:api_client_2) {XeroRuby::ApiClient.new(credentials: creds)} - let(:api_client_3) {XeroRuby::ApiClient.new(credentials: creds)} + let(:api_client_1) { XeroRuby::ApiClient.new(credentials: creds) } + let(:api_client_2) { XeroRuby::ApiClient.new(credentials: creds) } + let(:api_client_3) { XeroRuby::ApiClient.new(credentials: creds) } - let(:tkn_set_1){{'id_token': "abc.123.1", 'access_token': "xxx.yyy.zzz.111"}} - let(:tkn_set_2){{'id_token': "efg.456.2", 'access_token': "xxx.yyy.zzz.222"}} + let(:tkn_set_1) { { 'id_token': "abc.123.1", 'access_token': "xxx.yyy.zzz.111" } } + let(:tkn_set_2) { { 'id_token': "efg.456.2", 'access_token': "xxx.yyy.zzz.222" } } describe 'when configuration is changed, other instantiations of the client are not affected' do it 'applies to #set_access_token' do diff --git a/spec/api_error_spec.rb b/spec/api_error_spec.rb index 9ed1617c..24303e0e 100644 --- a/spec/api_error_spec.rb +++ b/spec/api_error_spec.rb @@ -29,7 +29,7 @@ } api_error = XeroRuby::ApiError.new( :code => 400, - :response_headers => {"content-type"=>"application/json; charset=utf-8", "content-length"=>"1700", "server"=>"nginx", "xero-correlation-id"=>"31e2c94e-3ae4-402a-a374-b7a94ef9445c", "x-appminlimit-remaining"=>"9988", "x-minlimit-remaining"=>"58", "x-daylimit-remaining"=>"4998", "expires"=>"Fri, 18 Dec 2020 17:56:29 GMT", "cache-control"=>"max-age=0, no-cache, no-store", "pragma"=>"no-cache", "date"=>"Fri, 18 Dec 2020 17:56:29 GMT", "connection"=>"close", "x-client-tls-ver"=>"tls1.3"}, + :response_headers => { "content-type" => "application/json; charset=utf-8", "content-length" => "1700", "server" => "nginx", "xero-correlation-id" => "31e2c94e-3ae4-402a-a374-b7a94ef9445c", "x-appminlimit-remaining" => "9988", "x-minlimit-remaining" => "58", "x-daylimit-remaining" => "4998", "expires" => "Fri, 18 Dec 2020 17:56:29 GMT", "cache-control" => "max-age=0, no-cache, no-store", "pragma" => "no-cache", "date" => "Fri, 18 Dec 2020 17:56:29 GMT", "connection" => "close", "x-client-tls-ver" => "tls1.3" }, :response_body => error_body ) expect(api_error.message).to include("The TaxType code NONE does not exist or cannot be used for this type of transaction") diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index b524b19a..3daa2666 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -25,12 +25,12 @@ end it 'should allow you to overwrite the default configuration options' do - client = XeroRuby::ApiClient.new(credentials: {}, config: {login_url: 'ngrok.login.xero.test'}) + client = XeroRuby::ApiClient.new(credentials: {}, config: { login_url: 'ngrok.login.xero.test' }) expect(client.config.login_url).to eq('ngrok.login.xero.test') end it 'should allow you to set the timeout config option' do - client = XeroRuby::ApiClient.new(credentials: {}, config: {timeout: 30}) + client = XeroRuby::ApiClient.new(credentials: {}, config: { timeout: 30 }) expect(client.config.timeout).to eq(30) end end diff --git a/spec/helper_methods_spec.rb b/spec/helper_methods_spec.rb index 29728e87..593948b0 100644 --- a/spec/helper_methods_spec.rb +++ b/spec/helper_methods_spec.rb @@ -3,7 +3,7 @@ describe 'shared helper methods' do describe '#parse_date' do - let(:model_instance) {XeroRuby::Accounting::Account.new} + let(:model_instance) { XeroRuby::Accounting::Account.new } it 'can parse from ms date with backslash' do datestring = "\/Date(1519851689297+0000)\/" @@ -85,11 +85,11 @@ describe '#attributes' do it 'can serialize invoice attributes into a snake_case hash' do - expect(invoice.to_attributes).to eq({currency_code: "USD", has_attachments: false, has_errors: false, invoice_number: "abc-123", total_discount: 100, type: "ACCPAY"}) + expect(invoice.to_attributes).to eq({ currency_code: "USD", has_attachments: false, has_errors: false, invoice_number: "abc-123", total_discount: 100, type: "ACCPAY" }) end it 'can serialize contact attributes into a snake_case hash' do - expect(contact.to_attributes).to eq({account_number: "abc-123", contact_status: "ACTIVE", email_address: "email@gmail.com", first_name: 'Contact', has_attachments: false, has_validation_errors: false, last_name: "Name", name: "Contact Name"}) + expect(contact.to_attributes).to eq({ account_number: "abc-123", contact_status: "ACTIVE", email_address: "email@gmail.com", first_name: 'Contact', has_attachments: false, has_validation_errors: false, last_name: "Name", name: "Contact Name" }) end it 'can serialize nested attributes into a snake_case hash' do diff --git a/spec/where_spec.rb b/spec/where_spec.rb index e1b9baeb..8987f4ce 100644 --- a/spec/where_spec.rb +++ b/spec/where_spec.rb @@ -69,13 +69,13 @@ end describe "operators starting with a dot" do - let(:opts) { {a: '.Test("Hello")' } } + let(:opts) { { a: '.Test("Hello")' } } it { is_expected.to eq %{A.Test("Hello")} } end describe "unmatched operators" do - let(:opts) { {a: 'something unusual'} } + let(:opts) { { a: 'something unusual' } } it { is_expected.to eq %{A something unusual} } end From 8313032ff44dffa098a5e250b6311f3bcfd105d3 Mon Sep 17 00:00:00 2001 From: Vignesh Kennadi Date: Thu, 5 Sep 2024 15:58:36 +0530 Subject: [PATCH 2/2] fixed review comments --- .rubocop.yml | 2 +- lib/xero-ruby/api_client.rb | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5dc960b5..d2ab95d2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -140,7 +140,7 @@ Lint/RequireParentheses: Enabled: true Style/RedundantReturn: - Enabled: true + Enabled: false AllowMultipleReturnValues: true Style/Semicolon: diff --git a/lib/xero-ruby/api_client.rb b/lib/xero-ruby/api_client.rb index 8a836527..dc9d679a 100644 --- a/lib/xero-ruby/api_client.rb +++ b/lib/xero-ruby/api_client.rb @@ -143,7 +143,7 @@ def set_token_set(token_set) set_access_token(token_set[:access_token]) if token_set[:access_token] set_id_token(token_set[:id_token]) if token_set[:id_token] - true + return true end def set_access_token(access_token) @@ -160,7 +160,7 @@ def get_client_credentials_token } token_set = token_request(data, '/token') - token_set + return token_set end def get_token_set_from_callback(params) @@ -173,7 +173,7 @@ def get_token_set_from_callback(params) validate_tokens(token_set) validate_state(params) - token_set + return token_set end def validate_tokens(token_set) @@ -184,14 +184,14 @@ def validate_tokens(token_set) decode_jwt(access_token) if access_token decode_jwt(id_token) if id_token end - true + return true end def validate_state(params) if params['state'] != @state raise StandardError.new "WARNING: @config.state: #{@state} and OAuth callback state: #{params['state']} do not match!" end - true + return true end def decode_jwt(tkn, verify = true) @@ -220,7 +220,7 @@ def refresh_token_set(token_set) grant_type: 'refresh_token', refresh_token: token_set[:refresh_token] } - token_request(data, '/token') + return token_request(data, '/token') end def revoke_token(token_set) @@ -228,7 +228,7 @@ def revoke_token(token_set) data = { token: token_set[:refresh_token] } - token_request(data, '/revocation') + return token_request(data, '/revocation') end def token_request(data, path) @@ -245,7 +245,7 @@ def token_request(data, path) else body = {} end - body + return body end # Connection heplers