Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Koala with sinatra #128

Closed
dayanand-sourcebits opened this Issue · 4 comments

3 participants

@dayanand-sourcebits

Hey iam a fairly new ruby newbie so could you please help me how to do this part in sinatra?

<%= link_to 'Previous', url_for(:page => @results.previous_page_params) %>
<%= link_to 'Next', url_for(:page => @results.next_page_params) %>

The example is only for rails iam not able to get this thing working for sinatra. Thanks

@jtarchie

For reference, this is not a Ruby question this would be a framework question.

The url_for method is not traditionally a method provided by Sinatra. There are gems that add the functionality, but not to complete usefulness and integration that Rails provides it.

I would recommend writing you app in Rails so you get all this functionality instead of trying to bend Sinatra to your will.

Or instead of using the helper functions, again only in Rails, use straight up HTML:

<a href="?page=<%=@results.previous_page_params.to_param%>">Previous</a>

etc...

@dayanand-sourcebits

Thanks jtarchie, the app was quite simple hence i thought of avoiding the bloat of rails. Any way ill give one final try with sinatra else stick to rails

@arsduo
Owner

Hey dayanandprabhu and JT,

Fortunately, this isn't impossible in Sinatra, just more difficult. When we get page-able results back, Facebook provides us with URLs for the next and previous pages:

  "paging" => {
        "previous" => "http://graph.facebook.com/search?q=facebook&limit=25&since=1319540282",
            "next" => "http://graph.facebook.com/search?q=facebook&limit=25&until=1319539843"
    }

Koala then parses them and makes them available in a format that's very easy for Rails to digest. However, if you want to use these in Sinatra, you can pass the entire URL as a parameter, then use Koala's methods to parse the URL and get the next page of results:

<a href="?pageURL=<%= CGI.escape @results.paging['previous'] %>">Previous</a>
def index
  # get the first page, or a next/previous page if appropriate params are sent
  if url = params[:pageURL]
    # this is hacky due to how GraphCollection is implemented
    # in the next version I'll expose parse_page_url as a class method
    fake_graph_collection = Koala::Facebook::GraphCollection.new({"data" => []}, your_graph_api_object)
    # use GraphCollection's method to get Koala-friendly params from the Facebook URL
    url_params = fake_graph_collection.parse_page_url(url)
    # then use those params to get the next page
    results = your_graph_api_object.get_page(url_params)
  else
    # get the first page of results as usual
  end
end

From playing around in the console, the general principle works -- let me know if you run into any problems. (You may need to massage the code a bit, I haven't tried it in an app.)

I've added a note and a link to this comment to the Graph API wiki page so that anyone who wants to do this in the future has a reference.

Best,

Alex

@arsduo arsduo closed this
@dayanand-sourcebits

ah thank you although i switched to rails for the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.