Using my Rails app API for iPhone App - iphone

I'm learning iOS Development with the Treehouse Library. Building an app that gets information from a json API.
In the Treehouse API page all the posts are under a parent called "Posts" (http://blog.teamtreehouse.com/api/get_recent_summary/)
posts: [
{
id: 22198,
url: "http://blog.teamtreehouse.com/using-github-pages-to-host-your-website",
title: "Using GitHub Pages To Host Your Website",
date: "2013-08-16 09:30:20",
author: "Matt West",
thumbnail: "http://blog.teamtreehouse.com/wp-content/uploads/2013/08/github-pages-feature-150x150.jpg"
},
{
id: 22196,
url: "http://blog.teamtreehouse.com/running-tests-in-ruby-on-rails-treehouse-quick-tip",
title: "Running Tests in Ruby on Rails – Treehouse Quick Tip",
date: "2013-08-15 14:30:48",
author: "Jason Seifer",
thumbnail: null
},
The API from my Rails app doesnt have a Parent (http://www.soleresource.com/releases.json)
[
{
shoe_name: "Air Jordan 4 "Green Glow"",
release_date: "2013-08-17T00:00:00.000Z",
shoe_colorway: "Dark-Grey/Green-Glow",
shoe_price: "160",
url: "http://www.soleresource.com/releases/8.json"
},
{
shoe_name: "Nike Barkley Posite",
release_date: "2013-08-17T00:00:00.000Z",
shoe_colorway: "Gamma-Green/Black",
shoe_price: "235",
url: "http://www.soleresource.com/releases/17.json"
},
In order to get the app to work I have to call the Parent (posts), like this:
self.upcomingReleases = [dataDictionary objectForKey:#"posts"];
How can I "wrap" my API under a Parent? (My model is called "Releases")

IMHO, if you are using your rails app as a REST API, you should definitely use ActiveModel::Serializer. It will let you specify everything for your JSON responses, like metadata or roots
Here is a RailsCast for it: http://railscasts.com/episodes/409-active-model-serializers

Related

Having an issue with Dialogflow API WebhookResponse V2 for Actions for Google

I'm testing Actions for Google, so I created some simple Sinatra application which looks something like:
require 'sinatra'
require 'json'
post '/google_assistant_api' do
content_type :json
case intent_name
when "input_welcome"
decorated_response
when "Recipe name"
basic_card
end
end
private
def decorated_response
{
source: "test source",
speech: "speech",
display_text: "something"
}.to_json
end
def intent_name
parsed_request["queryResult"]["intent"]["displayName"]
end
def parsed_request
#parsed_request ||= JSON.parse(request.body.read)
end
def basic_card
{
"fulfillmentText": "ACTIONS_ON_GOOGLE",
"fulfillmentMessages": [
{
"platform": "PLATFORM_UNSPECIFIED",
"text": {
"text": [
"string text"
]
},
"image": {
"imageUri": "https://avatars3.githubusercontent.com/u/119195?
s=400&v=4"
},
"basicCard": {
"title": "title string",
"subtitle": "subtitle",
"formattedText": "formatted text",
"image": {
"imageUri": "https://avatars3.githubusercontent.com/u/119195"
},
"buttons": []
}
}
],
"source": "source string"
}.to_json
end
Please note that I'm using V2 of the API and testing using google assistant:
I tried many other response formats based on https://gist.github.com/manniru/f52af230669bd3ed2e69ffe4a76ab309 with no luck. I keep getting:
Sorry! there was no response from the Agent. Please try later.
Is there anyone who tried non nodejs response with luck? I would appreciate any sample response as the simple response seems to be working, however as for the basic card I'm having no luck.
Dialogflow's v2 API uses a different format for webhook requests and responses which is documented here:
Dialogflow v2 Webhook Request
Dialogflow v2 Webhook Response
It appears that your code is using the old format.

Symfony - Api-Platform framework: Get all items of a user

I'm a young developer and I recently discover Api-Platform to make a full rest api.
Currently, I'm following the doc, but I don't understand how to get all books of a person by example.
Here I am:
I have Book and Person entities auto generated, I just add a ManyToMany relation between them.
Then I have the following results:
GET api.platform.dev/app_dev.php/
{
#context: "/app_dev.php/contexts/Entrypoint",
#id: "/app_dev.php/",
#type: "Entrypoint",
person: "/app_dev.php/people",
book: "/app_dev.php/books"
}
GET api.platform.dev/app_dev.php/people/3
{
#context: "/app_dev.php/contexts/Person",
#id: "/app_dev.php/people/3",
#type: "http://schema.org/Person",
birthDate: null,
description: "test",
gender: "Femme",
name: "test",
url: null,
books: [
"/app_dev.php/books/1",
"/app_dev.php/books/4"
]
}
Here is my questions, How can I get in the second result an hypermedia to all books of the person, and What is the best option to get all books of a person ?
I have started with create my custom operation:
#services.yml
resource.person.item_operation.custom_get:
class: "Dunglas\ApiBundle\Api\Operation\Operation"
public: false
factory:
- "#api.operation_factory"
- "createItemOperation"
arguments:
- "#resource.person" # Resource
- ["GET"] # Methods
- "/people/{id}/books" # Path
- "AppBundle:Person:custom" # Controller
- "my_custom_route2" # Route name
- # Context (will be present in Hydra documentation)
"#type": "hydra:Operation"
"hydra:title": "A custom operation"
"returns": "xmls:string"
and
// PersonController.php
<?php
namespace AppBundle\Controller;
use Dunglas\ApiBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
class PersonController extends ResourceController
{
public function customAction(Request $request, $id)
{
return parent::getAction($request, $id);
}
}
result on GET api.platform.dev/app_dev.php/people/3/books is the same of the basic api.platform.dev/app_dev.php/people/3, normal I call the parent.
But now what is the best way to have something like this:
# GET api.platform.dev/app_dev.php/people/3/books
{
#context: "/app_dev.php/contexts/Book",
#id: "/app_dev.php/people/3/books",
#type: "hydra:PagedCollection",
hydra:totalItems: 2,
hydra:itemsPerPage: 30,
hydra:firstPage: "/app_dev.php/people/3/books",
hydra:lastPage: "/app_dev.php/people/3/books",
hydra:member: [
{
#id: "/app_dev.php/books/1",
#type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
},
{
#id: "/app_dev.php/books/2",
#type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
}
]
}
And when I get api.platform.dev/app_dev.php/people/3 add this IRI /app_dev.php/people/3/books
Thanks you for the help you could give me.
Creating sub collections like this is doable but complex with the v1 of API Platform as it requires to create a lot of custom classes (it will be easier with the v2).
What I would suggest is to use the following structure:
GET /books?people=/people/3: retrieve all books owned bu the people 3
It can be done easily using the builtin API Platform search filter.
Then, if you want hypermedia support (btw do you really need it - I mean, is your client able to dereference such hypermedia links?), you need can create a custom Symfony normalizer decorating the Dunglas\ApiBundle\JsonLd\Serializer\ItemNormalizer (service api.json_ld.normalizer.item) provided by API Platform and adding a link to /books?people=/people/3 for the books property.
If you want to still want to follow the road you started, you need to use the Dunglas\ApiBundle\Hydra\Serializer\CollectionNormalizer to normalize your collection in the Hydra format. You can still decorate the ItemNormalizer to point to your custom collection URL.
I hope this help.

Facebook Open Graph publish action with object

I'm working on publishing stories with Facebook Open Graph, for this I'm using their JS SDK. Docs is here : https://developers.facebook.com/docs/opengraph/overview
So the point is to publish for a user : John Smith is listening Shakira - Lalala on Mywebsite.
listening is the action
Shakira is the object
The JS code would be:
FB.api(
'me/mynamespace:listen',
'post',
{
song: "http://mywebsite/shakiramusic"
},
function(response) {
// handle the response
}
);
In this page, some others informations will be specified with code like this:
<meta property="og:title" content="Lalala" />
<meta property="og:image" content="wonderShakira.jpeg" />
But, here is my problem. Imagine that I would like to make a Stories like :
John Smith is reading page 542 from a BigBook on mywebsite
The page number is highly variable and so can't be stock in meta property because Facebook won't refresh it in time.
So I would like making something like that:
FB.api(
'me/mynamespace:reading',
'post',
{
object:{
app_id: 1234567890,
type: "book",
url: "http://mywebsite/thebigbook
title: "The Big Book"
image: "bigbookcover.png",
description: "A super book",
** page_count:542 **
}
},
function(response) {
// handle the response
}
);
But of course it's not working and i get an error. May be i misunderstand the principe of custom stories, actions and objects... I hope my problem is clear and you'll be able to help me.
Thanks :)
I got the answer, here is the good code:
FB.api(
'me/mynamespace:reading',
'post',
{
book:{
app_id: 1234567890,
type: "book",
title: "The Big Book"
image: "bigbookcover.png",
description: "A super book",
url: "http://mywebsite/thebigbook
page_count:542
}
},
function(response) {
// handle the response
}
);
There is two things to consider:
you need to send an object call book for reading and you need to put the url parameter in last, otherwise, Open Graph will get the value in the meta tag. If you put it last, OG will take the parameter that you send.

Screenshot not share in facebook titanium

var media;
Titanium.Media.takeScreenshot(function(e) {
media=e.media;
}
);
var data = {
link: "https://developer.mozilla.org/en/JavaScript",
name: "Best online Javascript reference",
message: " Javascript reference",
caption: "Timbodi",
picture: media,
description: "This section of the site is dedicated to JavaScript-the-language, the parts that are not specific to web pages or other host environments...",
test: [ {foo:'Encoding test', bar:'Durp durp'}, 'test' ]
};
facebook.requestWithGraphPath('me/feed', data, 'POST', showRequestResult);
share the screen shot image, but the image show in facebook is different.
please help i am new in titanium.

How do I get the full URI (i.e. not "streamwrapped") for a Drupal node field in a single request to the Services module?

Preface
I'm building a Drupal site that doubles as a content-serving endpoint for a mobile app written in Flex. The CMS is responsible for storing a number of items that have, among other fields, a field for an image that represents the node (like a movie cover or small product shot). I'm using the Services module and the REST service to serve this content to the mobile app as JSON.
Problem
I've noticed that Drupal (or perhaps Services itself) restricts the amount of data that comes back with each request, seemingly based on the granularity of the request. For example, a node index request returns only fields that are more or less the same for every node: nid, title, status, etc. While a request for a single node additionally returns custom fields, including my image field.
My issue is that the URI value for the image field is, "streamwrapped" (or so I've seen it referred to in researching this issue) so that it is simply listed as public://images/node_image.jpg. In order to get the full URI, I have to make a request for the file. This means that for the index page in the mobile app, I have to make 1 + (n * 2) requests just to get the full data about a given node, plus n requests to get the images for each node display. At most n will be 8 for a given page of nodes, so that means I could be making up to 25 requests to the server per page of nodes.
Is this common/accepted or is there an easier/faster/less heavy-handed way to get access to this data in fewer requests?
Side note: One thing I did notice is that the file request comes back with the image data base64-encoded. I could certainly use that to display an image, but I'd prefer to just request the binary image from the server.
Example
/end-point/node.json
[
{
nid: "6",
vid: "6",
type: "page",
language: "und",
title: "Node 6 Title",
...snip...
uri: "http://example.com/end-point/node/6"
},
{
nid: "5",
vid: "5",
type: "game",
language: "und",
title: "Node 5 Title",
...snip...
uri: "http://example.com/end-point/node/5"
},
...snip...
]
/end-point/node/5.json
{
vid: "5",
uid: "1",
title: "Node 5 Title",
...snip...
body: {
und: [{
value: "Lorem ipsum dolor sit amet.",
summary: "Lorem ipsum...",
format: "filtered_html",
safe_value: "<p>Lorem ipsum dolor sit amet.</p>",
safe_summary: "<p>Lorem ipsum...</p>"
}]
},
field_artwork: {
und: [{
fid: "8",
alt: "",
title: "",
width: "252",
height: "272",
uid: "1",
filename: "node_image.jpg",
uri: "public://images/node_image.jpg",
filemime: "image/jpeg",
filesize: "32653",
status: "1",
timestamp: "1329839925",
rdf_mapping: []
}]
},
...snip...
}
/end-point/file/8.json
{
fid: "8",
uid: "1",
filename: "node_image.jpg",
uri: "public://node_image.jpg",
filemime: "image/jpeg",
filesize: "32653",
status: "1",
timestamp: "1329839925",
rdf_mapping: [ ],
uri_full: "http://example.com/sites/default/files/images/node_image.jpg",
target_uri: "images/node_image.jpg",
file: "..snip base64-encoded image data...",
image_styles: [ ]
}
Since got no answer, I made it by myself.
The new module, Full URL file information, will fill the info you are looking for.
More details in my blog post - "Drupal file information, filling the URL information for Services".