Class: Url
- Inherits:
-
Object
- Object
- Url
- Defined in:
- lib/imagekit/url.rb
Instance Method Summary collapse
- #build_url(options) ⇒ Object
- #extend_url_options(options) ⇒ Object
- #generate_url(options) ⇒ Object
- #get_signature(private_key, url, url_endpoint, expiry_timestamp) ⇒ Object
- #get_signature_timestamp(seconds) ⇒ Object
- #hash_to_url(url_hash) ⇒ Object
-
#initialize(request_obj) ⇒ Url
constructor
A new instance of Url.
- #transformation_to_str(transformation) ⇒ Object
- #trim_slash(str, both = true) ⇒ Object
Constructor Details
#initialize(request_obj) ⇒ Url
Returns a new instance of Url.
13 14 15 |
# File 'lib/imagekit/url.rb', line 13 def initialize(request_obj) @req_obj = request_obj end |
Instance Method Details
#build_url(options) ⇒ Object
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 |
# File 'lib/imagekit/url.rb', line 25 def build_url() # build url from all options path = .fetch(:path, "") src = .fetch(:src, "") url_endpoint = .fetch(:url_endpoint, "") transformation_position = [:transformation_position] unless Default::VALID_TRANSFORMATION_POSITION.include? transformation_position raise ArgumentError, INVALID_TRANSFORMATION_POS end src_param_used_for_url = false if (src != "") || (transformation_position == Default::QUERY_TRANSFORMATION_POSITION) src_param_used_for_url = true end if path == "" && src == "" return "" end result_url_hash = {'host': "", 'path': "", 'query': ""} existing_query=nil if path != "" parsed_url = URI.parse(path) existing_query=parsed_url.query parsed_host = URI(url_endpoint) result_url_hash[:scheme] = parsed_host.scheme # making sure single '/' at end result_url_hash[:host] = parsed_host.host.to_s.chomp("/") + parsed_host.path.chomp("/") + "/" result_url_hash[:path] = trim_slash(parsed_url.path) else parsed_url = URI.parse(src) existing_query=parsed_url.query host = parsed_url.host result_url_hash[:userinfo] = parsed_url.userinfo if parsed_url.userinfo result_url_hash[:host] = host result_url_hash[:scheme] = parsed_url.scheme result_url_hash[:path] = parsed_url.path src_param_used_for_url = true end query_params = {} if existing_query!=nil existing_query.split("&").each do |part| parts=part.split("=") if parts.length==2 query_params[parts[0]]=parts[1] else query_params[parts[0]]="" end end end .fetch(:query_parameters, {}).each do |key, value| query_params[key]=value end transformation_str = transformation_to_str([:transformation]).chomp("/") if transformation_str if (transformation_position == Default::QUERY_TRANSFORMATION_POSITION) || src_param_used_for_url == true result_url_hash[:query] = "#{Default::TRANSFORMATION_PARAMETER}=#{transformation_str}" query_params[:tr]=transformation_str else result_url_hash[:path] = "#{Default::TRANSFORMATION_PARAMETER}:#{transformation_str}/#{result_url_hash[:path]}" end end result_url_hash[:host] = result_url_hash[:host].to_s.reverse.chomp("/").reverse result_url_hash[:path] = result_url_hash[:path].chomp("/") result_url_hash[:scheme] ||= "https" # Signature String and Timestamp # We can do this only for URLs that are created using urlEndpoint and path parameter # because we need to know the endpoint to be able to remove it from the URL to create a signature # for the remaining. With the src parameter, we would not know the "pattern" in the URL query_param_arr = [] query_param_arr.push("ik-sdk-version=ruby-"+Imagekit::Sdk::VERSION) if [:signed] && !([:src]) intermediate_url = result_url_hash.fetch(:scheme, "") + "://" + result_url_hash.fetch(:host, "") + result_url_hash.fetch(:path, "") if result_url_hash[:query]!=nil && result_url_hash[:query]!="" intermediate_url += result_url_hash.fetch(:query, "") end end query_params.each do |key, value| query_param_arr.push(key.to_s + "=" + value.to_s) end query_param_str = query_param_arr.join("&") result_url_hash[:query] = query_param_str url=hash_to_url(result_url_hash) if [:signed] private_key = [:private_key] expire_seconds = [:expire_seconds] = (expire_seconds) url_signature = get_signature(private_key, url, url_endpoint, ) query_param_arr.push(Default::SIGNATURE_PARAMETER + "=" + url_signature) if && ( != Default::TIMESTAMP) query_param_arr.push(Default::TIMESTAMP_PARAMETER + "=" + .to_s) end query_param_str = query_param_arr.join("&") result_url_hash[:query] = query_param_str url=hash_to_url(result_url_hash) end url end |
#extend_url_options(options) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/imagekit/url.rb', line 189 def () attr_dict = {"public_key": @req_obj.public_key, "private_key": @req_obj.private_key, "url_endpoint": @req_obj.url_endpoint, "transformation_position": @req_obj.transformation_position, } # extending url options attr_dict.merge() end |
#generate_url(options) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/imagekit/url.rb', line 17 def generate_url() if .key? :src [:transformation_position] = Default::TRANSFORMATION_POSITION end = () build_url() end |
#get_signature(private_key, url, url_endpoint, expiry_timestamp) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/imagekit/url.rb', line 175 def get_signature(private_key, url, url_endpoint, ) # creates signature(hashed hex key) and returns from # private_key, url, url_endpoint and expiry_timestamp if ==0 =Default::DEFAULT_TIMESTAMP end if url_endpoint[url_endpoint.length-1]!="/" url_endpoint+="/" end replaced_url=url.gsub(url_endpoint, "") replaced_url = replaced_url + .to_s OpenSSL::HMAC.hexdigest("SHA1", private_key, replaced_url) end |
#get_signature_timestamp(seconds) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/imagekit/url.rb', line 163 def (seconds) # this function returns either default time stamp # or current unix time and expiry seconds to get # signature time stamp if seconds.to_i == 0 Default::DEFAULT_TIMESTAMP else DateTime.now.strftime("%s").to_i + seconds.to_i end end |
#hash_to_url(url_hash) ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'lib/imagekit/url.rb', line 198 def hash_to_url(url_hash) generated_url = url_hash.fetch(:scheme, "") + "://" + url_hash.fetch(:host, "") + url_hash.fetch(:path, "") if url_hash[:query] != "" generated_url = generated_url + "?" + url_hash.fetch(:query, "") return generated_url end generated_url end |
#transformation_to_str(transformation) ⇒ Object
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 |
# File 'lib/imagekit/url.rb', line 136 def transformation_to_str(transformation) # creates transformation_position string for url # from transformation dictionary unless transformation.is_a?(Array) return "" end parsed_transforms = [] (0..(transformation.length - 1)).each do |i| parsed_transform_step = [] transformation[i].keys.each do |key| transform_key = SUPPORTED_TRANS.fetch(key, nil) transform_key ||= key if transformation[i][key] == "-" parsed_transform_step.push(transform_key) else parsed_transform_step.push("#{transform_key}#{Default::TRANSFORM_KEY_VALUE_DELIMITER}#{transformation[i][key]}") end end parsed_transforms.push(parsed_transform_step.join(Default::TRANSFORM_DELIMITER)) end parsed_transforms.join(Default::CHAIN_TRANSFORM_DELIMITER) end |
#trim_slash(str, both = true) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/imagekit/url.rb', line 207 def trim_slash(str, both = true) if str == "" return "" end # remove slash from a string # if both is not provide trims both slash # example - '/abc/' returns 'abc' # if both=false it will only trim end slash # example - '/abc/' returns '/abc' # NOTE: IT'S RECOMMENDED TO USE inbuilt .chomp('string you want to remove') # FOR REMOVING ONLY TRAILING SLASh if both str[0].chomp("/") + str[1..-2] + str[-1].chomp("/") else str.chomp("/") end end |