From f9112f47a19fde66d39b292885fb4e972ba1e9f0 Mon Sep 17 00:00:00 2001 From: Gabi Date: Fri, 6 Apr 2018 08:42:00 +0200 Subject: [PATCH 1/2] Added custom cname option --- README.md | 42 ++++++++++++------- .../filestack_rails/application_helper.rb | 21 ++++++---- lib/filestack_rails/configuration.rb | 4 +- spec/lib/configuration_spec.rb | 12 ++++++ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d81a915..bbbd19d 100644 --- a/README.md +++ b/README.md @@ -35,18 +35,28 @@ Add the Filestack File Picker and initialization script to your layout: ```erb <%= filestack_js_include_tag %> -<%= filestack_js_init_tag %> +<%= filestack_js_init_tag %> ``` -Please note: the scripts need to be added before your application's custom scripts, e.g. before any scripts in your assets folder, if you need access the Filestack client in your own Javascript. +Please note: the scripts need to be added before your application's custom scripts, e.g. before any scripts in your assets folder, if you need access the Filestack client in your own Javascript. Set your API key and client name in `config/application.rb`: ```ruby -config.filestack_rails.api_key = "Your Filestack API Key" -config.filestack_rails.client_name = "custom_client_name" +config.filestack_rails.api_key = 'Your Filestack API Key' +config.filestack_rails.client_name = 'custom_client_name' +``` +The client name defaults to "filestack_client" and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. For more information, please see our [File Picker documenation](https://www.filestack.com/docs/javascript-api/pick-v3). + +### 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. + +Set your CNAME in `config/application.rb`: + +```ruby +config.filestack_rails.cname = 'custom_cname' ``` -The client name defaults to "filestack_client" and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. For more information, please see our [File Picker documenation](https://www.filestack.com/docs/javascript-api/pick-v3). ### Security @@ -68,19 +78,19 @@ You can access the generated policy and signature anytime by calling their attri puts config.filestack_rails.security.policy puts config.filestack_rails.security.signature ``` -You can also generate a new security object at any time, although this will only affect the filestack_image tag, and not the File Picker client. +You can also generate a new security object at any time, although this will only affect the filestack_image tag, and not the File Picker client. ## Usage -Filestack::Rails provides three main functionalities: +Filestack::Rails provides three main functionalities: ### Filestack Upload Button This is a generic button that can be added anywhere in your application and opens an instance of the File Picker. Once a user has chosen a file(s) and submitted, a callback will be executed, passing in the results. You can also pass in any options for the File Picker using the pickerOptions symbol: ```erb -<%= filestack_picker_element 'button test', 'callbackForButton', id: 'someuniequeid', pickerOptions: { 'fromSources' => 'facebook' } %> +<%= filestack_picker_element 'button test', 'callbackForButton', id: 'someuniqueid', input_id: 'someuniqueinputid', pickerOptions: { 'fromSources' => 'facebook' } %> ``` -File Picker options are exactly the same as in the Javscript SDK and can be found in the aforementioned documentation. +File Picker options are exactly the same as in the Javscript SDK and can be found in the aforementioned documentation. The callback can be either the name of a function you've defined in your main Javascript or it can be any code that is immediately executable, e.g. "console.log" or "(function(data){console.log(data)})". The callback should take in a response array as its only argument, which has the following structure: @@ -116,7 +126,7 @@ The form helper wraps the generic Pick element and adds the value of the returne ```erb <%= form_for @user do |f| %>
- <%= f.filestack_field :filepicker_url, 'Upload Your Avatar!', pickerOptions: {'fromSources': 'facebook'}, id: 'unique-id' %> + <%= f.filestack_field :filepicker_url, 'Upload Your Avatar!', pickerOptions: {'fromSources': 'facebook'}, id: 'unique-id', input_id: 'unique-input-id' %>
<%= f.submit %> @@ -129,16 +139,16 @@ Filestack::Rails now has access to the full list of image transforms through our <%= filestack_image @user.filepicker_url, transform: filestack_transform.resize(width:100, height:100).flip.enhance %> ``` ## Migrating from 2.x to 3.x -Filestack::Rails 3.x is a significant and breaking change. Users wishing to upgrade will need to change their current implementation in order to use the plugin correctly. +Filestack::Rails 3.x is a significant and breaking change. Users wishing to upgrade will need to change their current implementation in order to use the plugin correctly. ### Javascript-based File Picker The v3 File Picker is a Javascript application that lives on the client-side of your application. This means you have greater control and access to when it is called, access to the rest of the web SDK, as well as being able to pass callbacks executed once uploads have completed. You must keep in mind the File Picker client lives in global scope and adjust your namespaces accordingly, although you can also change the name of the client, as detailed in the above sections. ### Form Helper -The form helper's call remains essentially the same, except that it now takes as its argument the value of the button element displayed on the page. +The form helper's call remains essentially the same, except that it now takes as its argument the value of the button element displayed on the page. ```erb <%= f.filestack_field :filestack_url, 'Pick Your Avatar' > ``` ### Save Button -As user saving/downloading is not currently supported in the v3 File Picker, that functionality has been removed from Filestack::Rails for the time being. +As user saving/downloading is not currently supported in the v3 File Picker, that functionality has been removed from Filestack::Rails for the time being. ### Transformations The filestack_image tag wraps the generic Rails image_tag and generates a new URL with use of the Ruby SDK. This provides the entire scope of the possible transformations through Filestack's transformation engine, minus those which do not return an image (like debug, av_convert, and so forth). Defining transformations is as simple as chaining them together using the filestack_transform method: ```erb @@ -148,11 +158,11 @@ The filestack_image tag wraps the generic Rails image_tag and generates a new UR Filestack::Rails injects the Filestack Ruby SDK into your application for use anywhere. You can use it to access the rest of the Filestack API and find its documentation [here](https://github.com/filestack/filestack-ruby) ## Demo -To see the Filestack::Rails plugin in action, clone this repository and run the demo app by following these instructions (will only work in Rails 5.x): +To see the Filestack::Rails plugin in action, clone this repository and run the demo app by following these instructions (will only work in Rails 5.x): ### Set API key -Go to ```spec/dummy/config/application.rb``` and change the API key to your own. +Go to ```spec/dummy/config/application.rb``` and change the API key to your own. ### Install Dependencies @@ -199,4 +209,4 @@ Thank you to all the [contributors](https://github.com/filepicker/filestack-rail [code_climate]: https://codeclimate.com/github/filestack/filestack-rails [code_climate_badge]: https://codeclimate.com/github/filestack/filestack-rails.png [coveralls]: https://coveralls.io/github/filestack/filestack-rails?branch=master -[coveralls_badge]: https://coveralls.io/repos/github/filestack/filestack-rails/badge.svg?branch=master \ No newline at end of file +[coveralls_badge]: https://coveralls.io/repos/github/filestack/filestack-rails/badge.svg?branch=master diff --git a/app/helpers/filestack_rails/application_helper.rb b/app/helpers/filestack_rails/application_helper.rb index 7f1bb06..4ebace2 100644 --- a/app/helpers/filestack_rails/application_helper.rb +++ b/app/helpers/filestack_rails/application_helper.rb @@ -10,13 +10,8 @@ def filestack_js_include_tag def filestack_js_init_tag client_name, apikey = get_client_and_api_key - signature, policy = get_policy_and_signature - javascript_string = if policy && signature - "var #{client_name} = filestack.init('#{apikey}'," \ - "{'signature': '#{signature}', 'policy': '#{policy}'});" - else - "var #{client_name} = filestack.init('#{apikey}');" - end + signature_and_policy = get_policy_and_signature_string + javascript_string = "var #{client_name} = filestack.init('#{apikey}', #{signature_and_policy}, '#{cname}');" javascript_tag javascript_string end @@ -26,7 +21,7 @@ def filestack_picker_element(content, callback, options = {}) options[:onclick] = create_javascript_for_picker(callback, picker_options) options[:type] = 'button' button_tag content, options - end + end def filestack_transform _, apikey = get_client_and_api_key @@ -46,6 +41,10 @@ def filestack_image(url, options = {}) private + def cname + ::Rails.application.config.filestack_rails.cname + end + def create_javascript_for_picker(callback, options) client_name, = get_client_and_api_key json_string = if options.nil? @@ -74,5 +73,11 @@ def get_policy_and_signature end return [signature, policy] end + + def get_policy_and_signature_string + signature, policy = get_policy_and_signature + return "{'signature': '#{signature}', 'policy': '#{policy}'}" if policy && signature + return "''" + end end end diff --git a/lib/filestack_rails/configuration.rb b/lib/filestack_rails/configuration.rb index 1edd780..74d23a5 100644 --- a/lib/filestack_rails/configuration.rb +++ b/lib/filestack_rails/configuration.rb @@ -1,6 +1,6 @@ module FilestackRails class Configuration - attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret + attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret, :cname def api_key @api_key or raise "Set config.filepicker_rails.api_key" @@ -22,4 +22,4 @@ def security=(security_options = {}) end end -end \ No newline at end of file +end diff --git a/spec/lib/configuration_spec.rb b/spec/lib/configuration_spec.rb index ad43db0..af0662d 100644 --- a/spec/lib/configuration_spec.rb +++ b/spec/lib/configuration_spec.rb @@ -36,4 +36,16 @@ expect(configuration.security.signature) end end + + describe '#cname' do + it 'has no cname' do + expect(configuration.cname).to be(nil) + end + + it 'has cname' do + cname = 'fs.mycname.com' + configuration.cname = cname + expect(configuration.cname).to eq cname + end + end end From 59156da46028e88c8d354bdc479d88e040a5aa3f Mon Sep 17 00:00:00 2001 From: Gabi Date: Fri, 6 Apr 2018 11:11:36 +0200 Subject: [PATCH 2/2] Changed Rails SDK readme --- README.md | 88 ++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index bbbd19d..c512df7 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,30 @@ -[![Travis CI][travis_ci_badge]][travis_ci] -[![Coveralls][coveralls_badge]][coveralls] -[![Code Climate][code_climate_badge]][code_climate] +

+

Filestack::Rails SDK

+

+ + + + + + +

+

+Rails SDK for Filestack API and content management system. +

-# Filestack::Rails - -This is the official Rails plugin for Filestack - API and content management system that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application. +**Important: This is the readme for 3.1.0+.** -## Resources +Note that the [Filestack::Ruby](https://github.com/filestack/filestack-ruby/) dependency has been updated to no longer interfere with namespace. However, if you were using that dependency in your Rails application, you will need to change any `Client` and `Filelink` class declarations to `FilestackClient` and `FilestackFilelink`, as per documented [here](https://github.com/filestack/filestack-ruby/blob/master/README.md). -* [Filestack](https://www.filestack.com) -* [Documentation](https://www.filestack.com/docs) -* [API Reference](https://filestack.github.io/) +## Overview -## IMPORTANT -Users of 3.0.0 wishing to upgrade to 3.1.0+ should note that the Filestack::Ruby dependency has been updated to no longer interfere with namespace. However, if you were using that dependency in your Rails app, you will need to change any Client and Filelink class declarations to FilestackClient and FilestackFilelink, as per documented [here](https://github.com/filestack/filestack-ruby/blob/master/README.md) +* A multi-part uploader powered on the backend by the [Filestack CIN](https://www.filestack.com/products/content-ingestion-network). +* An interface to the [Filestack Processing Engine](https://www.filestack.com/docs/image-transformations) for transforming assets via URLs. +* The Filestack Picker - an upload widget for the web that integrates over a dozen cloud providers and provides pre-upload image editing. -## Installing +## Installation -Add this line to your application's Gemfile: +Add this line to your application's `Gemfile`: ```ruby gem 'filestack-rails' @@ -37,8 +44,7 @@ Add the Filestack File Picker and initialization script to your layout: <%= filestack_js_include_tag %> <%= filestack_js_init_tag %> ``` - -Please note: the scripts need to be added before your application's custom scripts, e.g. before any scripts in your assets folder, if you need access the Filestack client in your own Javascript. +**Please note:** The scripts need to be added before your application's custom scripts, e.g. before any scripts in your assets folder, if you need access the Filestack client in your own Javascript. Set your API key and client name in `config/application.rb`: @@ -46,7 +52,7 @@ Set your API key and client name in `config/application.rb`: config.filestack_rails.api_key = 'Your Filestack API Key' config.filestack_rails.client_name = 'custom_client_name' ``` -The client name defaults to "filestack_client" and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. For more information, please see our [File Picker documenation](https://www.filestack.com/docs/javascript-api/pick-v3). +The client name defaults to `"filestack_client"` and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. ### CNAME @@ -60,21 +66,23 @@ config.filestack_rails.cname = 'custom_cname' ### Security -If your account has security enabled, then you must initialize the File Picker with a signature and policy. This is easily enabled through the configuration options by setting your application secret and security options: +If your account has security enabled, then you must initialize the File Picker with a signature and policy. -```erb +Set up your application secret and security options in `config/application.rb`: + +```ruby config.filestack_rails.app_secret = 'YOUR_APP_SECRET' config.filestack_rails.security = {'call' => %w[pick store read convert] } ``` -If you set security to an empty object like so -```erb +If you set security to an empty object like so: +```ruby config.filestack_rails.security = {} ``` -it will provide a policy and signature with only an expiry setting (this defaults to one hour). +It will provide a policy and signature with only an expiry setting (this defaults to one hour). You can access the generated policy and signature anytime by calling their attributes on the created security object: -```erb +```ruby puts config.filestack_rails.security.policy puts config.filestack_rails.security.signature ``` @@ -82,17 +90,15 @@ You can also generate a new security object at any time, although this will only ## Usage -Filestack::Rails provides three main functionalities: - ### Filestack Upload Button -This is a generic button that can be added anywhere in your application and opens an instance of the File Picker. Once a user has chosen a file(s) and submitted, a callback will be executed, passing in the results. You can also pass in any options for the File Picker using the pickerOptions symbol: +This is a generic button that can be added anywhere in your application and opens an instance of the File Picker. Once a user has chosen a file(s) and submitted, a callback will be executed, passing in the results. You can also pass in any options for the File Picker using the `pickerOptions` symbol: ```erb <%= filestack_picker_element 'button test', 'callbackForButton', id: 'someuniqueid', input_id: 'someuniqueinputid', pickerOptions: { 'fromSources' => 'facebook' } %> ``` File Picker options are exactly the same as in the Javscript SDK and can be found in the aforementioned documentation. -The callback can be either the name of a function you've defined in your main Javascript or it can be any code that is immediately executable, e.g. "console.log" or "(function(data){console.log(data)})". The callback should take in a response array as its only argument, which has the following structure: +The callback can be either the name of a function you've defined in your main Javascript or it can be any code that is immediately executable, e.g. `console.log` or `(function(data){console.log(data)})`. The callback should take in a response array as its only argument, which has the following structure: ```javascript { @@ -133,15 +139,17 @@ The form helper wraps the generic Pick element and adds the value of the returne <% end %> ``` ### Displaying an image with Filestack Transformations: -Filestack::Rails now has access to the full list of image transforms through our custom Transformation Engine. This functionality is provided by the Filestack Ruby SDK and acts as a small wrapper around it. The filestack_image tag accepts the same options as the genric Rails image_tag, with the addition of a transform option, which accepts a filestack_transform chain: +Filestack::Rails now has access to the full list of image transforms through our custom Transformation Engine. This functionality is provided by the Filestack Ruby SDK and acts as a small wrapper around it. The `filestack_image` tag accepts the same options as the genric Rails `image_tag`, with the addition of a transform option, which accepts a `filestack_transform` chain: ```erb <%= filestack_image @user.filepicker_url, transform: filestack_transform.resize(width:100, height:100).flip.enhance %> ``` ## Migrating from 2.x to 3.x Filestack::Rails 3.x is a significant and breaking change. Users wishing to upgrade will need to change their current implementation in order to use the plugin correctly. + ### Javascript-based File Picker The v3 File Picker is a Javascript application that lives on the client-side of your application. This means you have greater control and access to when it is called, access to the rest of the web SDK, as well as being able to pass callbacks executed once uploads have completed. You must keep in mind the File Picker client lives in global scope and adjust your namespaces accordingly, although you can also change the name of the client, as detailed in the above sections. + ### Form Helper The form helper's call remains essentially the same, except that it now takes as its argument the value of the button element displayed on the page. ```erb @@ -149,13 +157,16 @@ The form helper's call remains essentially the same, except that it now takes as ``` ### Save Button As user saving/downloading is not currently supported in the v3 File Picker, that functionality has been removed from Filestack::Rails for the time being. + ### Transformations -The filestack_image tag wraps the generic Rails image_tag and generates a new URL with use of the Ruby SDK. This provides the entire scope of the possible transformations through Filestack's transformation engine, minus those which do not return an image (like debug, av_convert, and so forth). Defining transformations is as simple as chaining them together using the filestack_transform method: +The `filestack_image` tag wraps the generic Rails `image_tag` and generates a new URL with use of the Ruby SDK. This provides the entire scope of the possible transformations through Filestack's transformation engine, minus those which do not return an image (like debug, av_convert, and so forth). Defining transformations is as simple as chaining them together using the `filestack_transform` method: ```erb <%= @user.filestack_url, transform: filestack_transform.resize(width:100, height:100).enhance %> ``` + ### Ruby SDK -Filestack::Rails injects the Filestack Ruby SDK into your application for use anywhere. You can use it to access the rest of the Filestack API and find its documentation [here](https://github.com/filestack/filestack-ruby) +Filestack::Rails injects the Filestack Ruby SDK into your application for use anywhere. You can use it to access the rest of the Filestack API and find its documentation [here](https://github.com/filestack/filestack-ruby). + ## Demo To see the Filestack::Rails plugin in action, clone this repository and run the demo app by following these instructions (will only work in Rails 5.x): @@ -193,20 +204,3 @@ Filestack::Rails follows the [Semantic Versioning](http://semver.org/). ## Issues If you have problems, please create a [Github Issue](https://github.com/filepicker/filestack-rails/issues). - -## Contributing - -Please see [CONTRIBUTING.md](https://github.com/filepicker/filestack-rails/CONTRIBUTING.md) for details. - -## Credits - -Thank you to all the [contributors](https://github.com/filepicker/filestack-rails/graphs/contributors). - -[gem_version_badge]: https://badge.fury.io/rb/filestack-rails.svg -[ruby_gems]: http://rubygems.org/gems/filestack-rails -[travis_ci]: http://travis-ci.org/filestack/filestack-rails -[travis_ci_badge]: https://travis-ci.org/filestack/filestack-rails.svg?branch=master -[code_climate]: https://codeclimate.com/github/filestack/filestack-rails -[code_climate_badge]: https://codeclimate.com/github/filestack/filestack-rails.png -[coveralls]: https://coveralls.io/github/filestack/filestack-rails?branch=master -[coveralls_badge]: https://coveralls.io/repos/github/filestack/filestack-rails/badge.svg?branch=master