Skip to content

Commit

Permalink
Merge branch 'release/5.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Aug 2, 2019
2 parents 152c5dd + b572405 commit cda1ed7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

## 5.1.0 (August 2, 2019)
- Moved filestack_js_url to configuration to not valid it on every page load

## 5.0.0 (August 1, 2019)
- Improve choosing filestack-js version

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
5.0.0
5.1.0
40 changes: 37 additions & 3 deletions lib/filestack_rails/configuration.rb
@@ -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
@@ -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
2 changes: 1 addition & 1 deletion lib/filestack_rails/version.rb
@@ -1,3 +1,3 @@
module FilestackRails
VERSION = '5.0.0'
VERSION = '5.1.0'
end
13 changes: 13 additions & 0 deletions spec/lib/configuration_spec.rb
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
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

0 comments on commit cda1ed7

Please sign in to comment.