Search code examples
phpruby-on-railsrubyrestrest-client

Converting a PHP cURL request to Ruby


I'm attempting to send a NewSubmission to a CRM called LionDesk. The sample NewSubmission code is written in PHP and I need to convert this into Ruby code since I will be integrating this into an existing Rails application.

Here is the code they provide via their docs:

$url = 'https://api-v1.liondesk.com//';
$data = array(
    'action' => 'NewSubmission',
    'firstname' => 'Joe',
    'lastname' => 'Smith',
    'comments' => 'API Test',
    'phone' => '555-1212',
    'email' => 'joe@testaddress.com',
    'street_address' => '5937 Darwin Court',
    'city' => 'San Diego',
    'state' => 'CA',
    'zip' => '92025',
    'assigned_user_name' => 'Joe Smith',
    'assigned_user_email' => 'joeassigned@testaddress.com',
    'assigned_user_phone' => '760-123-1234',
    'tag' => 'Buyer,92025,Listings Landing Page',
    'sourcename' => 'Facebook Ad 1',
    'contactid' => '12345',
    'siteid' => '1'
);
$content = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_USERPWD, $APIKEY . ":" . "" );  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-LionDesk-Id: '.$USERKEY
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$res = json_decode($result);
print_r($res);
curl_close($ch);

Link to their docs: https://api.liondesk.com/docs.html

Any help would be greatly appreciated. I've attempted this with gems like 'rest-client' and 'rack', but am really having difficulties interpreting the php code from $ch = curl_init(); on downward.


Solution

  • Apparently you want to make a post request. more or less like this would be the translation youll have to adapt the headers

    require 'net/http'
    require 'uri'
    require 'json'
    
    uri = URI.parse('https://api-v1.liondesk.com/')
    
    header = {
      'Content-Type':  'text/json',
      'X-LionDesk-Id': 'yourId'
    }
    data = {  
      action:              'NewSubmission',
      firstname:           'Joe',
      lastname:            'Smith',
      comments:            'API Test',
      phone:               '555-1212',
      email:               'joe@testaddress.com',
      street_address:      '5937 Darwin Court',
      city:                'San Diego',
      state:               'CA',
      zip:                 '92025',
      assigned_user_name:  'Joe Smith',
      assigned_user_email: 'joeassigned@testaddress.com',
      assigned_user_phone: '760-123-1234',
      tag:                 'Buyer,92025,Listings Landing Page',
      sourcename:          'Facebook Ad 1',
      contactid:           '12345',
      siteid:              '1'
    }
    
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = data.to_json
    
    response = http.request(request)
    

    Hope it helps. Take a look at this for reference https://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html