Skip to content

Commit

Permalink
設定ファイルから設定値を読み込む機能の実装 (#6)
Browse files Browse the repository at this point in the history
* フォルダの配置の修正

* Settingsクラスの作成

* 設定値を読み込む機能の実装

* 不要なrequire削除

Co-authored-by: yonetani <[email protected]>
  • Loading branch information
Sibakeny and Sibakeny authored May 14, 2021
1 parent f31236d commit 86e1acc
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 81 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ gemspec

gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"

group :test do
gem "rails", "~> 6.1.0"
gem "generator_spec"
end
128 changes: 128 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,119 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.3.2)
actionpack (= 6.1.3.2)
activesupport (= 6.1.3.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.3.2)
actionpack (= 6.1.3.2)
activejob (= 6.1.3.2)
activerecord (= 6.1.3.2)
activestorage (= 6.1.3.2)
activesupport (= 6.1.3.2)
mail (>= 2.7.1)
actionmailer (6.1.3.2)
actionpack (= 6.1.3.2)
actionview (= 6.1.3.2)
activejob (= 6.1.3.2)
activesupport (= 6.1.3.2)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.3.2)
actionview (= 6.1.3.2)
activesupport (= 6.1.3.2)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.3.2)
actionpack (= 6.1.3.2)
activerecord (= 6.1.3.2)
activestorage (= 6.1.3.2)
activesupport (= 6.1.3.2)
nokogiri (>= 1.8.5)
actionview (6.1.3.2)
activesupport (= 6.1.3.2)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.3.2)
activesupport (= 6.1.3.2)
globalid (>= 0.3.6)
activemodel (6.1.3.2)
activesupport (= 6.1.3.2)
activerecord (6.1.3.2)
activemodel (= 6.1.3.2)
activesupport (= 6.1.3.2)
activestorage (6.1.3.2)
actionpack (= 6.1.3.2)
activejob (= 6.1.3.2)
activerecord (= 6.1.3.2)
activesupport (= 6.1.3.2)
marcel (~> 1.0.0)
mini_mime (~> 1.0.2)
activesupport (6.1.3.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
builder (3.2.4)
concurrent-ruby (1.1.8)
crass (1.0.6)
diff-lcs (1.4.4)
erubi (1.10.0)
generator_spec (0.9.4)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
loofah (2.9.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.1)
method_source (1.0.0)
mini_mime (1.0.3)
minitest (5.14.4)
nio4r (2.5.7)
nokogiri (1.11.3-x86_64-darwin)
racc (~> 1.4)
racc (1.5.2)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.3.2)
actioncable (= 6.1.3.2)
actionmailbox (= 6.1.3.2)
actionmailer (= 6.1.3.2)
actionpack (= 6.1.3.2)
actiontext (= 6.1.3.2)
actionview (= 6.1.3.2)
activejob (= 6.1.3.2)
activemodel (= 6.1.3.2)
activerecord (= 6.1.3.2)
activestorage (= 6.1.3.2)
activesupport (= 6.1.3.2)
bundler (>= 1.15.0)
railties (= 6.1.3.2)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.1.3.2)
actionpack (= 6.1.3.2)
activesupport (= 6.1.3.2)
method_source
rake (>= 0.8.7)
thor (~> 1.0)
rake (12.3.3)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand All @@ -21,11 +133,27 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (1.1.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)

PLATFORMS
ruby

DEPENDENCIES
generator_spec
rails (~> 6.1.0)
rake (~> 12.0)
rspec (~> 3.0)
sp-rails-saml!
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sp::Rails::Saml
# SpRailsSaml

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sp/rails/saml`. To experiment with that code, run `bin/console` for an interactive prompt.
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sp-rails-saml`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

Expand Down
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "sp/rails/saml"
require "sp-rails-saml"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
26 changes: 26 additions & 0 deletions lib/generators/sp-rails-saml/config_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails/generators'

module SpRailsSaml
class ConfigGenerator < Rails::Generators::Base

desc "config/initializersにイニシャライザファイルを作成します"

def create_initializer_file
create_file "config/initializers/sp-rails-saml.rb", default_initializer
end

private

