Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved url check to configuration #219

Merged
merged 1 commit into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lib/filestack_rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'net/http'

module FilestackRails
OUTDATED_VERSION = '0.11.5'

class Configuration
attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret, :cname, :version

OUTDATED_VERSION = '0.11.5'

def api_key
@api_key or raise "Set config.filestack_rails.api_key"
end
Expand All @@ -15,7 +15,10 @@ def client_name
end

def version
@version or '3.x.x'
@version ||= '3.x.x'

raise 'Incorrect config.filestack_rails.version' unless version_exists?
@version
end

def expiry
Expand All @@ -32,5 +35,36 @@ def security=(security_options = {})
def app_secret
@app_secret or nil
end

def url
@url
end

def version_exists?
@url = filestack_js_url

if @version == OUTDATED_VERSION
@url = outdated_filestack_js_url
end

url_exists?(@url)
end

def url_exists?(url)
uri = URI(url)
request = Net::HTTP.new(uri.host)
response = request.request_head(uri.path)
response.code.to_i == 200
rescue
raise 'Invalid URI'
end

def outdated_filestack_js_url
'https://static.filestackapi.com/v3/filestack.js'
end

def filestack_js_url
"https://static.filestackapi.com/filestack-js/#{@version}/filestack.min.js"
end
end
end
36 changes: 5 additions & 31 deletions lib/filestack_rails/filestack_js.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
require 'net/http'

class Picker
attr_reader :url

def initialize
raise 'Incorrect config.filestack_rails.version' unless url_exists?(filestack_js_url)
@url = filestack_js_url
end

def version
::Rails.application.config.filestack_rails.version
end

def security(signature, policy)
{ security: { signature: signature, policy: policy } }.to_json
end

def filestack_js_url
"https://static.filestackapi.com/filestack-js/#{version}/filestack.min.js"
@url = ::Rails.application.config.filestack_rails.url
end

def picker(client_name, api_key, options, callback, other_callbacks = nil)
Expand All @@ -30,21 +15,12 @@ def picker(client_name, api_key, options, callback, other_callbacks = nil)
HTML
end

def url_exists?(url)
uri = URI(url)
request = Net::HTTP.new(uri.host)
response = request.request_head(uri.path)
response.code.to_i == 200
rescue
false
end
def security(signature, policy)
{ security: { signature: signature, policy: policy } }.to_json
end
end

class OutdatedPicker < Picker
def filestack_js_url
'https://static.filestackapi.com/v3/filestack.js'
end

def picker(client_name, api_key, options, callback, other_callbacks = nil)
<<~HTML
(function(){
Expand All @@ -60,10 +36,8 @@ def security(signature, policy)

module FilestackRails
module FilestackJs
OUTDATED_VERSION = '0.11.5'

def get_filestack_js
if ::Rails.application.config.filestack_rails.version == OUTDATED_VERSION
if ::Rails.application.config.filestack_rails.version == FilestackRails::OUTDATED_VERSION
OutdatedPicker.new
else
Picker.new
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
expect(configuration.version).to eq('3.x.x')
end

it 'has incorrect version' do
configuration.version = "1.4.4.4"
expect { configuration.version }.to raise_error(RuntimeError, 'Incorrect config.filestack_rails.version')
end

it 'has version' do
version = '3.x.x'
configuration.version = version
Expand Down Expand Up @@ -94,4 +99,12 @@
expect(configuration.app_secret).to eq(nil)
end
end

describe '#url_exists?' do
it 'considers invalid url' do
expect do
configuration.url_exists?('invalid_url')
end.to raise_error(RuntimeError, 'Invalid URI')
end
end
end
4 changes: 0 additions & 4 deletions spec/lib/filestack_js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
security = get_filestack_js.security(signature, policy)
expect(security).to eq({ security: { signature: signature, policy: policy } }.to_json)
end

it 'considers url incorrect' do
expect(get_filestack_js.url_exists?('incorrect_url_format')).to eq(false)
end
end

context 'when version does not exist' do
Expand Down