Skip to content

Commit

Permalink
Determine filestack-js version
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Aug 1, 2019
1 parent cecd824 commit 47c23f7
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 57 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ config.filestack_rails.version = 'v2'
# filestack-js (1.x.x)
config.filestack_rails.version = 'v3'
```

For Filestack Rails SDK v.5.0.0+, you have to provide picker version precisely. By default the picker version is setup to `3.x.x`. If you want to use older filestack-js version (0.11.5), you have to configure `version` to `0.11.5` in `config/application.rb`:
```ruby
# filestack-js (0.11.5)
config.filestack_rails.version = '0.11.5'

# filestack-js (1.x.x)
config.filestack_rails.version = '1.x.x'

# filestack-js (3.x.x)
config.filestack_rails.version = '3.x.x'
```

You can find all available filestack-js versions in [`lib/filestack_rails/version.rb`](./lib/filestack_rails/version.rb)

### CNAME

If you have set up a custom CNAME, you can add it to your configuration file. The Picker will modify all assets to formatted with your domain origin instead of Filestack's.
Expand Down
42 changes: 10 additions & 32 deletions app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
require 'json'
include FilestackRails::Transform
include FilestackRails::Version
include FilestackRails::FilestackJs

module FilestackRails
module ApplicationHelper
def filestack_js_include_tag
v2 = -> { javascript_include_tag('https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript') }
v3 = -> { javascript_include_tag('https://static.filestackapi.com/filestack-js/1.x.x/filestack.min.js', type: 'text/javascript') }

get_filestack_js_result(v2: v2, v3: v3)
javascript_include_tag(get_filestack_js.url, type: 'text/javascript')
end

def filestack_js_init_tag
Expand Down Expand Up @@ -49,31 +46,15 @@ def cname
end

def create_javascript_for_picker(callback, options)
client_name, _api_key = get_client_and_api_key
client_name, api_key = get_client_and_api_key
other_callbacks = build_callbacks_js(options) if options
json_string = if options.nil?
''
else
options.to_json
end
v2 = -> do
<<~HTML
(function(){
#{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
})()
HTML
end
options = if options.nil?
''
else
options.to_json
end

v3 = -> do
json_string = json_string[1..-2] # removed curly brackets help to generate pickerOptions in js

<<~HTML
(function(){
#{client_name}.picker({ onUploadDone: data => #{callback}(data)#{other_callbacks}, #{json_string} }).open()
})()
HTML
end
get_filestack_js_result(v2: v2, v3: v3)
get_filestack_js.picker(client_name, api_key, options, callback, other_callbacks)
end

def build_callbacks_js(options)
Expand Down Expand Up @@ -104,10 +85,7 @@ def get_policy_and_signature_string
signature, policy = get_policy_and_signature

if policy && signature
signature_and_policy = { signature: signature, policy: policy }
v2 = -> { signature_and_policy.to_json }
v3 = -> { { security: signature_and_policy }.to_json }
get_filestack_js_result(v2: v2, v3: v3)
get_filestack_js.security(signature, policy)
else
"''"
end
Expand Down
1 change: 1 addition & 0 deletions lib/filestack-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "filestack_rails/transform"
require "filestack_rails/engine"
require "filestack_rails/version"
require "filestack_rails/filestack_js"

module FilestackRails
# Your code goes here...
Expand Down
6 changes: 5 additions & 1 deletion lib/filestack_rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require 'net/http'

module FilestackRails
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 @@ -11,7 +15,7 @@ def client_name
end

def version
@version or 'v3'
@version or '3.x.x'
end

def expiry
Expand Down
73 changes: 73 additions & 0 deletions lib/filestack_rails/filestack_js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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"
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

<<~HTML
(function(){
#{client_name}.picker({ onUploadDone: data => #{callback}(data)#{other_callbacks}, #{options_string} }).open()
})()
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(){
#{client_name}.pick(#{options}).then(function(data){#{callback}(data)})
})()
HTML
end

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

module FilestackRails
module FilestackJs
OUTDATED_VERSION = '0.11.5'

def get_filestack_js
if ::Rails.application.config.filestack_rails.version == OUTDATED_VERSION
OutdatedPicker.new
else
Picker.new
end
end
end
end
21 changes: 0 additions & 21 deletions lib/filestack_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
class FilestackVersion
def initialize(results)
@version = ::Rails.application.config.filestack_rails.version
@results = results
end

def determine_filestack_js_result
begin
@results[@version.to_sym].call
rescue
raise 'Set correct version in config.filestack_rails.version'
end
end
end

module FilestackRails
VERSION = '4.0.7'

module Version
def get_filestack_js_result(results)
FilestackVersion.new(results).determine_filestack_js_result
end
end
end
2 changes: 1 addition & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end

it "has correct src attribute" do
attribute = %{src="https://static.filestackapi.com/filestack-js/1.x.x/filestack.min.js"}
attribute = %{src="https://static.filestackapi.com/filestack-js/3.x.x/filestack.min.js"}
expect(filestack_js_include_tag).to include(attribute)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@

describe '#version' do
it 'has no version' do
expect(configuration.version).to eq('v3')
expect(configuration.version).to eq('3.x.x')
end

it 'has version' do
version = 'v3'
version = '3.x.x'
configuration.version = version
expect(configuration.version).to eq version
end
Expand Down
71 changes: 71 additions & 0 deletions spec/lib/filestack_js_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'rails_helper'

RSpec.describe FilestackRails::FilestackJs do
describe '#get_filestack_js' do
let!(:configuration) { ::Rails.application.config.filestack_rails }
let(:signature) { 'SIGNATURE' }
let(:policy) { 'POLICY' }

context 'when version is outdated' do
before do
configuration.version = '0.11.5'
end

it 'returns filestack-js url' do
expect(get_filestack_js.url).to eq('https://static.filestackapi.com/v3/filestack.js')
end

it 'returns picker' do
picker = get_filestack_js.picker('filestack', 'apikey', 'options', 'callback', nil)
expect(picker).to eq(
<<~HTML
(function(){
filestack.pick(options).then(function(data){callback(data)})
})()
HTML
)
end

it 'returns security' do
security = get_filestack_js.security(signature, policy)
expect(security).to eq({ signature: signature, policy: policy }.to_json)
end
end

context 'when version exists' do
before do
configuration.version = '3.x.x'
end

it 'returns filestack-js url' do
expect(get_filestack_js.url).to eq("https://static.filestackapi.com/filestack-js/3.x.x/filestack.min.js")
end

it 'returns picker' do
picker = get_filestack_js.picker('filestack', 'apikey', '{options}', 'callback', 'other_callbacks')
expect(picker).to eq(
<<~HTML
(function(){
filestack.picker({ onUploadDone: data => callback(data)other_callbacks, options }).open()
})()
HTML
)
end

it 'returns security' do
security = get_filestack_js.security(signature, policy)
expect(security).to eq({ security: { signature: signature, policy: policy } }.to_json)
end
end

context 'when version does not exist' do
before do
configuration.version = '0.6.0'
end

it 'considers incorrect version' do
expect{ get_filestack_js }.to raise_error(RuntimeError, 'Incorrect config.filestack_rails.version')
end
end
end
end

0 comments on commit 47c23f7

Please sign in to comment.