Skip to content

Commit

Permalink
Merge pull request #24 from DannyBen/add/file-permissions
Browse files Browse the repository at this point in the history
Add ability to set cache file permissions
  • Loading branch information
DannyBen authored May 19, 2023
2 parents bc172f3 + dba93c1 commit f617aef
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 23 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: { ruby: ['2.6', '2.7', '3.0', '3.1', head] }
matrix: { ruby: ['3.0', '3.1', '3.2', head] }

steps:
- name: Checkout code
Expand All @@ -25,5 +25,11 @@ jobs:
ruby-version: '${{ matrix.ruby }}'
bundler-cache: true

# this step is added since the public httpbin.prg is sometimes overloaded
- name: Start the httpbin test server
run: docker run -it --rm -d -p 3000:80 --name httpbin kennethreitz/httpbin

- name: Run tests
run: bundle exec rspec
env:
HTTPBIN_HOST: http://localhost:3000
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
--require spec_helper
--color
--format documentation
--fail-fast
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inherit_gem:
- rspec.yml

AllCops:
TargetRubyVersion: 2.6.0
TargetRubyVersion: 3.0
Exclude:
- 'debug.rb'
- 'dev/**/*'
Expand All @@ -25,3 +25,7 @@ Security/MarshalLoad:
# Allow non standard spec file name
RSpec/FilePath:
Enabled: false

# Alerts by this cop are irrelevant
RSpec/Rails:
Enabled: false
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ puts response.base_uri # => "http://example.com/"
```

By default, the cached objects are stored in the `./cache` directory, and
expire after 60 minutes. The cache directory will be created as needed.
expire after 60 minutes. The cache directory will be created as needed, and
the permissions of the cached files can be specified if needed.

You can change these settings on initialization:

```ruby
cache = WebCache.new dir: 'tmp/my_cache', life: '3d'
cache = WebCache.new dir: 'tmp/my_cache', life: '3d', permissions: 0o640
response = cache.get 'http://example.com'
```

Expand All @@ -74,6 +75,7 @@ Or later:
cache = WebCache.new
cache.dir = 'tmp/my_cache'
cache.life = '4h'
cache.permissions = 0o640
response = cache.get 'http://example.com'
```

Expand Down
8 changes: 6 additions & 2 deletions lib/webcache/cache_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

class WebCache
module CacheOperations
attr_accessor :permissions
attr_reader :last_error, :user, :pass, :auth
attr_writer :dir

def initialize(dir: 'cache', life: '1h', auth: nil)
def initialize(dir: 'cache', life: '1h', auth: nil, permissions: nil)
@dir = dir
@life = life_to_seconds life
@enabled = true
@auth = convert_auth auth
@permissions = permissions
end

def get(url, force: false)
Expand Down Expand Up @@ -85,7 +87,9 @@ def load_file_content(path)

def save_file_content(path, response)
FileUtils.mkdir_p dir
File.binwrite path, Marshal.dump(response)
File.open path, 'wb', permissions do |file|
file.write Marshal.dump(response)
end
end

def http_get(url)
Expand Down
7 changes: 0 additions & 7 deletions spec/README.md

This file was deleted.

4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
require 'rubygems'
require 'bundler'
Bundler.require :default, :development

def httpbin_host
ENV['HTTPBIN_HOST'] || 'https://httpbin.org'
end
2 changes: 0 additions & 2 deletions spec/webcache/response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

describe WebCache::Response do
describe '#new' do
context 'with hash' do
Expand Down
2 changes: 0 additions & 2 deletions spec/webcache/web_cache_eigenclass_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

describe WebCache do
subject { described_class }

Expand Down
35 changes: 30 additions & 5 deletions spec/webcache/web_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'spec_helper'

describe WebCache do
let(:url) { 'http://example.com' }

Expand All @@ -12,11 +10,15 @@
end

context 'with arguments' do
subject { described_class.new dir: 'store', life: 120 }
subject { described_class.new dir: 'store', life: 120, auth: auth }

let(:auth) { { user: 'user', pass: 's3cr3t' } }

it 'sets its properties' do
expect(subject.life).to eq 120
expect(subject.dir).to eq 'store'
expect(subject.user).to eq auth[:user]
expect(subject.pass).to eq auth[:pass]
end
end
end
Expand Down Expand Up @@ -47,6 +49,22 @@
expect(response.content.length).to be > 500
end

context 'with file permissions' do
before do
subject.permissions = 0o600
FileUtils.rm_f tmp_path
end

let(:tmp_path) { '/tmp/webcache-test-file' }
let(:file_mode) { File.stat(tmp_path).mode & 0o777 }

it 'chmods the cache file after saving' do
allow(subject).to receive(:get_path).with(url).and_return tmp_path
subject.get url
expect(file_mode).to eq 0o600
end
end

context 'with force: true' do
it 'always downloads a fresh copy' do
subject.get url
Expand Down Expand Up @@ -135,7 +153,7 @@
end

context 'with basic authentication' do
let(:response) { subject.get 'https://httpbin.org/basic-auth/user/pass' }
let(:response) { subject.get "#{httpbin_host}/basic-auth/user/pass" }

context 'when the credentials are valid' do
before { subject.auth = { user: 'user', pass: 'pass' } }
Expand All @@ -158,7 +176,7 @@
end

context 'with other authentication header' do
let(:response) { subject.get 'https://httpbin.org/bearer' }
let(:response) { subject.get "#{httpbin_host}/bearer" }

before { subject.auth = 'Bearer t0k3n' }

Expand Down Expand Up @@ -252,4 +270,11 @@
expect(subject.life).to eq 11 * 60 * 60 * 24
end
end

describe '#permissions=' do
it 'sets file permissions' do
subject.permissions = 0o600
expect(subject.permissions).to eq 0o600
end
end
end
2 changes: 1 addition & 1 deletion webcache.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.files = Dir['README.md', 'lib/**/*.*']
s.homepage = 'https://github.com/DannyBen/webcache'
s.license = 'MIT'
s.required_ruby_version = '>= 2.6.0'
s.required_ruby_version = '>= 3.0'

s.add_runtime_dependency 'http', '~> 5.0'
s.metadata['rubygems_mfa_required'] = 'true'
Expand Down

0 comments on commit f617aef

Please sign in to comment.