def default_initializer
<<-EOS
SpRailsSaml::Settings.setup do |config|
config.sp_entity_id = ''
config.name_identifier_format = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'
config.authn_context = 'urn:oasis:names:tc:SAML:2.0:ac:classes:X509'
config.authn_context_comparison = 'exact'
config.assertion_consumer_service_url = ''
end
EOS
end
end
end
6 changes: 6 additions & 0 deletions lib/sp-rails-saml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'sp-rails-saml/settings'
require 'generators/sp-rails-saml/config_generator'

module SpRailsSaml
class Error < StandardError; end
end
18 changes: 18 additions & 0 deletions lib/sp-rails-saml/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module SpRailsSaml
class Settings
class << self
attr_accessor :sp_entity_id
attr_accessor :name_identifier_format
attr_accessor :authn_context
attr_accessor :authn_context_comparison
attr_accessor :assertion_consumer_service_url

def setup(options = {})
options.each do |key, value|
instance_variable_set("@#{key}", value)
end
yield(self) if block_given?
end
end
end
end
3 changes: 3 additions & 0 deletions lib/sp-rails-saml/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module SpRailsSaml
VERSION = "0.1.0"
end
11 changes: 0 additions & 11 deletions lib/sp/rails/saml.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/sp/rails/saml/hello.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/sp/rails/saml/version.rb

This file was deleted.

5 changes: 3 additions & 2 deletions sp-rails-saml.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require_relative 'lib/sp/rails/saml/version'
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'sp-rails-saml/version'

Gem::Specification.new do |spec|
spec.name = "sp-rails-saml"
spec.version = Sp::Rails::Saml::VERSION
spec.version = SpRailsSaml::VERSION
spec.authors = ["psyashes"]
spec.email = ["[email protected]"]

Expand Down
24 changes: 24 additions & 0 deletions spec/generators/sp_rails_saml/settings_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe SpRailsSaml::ConfigGenerator, type: :generator do
destination File.expand_path("../../../../tmp", __FILE__)

before(:all) do
prepare_destination
run_generator
end

let(:initializer_text) do
<<-EOS
SpRailsSaml::Settings.setup do |config|
config.sp_entity_id = ''
config.name_identifier_format = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'
config.authn_context = 'urn:oasis:names:tc:SAML:2.0:ac:classes:X509'
config.authn_context_comparison = 'exact'
config.assertion_consumer_service_url = ''
end
EOS
end

it "should create saml_settings initializer file" do
assert_file "config/initializers/sp-rails-saml.rb", initializer_text
end
end
37 changes: 0 additions & 37 deletions spec/setting_spec.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/sp/rails/saml_spec.rb

This file was deleted.

5 changes: 5 additions & 0 deletions spec/sp_rails_saml/saml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.describe SpRailsSaml do
it "has a version number" do
expect(SpRailsSaml::VERSION).not_to be nil
end
end
25 changes: 25 additions & 0 deletions spec/sp_rails_saml/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
RSpec.describe SpRailsSaml::Settings do
describe '#setup' do
let(:sp_entity_id) { 'sp_entity_id' }
let(:name_identifier_format) { 'name_identifier_format' }
let(:authn_context) { 'authn_context' }
let(:authn_context_comparison) { 'authn_context_comparison' }
let(:assertion_consumer_service_url) { 'assertion_consumer_service_url' }

it 'should set settings value' do
SpRailsSaml::Settings.setup do |config|
config.sp_entity_id = sp_entity_id
config.name_identifier_format = name_identifier_format
config.authn_context = authn_context
config.authn_context_comparison = authn_context_comparison
config.assertion_consumer_service_url = assertion_consumer_service_url
end

expect(SpRailsSaml::Settings.sp_entity_id).to eq sp_entity_id
expect(SpRailsSaml::Settings.name_identifier_format).to eq name_identifier_format
expect(SpRailsSaml::Settings.authn_context).to eq authn_context
expect(SpRailsSaml::Settings.authn_context_comparison).to eq authn_context_comparison
expect(SpRailsSaml::Settings.assertion_consumer_service_url).to eq assertion_consumer_service_url
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "bundler/setup"
require "sp/rails/saml"
require "sp-rails-saml"
require "generator_spec"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 86e1acc

Please sign in to comment.