0

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' => '[email protected]',
    'street_address' => '5937 Darwin Court',
    'city' => 'San Diego',
    'state' => 'CA',
    'zip' => '92025',
    'assigned_user_name' => 'Joe Smith',
    'assigned_user_email' => '[email protected]',
    '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.

1
  • Can you post what code you tried with and what errors are you getting and the curl_set_opt are just setting options on the request, with rest_client you'd be doing same thing like setting the type of request, post body etc... Commented Jan 17, 2018 at 0:58

1 Answer 1

4

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:               '[email protected]',
  street_address:      '5937 Darwin Court',
  city:                'San Diego',
  state:               'CA',
  zip:                 '92025',
  assigned_user_name:  'Joe Smith',
  assigned_user_email: '[email protected]',
  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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all. That was exactly what I needed! I now understand PHP a bit better as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.