-
Notifications
You must be signed in to change notification settings - Fork 9
/
settings.rb
84 lines (74 loc) · 1.86 KB
/
settings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'json'
require 'ostruct'
module Github
module Nippou
class Settings
def initialize(client:)
@client = client
end
# Getting format settings
#
# @return [OpenStruct]
def format
open_struct(data[:format])
end
# Getting dictionary settings
#
# @return [OpenStruct]
def dictionary
open_struct(data[:dictionary])
end
# Getting settings as YAML format
#
# return [String]
def yaml
data.to_yaml
end
private
attr_reader :client
# Getting settings data as Hash
#
# return [Hash]
def data
@data ||=
begin
if gist_id.present?
gist = client.gist(gist_id)
yaml_data = gist[:files][:'settings.yml'][:content]
YAML.load(yaml_data).deep_symbolize_keys
else
default_yml = File.expand_path('../../../config/settings.yml', __dir__)
YAML.load_file(default_yml).deep_symbolize_keys
end
rescue Psych::SyntaxError
puts <<~MESSAGE
** YAML syntax error.
#{$!.message}
#{yaml_data}
MESSAGE
raise $!
end
end
# Getting gist id which has settings.yml
#
# @return [String] gist id
def gist_id
@gist_id ||=
begin
ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] ||
begin
git_config = `git config github-nippou.settings-gist-id`.chomp
git_config.present? ? git_config : nil
end
end
end
# Cast to OpenStruct
#
# @param hash [Hash]
# @return [OpenStruct]
def open_struct(hash)
JSON.parse(hash.to_json, object_class: OpenStruct)
end
end
end
end