Skip to content

Instantly share code, notes, and snippets.

@dohoonk
Created March 7, 2016 19:51
Show Gist options
  • Select an option

  • Save dohoonk/9754ebd00ef1d648a942 to your computer and use it in GitHub Desktop.

Select an option

Save dohoonk/9754ebd00ef1d648a942 to your computer and use it in GitHub Desktop.
Building API/Faraday

#Api vesion control

-bin/rails g controller api/v1/questions

will generate api folder and v1


faraday gem can mimic http request

on a seperate file make a plain rb file ex. client.rb

install faraday on gem on your console

on client.rb require "faraday"

conn = Faraday.new(:url => 'http://localhost:3000') do |faraday| faraday.request :url_encoded faraday.response :logger faraday.adapter Faraday.default_adapter end

response = conn.get '/api/v1/questions/' response.body

or we can parse the JSON

parsed_body = JSON.parse(response.body) puts parsed_body.class

parsed_body["questions"].each do |question_hash| puts question_hash["title"] end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment