Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSON and the APInauts

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

JSON and the APInauts

Avatar for Wynn Netherland

Wynn Netherland

September 17, 2011
Tweet

More Decks by Wynn Netherland

Other Decks in Programming

Transcript

  1. +

  2. {"chart":{ "issueDate":2006-03-04, "description":"Chart", "chartItems":{ "firstPosition":1, "totalReturned":15, "totalRecords":25663, "chartItem":[{ "songName":"Lonely Runs

    Both Ways", "artistName":"Alison Krauss + Union Station", "peek":1, "catalogNo":"610525", "rank":1, "exrank":1, "weeksOn":65, "albumId":655684, ... }}
  3. {"chart":{ "issueDate":2006-03-04, "description":"Chart", "chartItems":{ "firstPosition":1, "totalReturned":15, "totalRecords":25663, "chartItem":[{ "songName":"Lonely Runs

    Both Ways", "artistName":"Alison Krauss + Union Station", "peek":1, "catalogNo":"610525", "rank":1, "exrank":1, "weeksOn":65, "albumId":655684, ... }}
  4. {"chart":{ "issueDate":2006-03-04, "description":"Chart", "chartItems":{ "firstPosition":1, "totalReturned":15, "totalRecords":25663, "chartItem":[{ "songName":"Lonely Runs

    Both Ways", "artistName":"Alison Krauss + Union Station", "peek":1, "catalogNo":"610525", "rank":1, "exrank":1, "weeksOn":65, "albumId":655684, ... }}
  5. {"chart":{ "issue_date":2006-03-04, "description":"Chart", "chart_items":{ "first_position":1, "total_returned":15, "total_records":25663, "chart_item":[{ "song_name":"Lonely Runs

    Both Ways", "artist_name":"Alison Krauss + Union Station", "peek":1, "catalog_no":"610525", "rank":1, "exrank":1, "weeks_on":65, "album_id":655684, ... }}
  6. # Retrieve the details about a user by email #

    # +email+ (Required) # The email of the user to look within. To run getInfoByEmail on multiple addresses, simply pass a comma-separated list of valid email addresses. # def self.info_by_email(email) email = email.join(',') if email.is_a?(Array) Mash.new(self.get('/', ! ! :query => { ! ! ! :method => 'user.getInfoByEmail', ! ! ! :email => email }.merge(Upcoming.default_options))).rsp.user end
  7. # Retrieve the details about a user by email #

    # +email+ (Required) # The email of the user to look within. To run getInfoByEmail on multiple addresses, simply pass a comma-separated list of valid email addresses. # def self.info_by_email(email) email = email.join(',') if email.is_a?(Array) Mash.new(self.get('/', ! ! :query => { ! ! ! :method => 'user.getInfoByEmail', ! ! ! :email => email }.merge(Upcoming.default_options))).rsp.user end More Ruby like than
  8. SOME TWITTER EXAMPLES Twitter Auth from @mbleigh user.twitter.post( '/statuses/update.json', 'status'

    => 'Tweet, tweet!' ) Grackle from @hayesdavis client.statuses.update.json! :status => 'Tweet, tweet!' Twitter from @jnunemaker client.update('Tweet, tweet!') Wrapping Wrapping... with style Abstraction
  9. HTTParty - Ruby module - GET, POST, PUT, DELETE -

    basic_auth, base_uri, default_params, etc. - Net::HTTP for transport - Crack parses JSON and XML
  10. HTTParty class Delicious include HTTParty base_uri 'https://api.del.icio.us/v1' def initialize(u, p)

    @auth = {:username => u, :password => p} end ... def recent(options={}) options.merge!({:basic_auth => @auth}) self.class.get('/posts/recent', options) end ...
  11. RestClient - Simple DSL - ActiveResource support - Built-in shell

    RestClient.post( ! 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' } )
  12. Weary declare "foo" do |r| r.url = "path/to/foo" r.via =

    :post r.requires = [:id, :bar] r.with = [:blah] r.authenticates = false r.follows = false r.headers = {'Accept' => 'text/html'} end client.foo :id => 123, :bar => 'baz' becomes
  13. Faraday url = 'http://api.twitter.com/1' conn = Faraday::Connection.new(:url => url )

    do |builder| builder.adapter Faraday.default_adapter builder.use Faraday::Response::MultiJson builder.use Faraday::Response::Mashify end resp = conn.get do |req| req.url '/users/show.json', :screen_name => 'pengwynn' end u = resp.body u.name # => "Wynn Netherland"
  14. My current stack - Faraday - Faraday Middleware - Hashie

    - Multi JSON - OAuth, OAuth2 as needed
  15. ShamRack ShamRack.at("sinatra.xyz").sinatra do get "/hello/:subject" do "Hello, #{params[:subject]}" end end

    open("http://sinatra.xyz/hello/ stranger").read #=> "Hello, stranger"