15

We have deployed an instance of Wordpress and have installed a JSON API plug in

Using this plug-in you can make simple HTTP GET request like:

http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana

And a response comes back in JSON:

{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{...}}

My question is how should I go about making these types of requests (and then parsing the returned JSON) in my Rails app. I was about to get started just writing something from scratch but I imagine there's some good existing tools out there to manage something like this. Basically I've never attempted connecting to an API when a Ruby-wrapper wasn't already available. Can you help a noob with how to get started?

1 Answer 1

30

It looks like HTTParty is probably your best case for something like this. I think it'll be an easy way for you to get started and handles the JSON for you easily.

Add 'httparty' to your Gemfile and bundle.

recent_posts = HTTParty.get "http://www.example.org/?json=get_recent_posts"
puts recent_posts.status, recent_posts.count

recent_posts.posts.each do |post|
  puts post.title #guessing
end
Sign up to request clarification or add additional context in comments.

Comments

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.