Last active
November 4, 2015 17:50
-
-
Save jordanhudgens/4ba1d38a04227ea1bd7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'httparty' | |
# Base Example | |
class EdutechionalResty | |
include HTTParty | |
base_uri 'edutechional-resty.herokuapp.com' | |
def posts | |
self.class.get("/posts.json") | |
end | |
end | |
edutechional_resty = EdutechionalResty.new | |
puts edutechional_resty.posts | |
# Use the class methods to get down to business quickly | |
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow') | |
puts response.body, response.code, response.message, response.headers.inspect | |
# Or wrap things up in your own class | |
class StackExchange | |
include HTTParty | |
base_uri 'api.stackexchange.com' | |
def initialize(service, page) | |
@options = { query: {site: service} } | |
end | |
def questions | |
self.class.get("/2.2/questions", @options) | |
end | |
def users | |
self.class.get("/2.2/users", @options) | |
end | |
end | |
stack_exchange = StackExchange.new("stackoverflow", 1) | |
puts stack_exchange.questions | |
puts stack_exchange.users | |
# Parsing: | |
# For easier reading: https://jsonformatter.curiousconcept.com/ | |
edutechional_resty.posts.each do |post| | |
p "Title: #{post['title']} | Description: #{post['description']}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment