Skip to content

Commit 52808ab

Browse files
author
William Tsai
committed
Enforce double quotes for strings
1 parent 03e7b48 commit 52808ab

8 files changed

Lines changed: 83 additions & 65 deletions

File tree

.gitlab-ci.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ include:
66
stages:
77
- test
88
- sast_upload
9+
- lint
910
- rspec
1011
- publish
1112

13+
rubocop:
14+
stage: lint
15+
image: ruby:3.4.8
16+
tags:
17+
- arm64
18+
script:
19+
- bundle install
20+
- bundle exec rubocop
21+
rules:
22+
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
23+
- if: $CI_COMMIT_BRANCH == "master"
24+
1225
rspec:
1326
stage: rspec
1427
image: ruby:3.4.8
@@ -17,8 +30,9 @@ rspec:
1730
script:
1831
- bundle install
1932
- rspec spec
20-
only:
21-
- master
33+
rules:
34+
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
35+
- if: $CI_COMMIT_BRANCH == "master"
2236

2337
to_gem:
2438
stage: publish

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Layout/LineLength:
1515
Style/Documentation:
1616
Enabled: false
1717

18+
Style/StringLiterals:
19+
Enabled: true
20+
EnforcedStyle: double_quotes
21+
1822
Metrics/MethodLength:
1923
Max: 25
2024

Gemfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

3-
source 'https://rubygems.org'
3+
source "https://rubygems.org"
44

55
gemspec
66

77
group :development, :test do
8-
gem 'irb', '~> 1.17.0'
9-
gem 'rspec', '~> 3.13.2'
10-
gem 'rubocop', '~> 1.85.1'
11-
gem 'rubocop-performance', '~> 1.26.1'
8+
gem "irb", "~> 1.17.0"
9+
gem "rspec", "~> 3.13.2"
10+
gem "rubocop", "~> 1.85.1"
11+
gem "rubocop-performance", "~> 1.26.1"
1212
end

error_response.gemspec

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# frozen_string_literal: true
22

33
Gem::Specification.new do |s|
4-
s.name = 'error_response'
5-
s.version = File.read('./VERSION.md')
6-
s.summary = 'A tool for API error response'
7-
s.description = 'use for error_response'
8-
s.authors = ['Kdan Mobile Software Developer']
9-
s.email = 'dev@kdanmobile.com'
10-
s.homepage = 'https://github.com/kdan-mobile-software-ltd/error_response'
11-
s.license = 'MIT'
12-
s.files = Dir['lib/**/*']
13-
s.require_path = ['lib']
14-
s.required_ruby_version = '>= 3.0'
4+
s.name = "error_response"
5+
s.version = File.read("./VERSION.md")
6+
s.summary = "A tool for API error response"
7+
s.description = "use for error_response"
8+
s.authors = ["Kdan Mobile Software Developer"]
9+
s.email = "dev@kdanmobile.com"
10+
s.homepage = "https://github.com/kdan-mobile-software-ltd/error_response"
11+
s.license = "MIT"
12+
s.files = Dir["lib/**/*"]
13+
s.require_path = ["lib"]
14+
s.required_ruby_version = ">= 3.0"
1515
s.metadata = {
16-
'source_code_uri' => 'https://github.com/kdan-mobile-software-ltd/error_response',
17-
'changelog_uri' => 'https://github.com/kdan-mobile-software-ltd/error_response/blob/master/CHANGELOG.md',
18-
'rubygems_mfa_required' => 'true'
16+
"source_code_uri" => "https://github.com/kdan-mobile-software-ltd/error_response",
17+
"changelog_uri" => "https://github.com/kdan-mobile-software-ltd/error_response/blob/master/CHANGELOG.md",
18+
"rubygems_mfa_required" => "true"
1919
}
2020

21-
s.add_dependency 'activesupport', '~> 7.2.3.1'
21+
s.add_dependency "activesupport", "~> 7.2.3.1"
2222
end

lib/error_response.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

3-
require 'yaml'
4-
require 'net/http'
5-
require 'uri'
3+
require "yaml"
4+
require "net/http"
5+
require "uri"
66

7-
require 'error_response/configuration'
8-
require 'error_response/helper'
9-
require 'error_response/request_error'
7+
require "error_response/configuration"
8+
require "error_response/helper"
9+
require "error_response/request_error"
1010

1111
module ErrorResponse
1212
class << self
@@ -27,16 +27,16 @@ def all
2727
def to_hash(key)
2828
return {} unless yaml_hash.key?(key.to_s)
2929

30-
yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
30+
yaml_hash[key.to_s].merge({ "error_key" => key.to_s })
3131
end
3232

3333
def to_api(key, message = nil)
3434
json = deep_dup(yaml_hash)
35-
json = json[key.to_s] || { 'error_code' => 500_000, 'error_message' => key.to_s }
36-
json['error_key'] = key.to_s
37-
json['error_message'] += ": #{message}" unless message.nil?
35+
json = json[key.to_s] || { "error_code" => 500_000, "error_message" => key.to_s }
36+
json["error_key"] = key.to_s
37+
json["error_message"] += ": #{message}" unless message.nil?
3838
{
39-
status: parse_status(json['error_code']),
39+
status: parse_status(json["error_code"]),
4040
json: json
4141
}
4242
end
@@ -47,10 +47,10 @@ def yaml_hash
4747
return @hash unless @hash.nil?
4848

4949
settings = YAML.safe_load_file(configuration.yaml_config_path, permitted_classes: permitted_classes, aliases: true)
50-
local_array = settings['source']['local']
50+
local_array = settings["source"]["local"]
5151
local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.safe_load_file(path, permitted_classes: permitted_classes, aliases: true) }.inject(&:merge)
5252

53-
remote_array = settings['source']['remote']
53+
remote_array = settings["source"]["remote"]
5454
remote_hash = remote_array.nil? ? {} : remote_array.map { |url| build_yaml(url) }.inject(&:merge)
5555

5656
@hash = local_hash.merge(remote_hash)

lib/error_response/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Configuration
55
attr_accessor :yaml_config_path
66

77
def initialize
8-
@yaml_config_path = ENV['YAML_CONFIG_PATH'] || 'config/error_response.yml'
8+
@yaml_config_path = ENV["YAML_CONFIG_PATH"] || "config/error_response.yml"
99
end
1010
end
1111
end

lib/error_response/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'active_support/concern'
3+
require "active_support/concern"
44

55
module ErrorResponse
66
module Helper

spec/error_response_spec.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
# frozen_string_literal: true
22

3-
require './lib/error_response'
3+
require "./lib/error_response"
44

55
RSpec.describe ErrorResponse do
6-
describe '#yaml_hash' do
7-
it 'should fetch data from local' do
6+
describe "#yaml_hash" do
7+
it "should fetch data from local" do
88
result = ErrorResponse.to_api(:resource_not_found)
9-
expect(result[:json]['error_message']).to eq 'resource not found'
10-
expect(result[:json]['error_code']).to eq 404_001
9+
expect(result[:json]["error_message"]).to eq "resource not found"
10+
expect(result[:json]["error_code"]).to eq 404_001
1111
end
1212

13-
it 'should fetch data from remote' do
13+
it "should fetch data from remote" do
1414
result = ErrorResponse.to_api(:bad_request_2)
15-
expect(result[:json]['error_message']).to eq 'bad request 2'
16-
expect(result[:json]['error_code']).to eq 400_002
15+
expect(result[:json]["error_message"]).to eq "bad request 2"
16+
expect(result[:json]["error_code"]).to eq 400_002
1717
end
1818
end
1919

20-
describe '#to_api' do
21-
it 'should return correspond hash when key existed' do
20+
describe "#to_api" do
21+
it "should return correspond hash when key existed" do
2222
hash = {
2323
status: 400,
2424
json: {
25-
'error_code' => 400_001,
26-
'error_message' => 'bad request 1',
27-
'error_key' => 'bad_request_1'
25+
"error_code" => 400_001,
26+
"error_message" => "bad request 1",
27+
"error_key" => "bad_request_1"
2828
}
2929
}
3030
result = ErrorResponse.to_api(:bad_request_1)
3131
expect(result).to eq hash
3232
end
3333

34-
it 'should return personalized hash when key not existed' do
34+
it "should return personalized hash when key not existed" do
3535
hash = {
3636
status: 500,
3737
json: {
38-
'error_code' => 500_000,
39-
'error_message' => 'internal_error: something went wrong',
40-
'error_key' => 'internal_error'
38+
"error_code" => 500_000,
39+
"error_message" => "internal_error: something went wrong",
40+
"error_key" => "internal_error"
4141
}
4242
}
43-
result = ErrorResponse.to_api(:internal_error, 'something went wrong')
43+
result = ErrorResponse.to_api(:internal_error, "something went wrong")
4444
expect(result).to eq hash
4545
end
4646
end
4747

48-
describe '#to_hash' do
49-
it 'should return hash when key existed' do
48+
describe "#to_hash" do
49+
it "should return hash when key existed" do
5050
hash = {
51-
'error_code' => 400_001,
52-
'error_message' => 'bad request 1',
53-
'error_key' => 'bad_request_1'
51+
"error_code" => 400_001,
52+
"error_message" => "bad request 1",
53+
"error_key" => "bad_request_1"
5454
}
5555
result = ErrorResponse.to_hash(:bad_request_1)
5656
expect(result).to eq hash
5757
end
5858

59-
it 'should return hash when key existed in remote source' do
59+
it "should return hash when key existed in remote source" do
6060
hash = {
61-
'error_code' => 400_002,
62-
'error_message' => 'bad request 2',
63-
'error_key' => 'bad_request_2'
61+
"error_code" => 400_002,
62+
"error_message" => "bad request 2",
63+
"error_key" => "bad_request_2"
6464
}
6565
result = ErrorResponse.to_hash(:bad_request_2)
6666
expect(result).to eq hash
6767
end
6868

69-
it 'should return {} when key not existed' do
69+
it "should return {} when key not existed" do
7070
result = ErrorResponse.to_hash(:some_error)
7171
empty_hash = {}
7272
expect(result).to eq empty_hash

0 commit comments

Comments
 (0)