#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Nelp help translating a func from php to ruby


    Would someone be so kind as to help me translate this over from PHP. I'd be happy to paypal $10 the first person to respond with a translation. Thanks!

    Code:
    /**
     * Makes a remote call over GET/HTTPS
     * See example call below on how it might be used.
     * @param cmd {string} - a Command string ID
     * @argsArray {array} - associative array of argument names and argument values
     * @return {array} - array of responses
     */
    function callAPI( $cmd, $argsArray )
    {
       $apikey="MY_API_KEY";
       $secret="MY_SECRET";
       $apiurl="https://domain.com/api";
       $epoch_time=time();
    
       //--- assemble argument array into string
       $query = "cmd=$cmd"
       foreach ($argsArray as $argName=>$argValue) {
           $query .= "&" . $argName . "=" . urlencode($argValue);
       }
       $query .= "&key=$apikey&time=$epoch_time";
    
       //--- an example $query var would look like this:
       //--- "cmd=loginUser&email=my@address.com ...
       //---  &pw=pass&key=MY_API_KEY&time=1221166373"
    
       //--- make md5 hash of the query + secret string
       $md5 = md5($query . $secret);
       $url = "$apiurl?$query&md5=$md5";
    
       //--- simple GET request, put its contents into $response
       //--- in php, file_get_contents opens a local file or url
       $response = file_get_contents($url);
    
       //--- the reponse is a "|" (pipe) delimited string, 
       //--- which we will convert to array
       //--- example string response: 
       //--- "OK|USER_ID_HERE|User was logged in successfully"
       $responseArray = explode("|", $response,);
       return $responseArray;
    }
    
    //--- example usage:
    $response=callAPI("loginUser", array("email"=>"my@address.com", "pw"=>"pass"));
    
    $responseCode = $response[0];
    $userID = $response[1];
    $message = $response[2];

    Notes for those not familiar with PHP:
    1. The "." (period) in the $md5 line just concats the $query and $secret strings
    2. $var is a variable, and when placed in double quotes $var will be replaced with its value, eg. $var="world"; echo "Hello $var"
    3. $argsArray is an associative array, in some langs like python its called a dictionary
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Homework, I presume?

    Well, I wont do it for you, but I will point you in the right direction.
    instead of foreach you could use
    Code:
    irb(main):002:0> s.each do |key, val|
    irb(main):003:1* puts "Key #{key} Val: #{val}"
    irb(main):004:1> end
    Key a Val: 1
    Key b Val: 2
    => {"a"=>1, "b"=>2}
    I have also explained something else there, variables' value can be used by enclosing them in #{}
    Adding strings is like adding numbers
    Code:
    irb(main):006:0* s = "string"
    => "string"
    irb(main):007:0> t = "ified"
    => "ified"
    irb(main):008:0> s + t
    => "stringified"
    And to break apart a sring based on some delimiter, use split.
    Code:
    irb(main):009:0> s = "A|pipe|separated|string"
    => "A|pipe|separated|string"
    irb(main):010:0> s.split("|")
    => ["A", "pipe", "separated", "string"]
    I think that about covers it.
    Oh, instead of
    Code:
    function callAPI (...
    use
    Code:
    def callAPI(...
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0
    ha... nice. I wish i thought of that when i was in school.
    no, not homework. this is a wrapper function for an API for a my conferencing tool -- Vyew.com.

    Since its an external API i need it translated into
    ASP,Python,Perl,Ruby and anything else relevant.
    I got it in python so far

    if you could be so kind as to put it all together for me,
    i'll go double or nothin to $20 ?

    plus $10 per any other lang listed taht you can translate into?

IMN logo majestic logo threadwatch logo seochat tools logo