render soap data with washout gem rails - ruby-on-rails-plugins

I am trying to render a ruby hash in soap format
render :soap => {:notification_response => {:ack => true}}
The generated xml is like
<notification_response>{"ack"=>"false"}</notification_response>
I tried to eliminate ruby hash from the generated resposne but not succeed.
How to generate xml as
<notification_response><ack>false</ack></notification_response>

render :soap => {:notification_response => {:ack => true}.to_xml}

Related

How to send arbitrary payload with Mojo::UserAgent?

I'm trying to use Mojo::UserAgent to access the eBay API.
One of the options is to use API requests with an XML payload, but I have had no success doing it with Mojo::UserAgent. I didn't find any options for the $ua->post method.
I also tried
my $tx = $ua->build_tx(POST => $ebay_api_url => $headers);
$tx->req->body($xml_body);
my $res = $ua->start($tx)->res->json;
with no success. The XML body is not set for the request.
What do I need to do to achieve the desired result?
I know about the possibility of using JSON requests, but that is a reserve plan.
Try to post your $xml_body like so:
my $tx = $ua->post($ebay_api_url => form => $xml_body);
You likely want (2nd example in post doc):
my $tx = $ua->post($ebay_api_url => {Accept => '*/*'} => $xml_body);
I faced the similar issue like yours but later I realized that the problem was with the xml data. Please ensure that you do not have any trailing or leading white spaces in your $xml_body. This works:
my $tx = $ua->post($ebay_api_url => $headers => $xml_body);
Mojolicious and Mojo::UserAgent is awesome and light.

Get nested ressources when using cakePhp restfull

I'm using cakePhp to create a Rest api (see http://book.cakephp.org/2.0/fr/development/rest.html) and I need to get nested resources. The documentation tells how to get let's say books implementing a URI /books.json. But does not tell how to get for example reviews for a given book. What I'm trying to make is somthing like this: /books/14/reviews.json that returns Review resources.
Can any one tell me hwo to make this?
See the Custom REST Routing section of the docs you've linked. In case the default routing doesn't work for you, you'll have to create your own custom routes that either replace or extend the default ones.
Your /books/14/reviews.json URL could for example be mapped to BooksController::reviews() likes this:
Router::connect(
'/books/:id/reviews',
array(
'[method]' => 'GET',
'controller' => 'books',
'action' => 'reviews'
),
array(
'id' => Router::ID . '|' . Router::UUID,
'pass' => array(
'id'
)
)
);
When placed before Router::mapResources() it should work fine together with the default routes.

Is it possible to do batch POST API calls?

I'm writing an app that makes a daily post as a user, and having benchmarked the PHP code that does this, it seems to take about two seconds per user. I'm dividing the work up in to chunks, and using multiple cron jobs to do each chunk. I'd like to scale to many thousands of users one day, but this kind of work load is just too much. It would take my server literally all day to post to each user one at a time using this method.
How do people normally do this? I've seen other apps that do it. Is there some way of sending all these posts off at once using just one API call? Using individual API calls per user is crazy slow.
Thanks.
On one hand, this is entirely dependent on the API.
However, you could use a multi-threaded or pseudo-parallel approach to this, such that your program sends, say, 100 HTTP POST requests at a time, rather than generating one request after another in series.
Since you're using PHP, multi-threading is out (I think) but this question is very similar to others. For example, see how these folks recommend curl_multi.
You can use batch query to achieve what you need.
The code for batch query is mentioned below. You can refer more about Facebook batch query at : http://25labs.com/tutorial-post-to-multiple-facebook-wall-or-timeline-in-one-go-using-graph-api-batch-request/
$body = array(
'message' => $_POST['message'],
'link' => $_POST['link'],
'picture' => $_POST['picture'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description'],
);
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID1}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID2}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID3}/feed",
'body' => http_build_query($body) );
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');

Magento soap v2 error: Attribute "available_sort_by" is required

I'm trying to create category with magento soap v2 api call. I'm getting the error:
Attribute "available_sort_by" is required.
Code for calling the Api:
$category_data = array( "name" => "testcategory", "is_active" => "1", "include_in_menu" => "1","available_sort_by" => "","default_sort_by" => "");
$result = $client->catalogCategoryCreate($session,2,$category_data,1);
echo $result;
I have tried also with "available_sort_by" => array("name", "price", ...)
Is this the right way for calling the v2 soap api.
Thanks for any advice.
The WSDL does define what data you need for your call and exposes it for you to look at if you put 'trace' on your API SOAP calls.
After your call, with trace on, you can get your last XML, but it will be tidied up by the SOAP to be shoehorned into what the WSDL thinks you need for that call. The data will be different to what you submitted and show fields you never knew were in existence - it kind of reveals the documentation that you wish you had to start with.

Validation of Zend_Captcha_Image when using in form

I have the following code in a form in zend framework application.
$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => "",
'captcha' => 'image',
'captchaOptions' => array(
'captcha' => 'image',
'font'=> APPLICATION_PATH . '/../public_html/assets/fonts/akbar.ttf',
'imgDir'=> APPLICATION_PATH . '/../public_html/assets/captcha/',
'imgUrl'=> '/assets/captcha/',
'wordLen' => 1,
'fsize'=>20,
'height'=>60,
'width'=>200,
'gcFreq'=>50,
'expiration' => 300)
));
and the display of the form element is as expected.
When I try to validate the form using the following code it always returns error even if I enter the captcha correctly.
if($this->getRequest()->isPost()) {
if($this->view->form->isValid($_POST)) {
Any solution on how to validate it correctly will be of great help.
Thanks
Nizam
I'm going to guess that you're also doing $captcha->generate(); before the isPost() check. The problem with this is that when you submit the form, you're generating a new CAPTCHA before checking the POST data, so the check will always fail because it's validating it against the new CAPTCHA. The solution is simply to move the generate call further down.
I put up a blog post a while back with some code examples of this component, see here - http://tfountain.co.uk/blog/2009/1/6/zend-captcha-image-experiences but the post is a couple of years old now so some things may have changed.
If this doesn't help, please edit your question to include a bit more code so we can see what else might be causing the problem.
Check this post: Zend Framework: Captcha problem
Basically you have to remove the "viewhelper" from the element.
ex.:
$form->getElement('captcha')->removeDecorator("viewhelper");