-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters