Need help converting Ruby code to php code
- by newprog
Yesterday I posted this queston.
Today I found the code which I need but written in Ruby. Some parts of code I have understood (I don't know Ruby) but there is one part that I can't. I think people who know ruby and php can
help me understand this code.
def do_create(image)
# Clear any old info in case of a re-submit
FIELDS_TO_CLEAR.each { |field| image.send(field+'=', nil) }
image.save
# Compose request
vm_params = Hash.new
# Submitting a file in ruby requires opening it and then reading the contents into the post body
file = File.open(image.filename_in, "rb")
# Populate the parameters and compute the signature
# Normally you would do this in a subroutine - for maximum clarity all
# parameters are explicitly spelled out here.
vm_params["image"] = file # Contents will be read by the multipart object created below
vm_params["image_checksum"] = image.image_checksum
vm_params["start_job"] = 'vectorize'
vm_params["image_type"] = image.image_type if image.image_type != 'none'
vm_params["image_complexity"] = image.image_complexity if image.image_complexity != 'none'
vm_params["image_num_colors"] = image.image_num_colors if image.image_num_colors != ''
vm_params["image_colors"] = image.image_colors if image.image_colors != ''
vm_params["expire_at"] = image.expire_at if image.expire_at != ''
vm_params["licensee_id"] = DEVELOPER_ID
#in php it's like this $vm_params["sequence_number"] = -rand(100000000);?????
vm_params["sequence_number"] = Kernel.rand(1000000000) # Use a negative value to force an error when calling the test server
vm_params["timestamp"] = Time.new.utc.httpdate
string_to_sign =
CREATE_URL + # Start out with the URL being called...
#vm_params["image"].to_s + # ... don't include the file per se - use the checksum instead
vm_params["image_checksum"].to_s + # ... then include all regular parameters
vm_params["start_job"].to_s +
vm_params["image_type"].to_s +
vm_params["image_complexity"].to_s + # (nil.to_s => '', so this is fine for vm_params we don't use)
vm_params["image_num_colors"].to_s +
vm_params["image_colors"].to_s +
vm_params["expire_at"].to_s +
vm_params["licensee_id"].to_s + # ... then do all the security parameters
vm_params["sequence_number"].to_s +
vm_params["timestamp"].to_s
vm_params["signature"] = sign(string_to_sign) #no problem
# Workaround class for handling multipart posts
mp = Multipart::MultipartPost.new
query, headers = mp.prepare_query(vm_params) # Handles the file parameter in a special way (see /lib/multipart.rb)
file.close # mp has read the contents, we can close the file now
response = post_form(URI.parse(CREATE_URL), query, headers)
logger.info(response.body)
response_hash = ActiveSupport::JSON.decode(response.body) # Decode the JSON response string
##I have understood below
def sign(string_to_sign)
#logger.info("String to sign: '#{string_to_sign}'")
Base64.encode64(HMAC::SHA1.digest(DEVELOPER_KEY, string_to_sign))
end
# Within Multipart modul I have this:
class MultipartPost
BOUNDARY = 'tarsiers-rule0000'
HEADER = {"Content-type" => "multipart/form-data, boundary=" + BOUNDARY + " "}
def prepare_query (params)
fp = []
params.each {|k,v|
if v.respond_to?(:read)
fp.push(FileParam.new(k, v.path, v.read))
else
fp.push(Param.new(k,v))
end
}
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
return query, HEADER
end
end
end
Thanks for your help.