Skip to content

Commit 7cabfad

Browse files
committed
update Readme file
1 parent 39262f7 commit 7cabfad

File tree

1 file changed

+190
-62
lines changed

1 file changed

+190
-62
lines changed

README.md

Lines changed: 190 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ImageKit gem for Ruby on Rails that allows you to use real-time [image resizing]
1313
Table of contents -
1414
* [Installation](#Installation)
1515
* [Initialization](#Initialization)
16+
- [CarrierWave](#Carrierwave-config)
17+
- [ActiveStorage](#ActiveStorage-config)
1618
* [URL Generation](#URL-generation)
1719
* [File Upload](#File-Upload)
1820
* [File Management](#File-Management)
@@ -45,18 +47,22 @@ $ gem install imagekitio
4547

4648
## Initialization
4749

48-
Add this configuration to `config/environments/development.rb` and `config/environments/production.rb`
49-
50+
Create a new file `config/initializers/imagekitio.rb` then add the configuration.
5051
```ruby
51-
config.imagekit={
52-
private_key: "<your-private-key>",
53-
public_key: "<your-public-key>",
54-
url_endpoint: "<endpoint-url>"
55-
}
52+
ImageKitIo.configure do |config|
53+
if Rails.env.development?
54+
config.public_key = '<your-public-key>'
55+
config.private_key = '<your-private-key>'
56+
config.url_endpoint = '<endpoint-url>'
57+
end
58+
config.service = :carrierwave
59+
#config.service = :active_storage
60+
#config.constants.MISSING_PRIVATE_KEY = 'custom error message'
61+
end
5662
```
57-
63+
#### CarrierWave
5864
You can create a carrierwave uploader in order to attach pictures to your database objects as their attributes. To upload images without designating them as database attributes, skip to [this section](https://github.com/imagekit-developer/imagekit-ruby#file-upload).
59-
65+
(make sure to add service `:carrierwave` as shown in [initialization section](#Initialization))
6066
```bash
6167
rails g uploader <Uploading_attribute_name>
6268
# For example, if you want to create an uploader for Avatar attribute, then use
@@ -66,8 +72,8 @@ rails g uploader Avatar
6672

6773
After that, you need to edit your generated uploader and make the following changes:
6874
```ruby
69-
# Set store as imagekit_store
70-
storage :imagekit_store
75+
# include this module inside the top of the uploader class
76+
include ImageKitIo::CarrierWave
7177

7278
# If you want to add uploading options, then create this method inside the uploader file as an example
7379

@@ -108,6 +114,31 @@ Get image url:
108114
@employee.avatar.url_with(options)
109115
```
110116

117+
#### ActiveStorage
118+
119+
Once you [install](https://guides.rubyonrails.org/active_storage_overview.html#setup) the active_storage gem, then any model can have the attachment using `has_one_attached` or `has_many_attached` like below:
120+
121+
```ruby
122+
class Employee < ApplicationRecord
123+
has_one_attached :avatar
124+
end
125+
```
126+
Now lets configure active_storage as a service for the imagekitio.
127+
128+
First add `:active_storage` in initializer file.
129+
130+
```ruby
131+
config.service = :active_storage
132+
```
133+
134+
Then add the imagekitio service in the `storage.yml` file:
135+
136+
```ruby
137+
imagekitio:
138+
service: ImageKitIo
139+
```
140+
141+
111142
## Usage
112143

113144
You can use this Ruby SDK for three different kinds of methods - URL generation, file upload, and file management.
@@ -126,6 +157,7 @@ about paths with different kinds of origins.
126157

127158

128159
```ruby
160+
imagekitio = ImageKitIo.client
129161
image_url = imagekitio.url({
130162
path: "/default-image.jpg",
131163
url_endpoint: "https://ik.imagekit.io/your_imagekit_id/endpoint/",
@@ -376,37 +408,173 @@ imagekitio.update_file_details(file_id, {
376408
})
377409
```
378410

379-
**6. Delete file**
411+
**5. Copy File**
412+
413+
Copy file from one path to another path using the source file path and the destination path as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-file)
414+
415+
```ruby
416+
imagekitio.copy_file('/path/to/file.jpg', '/folder/to/copy/into')
417+
```
418+
419+
**6. Move File**
420+
421+
Move file from one folder to another folder using the source file path and destination path as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-file)
422+
423+
```ruby
424+
imagekitio.move_file('/path/to/file.jpg', '/folder/to/move/into/')
425+
```
426+
427+
**7. Rename File**
428+
429+
Rename file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/rename-file)
430+
431+
```ruby
432+
imagekitio.rename_file('/path/to/old-file-name.jpg', 'new-file-name.jpg')
433+
```
434+
435+
**8. Delete file**
380436
Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file). The method accepts the file ID of the file that has to be deleted.
381437

382438
```ruby
383439
imagekitio.delete_file(file_id)
384440
```
385441

386-
**6. Bulk File Delete by IDs**
442+
**9. Bulk File Delete by IDs**
387443
Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-files-bulk). The method accepts a list of file IDs of files that has to be
388444
deleted.
389445

390446
```ruby
391447
imagekitio.bulk_file_delete(["file_id1", "file_id2"])
392448
```
393449

394-
**6. Purge Cache**
395-
Programmatically issue a cache clear request as pet the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
450+
**10. Purge Cache**
451+
Programmatically issue a cache clear request as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
396452
Accepts the full URL of the file for which the cache has to be cleared.
397453

398454
```ruby
399455
imagekitio.purge_file_cache(full_url)
400456
```
401-
**7. Purge Cache Status**
457+
**11. Purge Cache Status**
402458

403-
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as pet the
459+
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as per the
404460
[API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
405461

406462
```ruby
407463
imagekitio.get_purge_file_cache_status(cache_request_id)
408464
```
409465

466+
**12. Stream Large File**
467+
468+
Stream large size file using the file url and the block to evaluate on each chunk of response data.
469+
(supported on active_storage)
470+
471+
```ruby
472+
blob_record = employee.avatar.blob
473+
temp_file = Tempfile.new(['downloaded_file.png', '.png'], binmode: true)
474+
blob_record.service.download(blob_record.key) do |chunk|
475+
temp_file.write(chunk)
476+
end
477+
```
478+
479+
**13. Add Bulk Tags**
480+
481+
Add multiple tags on multiple files using array of file ids and array of tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/add-tags-bulk)
482+
483+
```ruby
484+
imagekitio.add_bulk_tags(['id_1', 'id_2'], ['custom_tags', 'image', 'favourite'])
485+
```
486+
487+
**14. Remove Bulk Tags**
488+
489+
Remove multiple tags from multiple files using array of file ids and array of tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/remove-tags-bulk)
490+
491+
```ruby
492+
imagekitio.remove_bulk_tags(['id_1', 'id_2'], ['custom_tags', 'image'])
493+
```
494+
495+
**15. Create Folder**
496+
497+
Create folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/create-folder)
498+
499+
```ruby
500+
imagekitio.create_folder('new_folder', 'source/folder/path')
501+
```
502+
503+
504+
**16. Copy Folder**
505+
506+
Copy folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder)
507+
508+
```ruby
509+
imagekitio.copy_folder('/folder/to/copy', '/folder/to/copy/into')
510+
```
511+
512+
**17. Move Folder**
513+
514+
Move folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder)
515+
516+
```ruby
517+
imagekitio.move_folder('/folder/to/move', '/folder/to/move/into/')
518+
```
519+
520+
**18. Delete Folder**
521+
522+
Delete folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder)
523+
524+
```ruby
525+
imagekitio.delete_folder('folder/to/delete')
526+
```
527+
528+
**19. Bulk Job Status**
529+
530+
Get the bulk job status as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status)
531+
532+
```ruby
533+
imagekitio.bulk_job_status('job_id')
534+
```
535+
536+
**20. Create Custom Metadata Fields**
537+
538+
Create custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/create-custom-metadata-field)
539+
540+
```ruby
541+
542+
imagekitio.create_custom_metadata_fields(
543+
'price',
544+
'price_label',
545+
{
546+
type: 'Number',
547+
minValue: 100,
548+
maxValue: 300
549+
}
550+
)
551+
```
552+
553+
**21. Get Custom Metadata Fields**
554+
555+
Get the custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/get-custom-metadata-field)
556+
557+
```ruby
558+
imagekitio.get_custom_metadata_fields
559+
```
560+
561+
**22. Update Custom Metadata Fields**
562+
563+
Update custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/update-custom-metadata-field)
564+
565+
```ruby
566+
imagekitio.update_custom_metadata_fields('file_id', label: 'custom-price', schema: nil)
567+
```
568+
569+
**23. Delete Custom Metadata Fields**
570+
571+
Delete custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/delete-custom-metadata-field)
572+
573+
```ruby
574+
imagekitio.delete_custom_metadata_fields('file_id')
575+
```
576+
577+
410578
## Utility functions
411579

412580
We have included the following commonly used utility functions in this package.
@@ -469,51 +637,11 @@ imagekitio.phash_distance('a4a65595ac94518b', '7838873e791f8400')
469637

470638
## Sample Application
471639
There are two sample apps:
472-
* [Rails application using Carrierwave](#Instructions-for-rails-application)
473-
* [Plain ruby application](#Instructions-for-ruby-application)
640+
* Rails application using Carrierwave
641+
* Rails application using ActiveStorage
642+
* Plain ruby application
474643

475-
### Instructions for a rails application
476-
This is under [samples/rails_app](https://github.com/imagekit-developer/imagekit-ruby/blob/master/samples/rails_app) directory. Follow the instructions below to set up a rails application.
477-
478-
**1. Clone git repository**
479-
```bash
480-
git clone `https://github.com/imagekit-developer/imagekit-gem
481-
```
482-
**2. Go to sample project directory**
483-
```bash
484-
cd sample/rails_app
485-
```
486-
**3. Write imagekit configuration in `config/environments/development.rb`**
487-
```ruby
488-
config.imagekit={
489-
private_key: "<your-private-key>",
490-
public_key: "<your-public-key>",
491-
url_endpoint: "<endpoint-url>"
492-
}
493-
```
494-
**4. Install dependency**
495-
```ruby
496-
bundle install
497-
```
498-
This sample project is using the Sqlite3 database. If you are getting `sqlite3` gem installation error, then install sqlite3 first, then again run `bundle install`.
499-
500-
**5. Migrate the database**
501-
```ruby
502-
bundle exec rake db:migrate
503-
```
504-
505-
**6. Run your application**
506-
```ruby
507-
rails s
508-
```
509-
It will run on your default rails port [3000].
510-
Sample Application URL: http://localhost:3000/posts/
511-
512-
### Instructions for ruby application
513-
Run following command under [samples/ruby_app](https://github.com/imagekit-developer/imagekit-ruby/blob/master/samples/ruby_app) directory
514-
```ruby
515-
ruby app.rb
516-
```
644+
Please see the sample applications in [here](https://github.com/imagekit-samples/quickstart).
517645

518646
## Support
519647
For any feedback or to report any issues or general implementation support, please reach out to [[email protected]](mailto:[email protected])
@@ -523,4 +651,4 @@ For any feedback or to report any issues or general implementation support, plea
523651
- [Main website](https://imagekit.io)
524652

525653
## License
526-
Released under the MIT license.
654+
Released under the MIT license.

0 commit comments

Comments
 (0)