-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathcloudinary.rb
164 lines (138 loc) · 4.58 KB
/
cloudinary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright Cloudinary
require "ostruct"
require "pathname"
require "yaml"
require "uri"
require "erb"
require "cloudinary/version"
require "cloudinary/exceptions"
require "cloudinary/missing"
module Cloudinary
autoload :Utils, 'cloudinary/utils'
autoload :Uploader, 'cloudinary/uploader'
autoload :BaseConfig, "cloudinary/base_config"
autoload :Config, "cloudinary/config"
autoload :AccountConfig, "cloudinary/account_config"
autoload :BaseApi, "cloudinary/base_api"
autoload :Api, "cloudinary/api"
autoload :AccountApi, "cloudinary/account_api"
autoload :Downloader, "cloudinary/downloader"
autoload :Blob, "cloudinary/blob"
autoload :PreloadedFile, "cloudinary/preloaded_file"
autoload :Static, "cloudinary/static"
autoload :CarrierWave, "cloudinary/carrier_wave"
autoload :Search, "cloudinary/search"
autoload :SearchFolders, "cloudinary/search_folders"
autoload :Analytics, "cloudinary/analytics"
SHARED_CDN = "res.cloudinary.com"
USER_AGENT = "CloudinaryRuby/#{VERSION} (Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})"
@@user_platform = defined?(Rails.version) ? "Rails/#{Rails.version}" : ""
# Add platform information to the USER_AGENT header
# This is intended for platform information and not individual applications!
def self.user_platform=(value)
@@user_platform= value
end
def self.user_platform
@@user_platform
end
def self.USER_AGENT
if @@user_platform.empty?
USER_AGENT
else
"#{@@user_platform} #{USER_AGENT}"
end
end
FORMAT_ALIASES = {
"jpeg" => "jpg",
"jpe" => "jpg",
"tif" => "tiff",
"ps" => "eps",
"ept" => "eps"
}
# Cloudinary config
#
# @param [Hash] new_config If +new_config+ is passed, Config will be updated with it
# @yieldparam [OpenStruct] Config can be updated in the block
#
# @return [OpenStruct]
def self.config(new_config=nil)
@@config ||= make_new_config(Config)
@@config.update(new_config) if new_config
yield @@config if block_given?
@@config
end
# Cloudinary account config
#
# @param [Hash] new_config If +new_config+ is passed, Account Config will be updated with it
# @yieldparam [OpenStruct] Account config can be updated in the block
#
# @return [OpenStruct]
def self.account_config(new_config=nil)
@@account_config ||= make_new_config(AccountConfig)
@@account_config.update(new_config) if new_config
yield @@account_config if block_given?
@@account_config
end
def self.config_from_url(url)
config.load_from_url(url)
end
def self.config_from_account_url(url)
account_config.load_from_url(url)
end
def self.app_root
if defined? Rails::root
# Rails 2.2 return String for Rails.root
Rails.root.is_a?(Pathname) ? Rails.root : Pathname.new(Rails.root)
else
Pathname.new(".")
end
end
private
def self.config_env
return ENV["CLOUDINARY_ENV"] if ENV["CLOUDINARY_ENV"]
return Rails.env if defined? Rails::env
nil
end
def self.config_dir
return Pathname.new(ENV["CLOUDINARY_CONFIG_DIR"]) if ENV["CLOUDINARY_CONFIG_DIR"]
self.app_root.join("config")
end
def self.set_config(new_config)
new_config.each{|k,v| @@config.send(:"#{k}=", v) if !v.nil?}
end
# Builds config from yaml file, extends it with specific module and loads configuration from environment variable
#
# @param [Module] config_module Config is extended with this module after being built
#
# @return [OpenStruct]
def self.make_new_config(config_module)
import_settings_from_file.tap do |config|
config.extend(config_module)
config.load_config_from_env
end
end
private_class_method :make_new_config
# Import settings from yaml file
#
# @return [OpenStruct]
def self.import_settings_from_file
yaml_env_config = begin
yaml_source = ERB.new(IO.read(config_dir.join("cloudinary.yml"))).result
yaml_config = if YAML.respond_to?(:safe_load)
YAML.safe_load(yaml_source, aliases: true)
else
YAML.load(yaml_source)
end
yaml_config[config_env]
rescue StandardError
{}
end
OpenStruct.new(yaml_env_config)
end
private_class_method :import_settings_from_file
end
# Prevent require loop if included after Rails is already initialized.
require "cloudinary/helper" if defined?(::ActionView::Base)
require "cloudinary/cloudinary_controller" if defined?(::ActionController::Base)
require "cloudinary/railtie" if defined?(Rails) && defined?(Rails::Railtie)
require "cloudinary/engine" if defined?(Rails) && defined?(Rails::Engine)