Skip to content

Commit

Permalink
Merge pull request #60 from giginet/initialize-file-path
Browse files Browse the repository at this point in the history
Add constructor on FileParamStorage
  • Loading branch information
CognitiveDisson authored Jul 18, 2018
2 parents 5535356 + 3f88b62 commit 89945e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/fabricio/authorization/file_param_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ module Fabricio
module Authorization
# Stores default params as organization, app, etc.
class FileParamStorage < AbstractParamStorage
def initialize(path = PARAM_FILE_PATH)
@path = path
end

# Returns all stored variable
#
# @return [Hash]
def obtain
return nil unless File.exist?(PARAM_FILE_PATH)
params = YAML.load_file(PARAM_FILE_PATH)
return nil unless File.exist?(@path)
params = YAML.load_file(@path)
return {} unless params
return params
end
Expand All @@ -29,7 +32,7 @@ def store(hash)

# Resets current state and deletes all saved params
def reset
FileUtils.remove_file(PARAM_FILE_PATH)
FileUtils.remove_file(@path)
end

def organization_id
Expand Down Expand Up @@ -58,7 +61,7 @@ def store_app_id(app_id)

def save_to_file(hash)
FileUtils.mkdir_p(FABRICIO_DIRECTORY_PATH)
File.open(PARAM_FILE_PATH,'w') do |f|
File.open(@path, 'w') do |f|
f.write hash.to_yaml
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/fabricio/authorization/file_param_storage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rspec'
require 'fabricio/authorization/file_session_storage'
require 'fabricio/authorization/file_param_storage'

describe 'FileParamStorage' do
let(:path) { '/path/to/params.yml' }
let(:yaml) do
yaml = <<-YAML
organization_id: my_organization
app_id: my_app
YAML
YAML.load(yaml)
end

before(:each) do
allow(YAML).to receive(:load_file).with(path).and_return(yaml)
allow(File).to receive(:exist?).with(path).and_return(true)
@storage = Fabricio::Authorization::FileParamStorage.new(path)
end

describe '#organization_id' do
it { expect(@storage.organization_id).to eq('my_organization') }
end

describe '#app_id' do
it { expect(@storage.app_id).to eq('my_app') }
end
end

0 comments on commit 89945e0

Please sign in to comment.