Skip to content

Commit

Permalink
Merge 85c35f4 into 6438ceb
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Aug 2, 2019
2 parents 6438ceb + 85c35f4 commit e13e8d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
38 changes: 35 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,34 @@ def security=(security_options = {})
def app_secret
@app_secret or nil
end

def url
@url ||= filestack_js_url
end

private

def version_exists?
@url = filestack_js_url

if @version == OUTDATED_VERSION
@url = 'https://static.filestackapi.com/v3/filestack.js'
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 => e
raise e
end

def filestack_js_url
"https://static.filestackapi.com/filestack-js/#{@version}/filestack.min.js"
end
end
end
28 changes: 2 additions & 26 deletions lib/filestack_rails/filestack_js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@ 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
@url = ::Rails.application.config.filestack_rails.url
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"
end

def picker(client_name, api_key, options, callback, other_callbacks = nil)
options_string = options[1..-2] # removed curly brackets help to generate pickerOptions in js

Expand All @@ -29,22 +20,9 @@ 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
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 +38,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
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

0 comments on commit e13e8d0

Please sign in to comment.