-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from masutaka/settings-class
Refactor creating Github::Nippou::Settings class
- Loading branch information
Showing
6 changed files
with
194 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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 | ||
yaml_data = | ||
case | ||
when ENV['GITHUB_NIPPOU_SETTINGS'] | ||
ENV['GITHUB_NIPPOU_SETTINGS'].chomp | ||
when !`git config github-nippou.settings`.chomp.empty? | ||
`git config github-nippou.settings`.chomp | ||
when !`git config github-nippou.settings-gist-id`.chomp.empty? | ||
gist_id = `git config github-nippou.settings-gist-id`.chomp | ||
gist = client.gist(gist_id) | ||
gist[:files][:'settings.yml'][:content] | ||
end | ||
|
||
if yaml_data | ||
YAML.load(yaml_data).deep_symbolize_keys | ||
else | ||
YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys | ||
end | ||
rescue Psych::SyntaxError | ||
puts <<~MESSAGE | ||
** YAML syntax error. | ||
#{$!.message} | ||
#{yaml_data} | ||
MESSAGE | ||
raise $! | ||
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
describe Github::Nippou::Settings do | ||
let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } | ||
let(:settings) { described_class.new(client: client) } | ||
|
||
describe '#format' do | ||
context 'given valid settings' do | ||
let(:settings_format) do | ||
{ | ||
format: { | ||
subject: '### %{subject}', | ||
line: '* [%{title}](%{url}) by %{user} %{status}', | ||
}, | ||
} | ||
end | ||
|
||
before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml } | ||
|
||
it 'is valid `subject`' do | ||
expect(settings.format.subject).to eq '### %{subject}' | ||
end | ||
|
||
it 'is valid `line`' do | ||
expect(settings.format.line).to eq '* [%{title}](%{url}) by %{user} %{status}' | ||
end | ||
end | ||
|
||
context 'given invalid settings' do | ||
let(:settings_format_yaml) do | ||
<<~INVALID_YAML | ||
format: | ||
**!!invalid!!** | ||
line: '* [%{title}](%{url}) by %{user} %{status}' | ||
INVALID_YAML | ||
end | ||
|
||
before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format_yaml } | ||
|
||
it 'outputs YAML syntax error message' do | ||
expect { settings.format }.to raise_error Psych::SyntaxError | ||
end | ||
end | ||
end | ||
|
||
describe '#dictionary' do | ||
context 'given valid settings' do | ||
let(:settings_dictionary) do | ||
{ | ||
dictionary: { | ||
status: { | ||
merged: '**merged!**', | ||
closed: '**closed!**', | ||
}, | ||
}, | ||
} | ||
end | ||
|
||
before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_dictionary.to_yaml } | ||
|
||
it 'is valid `status.merged`' do | ||
expect(settings.dictionary.status.merged).to eq '**merged!**' | ||
end | ||
|
||
it 'is valid `status.closed`' do | ||
expect(settings.dictionary.status.closed).to eq '**closed!**' | ||
end | ||
end | ||
end | ||
|
||
describe '#yaml' do | ||
context 'given valid settings' do | ||
let(:settings_yaml) do | ||
<<~VALID_YAML | ||
format: | ||
subject: "### %{subject}" | ||
line: "* [%{title}](%{url}) by %{user} %{status}" | ||
dictionary: | ||
status: | ||
merged: "**merged!**" | ||
closed: "**closed!**" | ||
VALID_YAML | ||
end | ||
|
||
before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_yaml } | ||
|
||
it 'is valid yaml' do | ||
expect(settings.yaml).to eq <<~VALID_YAML | ||
--- | ||
:format: | ||
:subject: "### %{subject}" | ||
:line: "* [%{title}](%{url}) by %{user} %{status}" | ||
:dictionary: | ||
:status: | ||
:merged: "**merged!**" | ||
:closed: "**closed!**" | ||
VALID_YAML | ||
end | ||
end | ||
end | ||
end |