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

API-driven development in Ruby

API-driven development in Ruby

Presented at the L.A. Ruby meetup, June 2014

Claudio B.

June 12, 2014
Tweet

More Decks by Claudio B.

Other Decks in Programming

Transcript

  1. API-driven
    development
    in Ruby
    claudiob.github.io

    View Slide

  2. Application
    Programming
    Interface
    1/10
    ers
    class Artist < ActiveRecord::Base
    has_many :concerts
    end

    View Slide

  3. Application
    Programming
    Interface
    Claudio Baccigalupo
    claudiob.github.io
    class Artist < ActiveRecord::Base
    include ActiveRecord::Associations
    has_many :concerts
    end
    2/10
    ers

    View Slide

  4. Application
    Programming
    Interface
    Claudio Baccigalupo
    claudiob.github.io
    3/10
    ers
    class Artist < ActiveRecord::Base
    require 'active_record/associations'
    module ActiveRecord::Associations::Builder
    class CollectionAssociation < Association #:nodoc:
    CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
    def valid_options
    super + [:table_name, :before_add,
    :after_add, :before_remove, :after_remove, :extend]
    end
    attr_reader :block_extension
    def initialize(model, name, scope, options)
    super
    @mod = nil
    if block_given?
    @mod = Module.new(&Proc.new)
    @scope = wrap_scope @scope, @mod
    end
    end
    def self.define_callbacks(model, reflection)
    super
    name = reflection.name
    options = reflection.options
    CALLBACKS.each { |callback_name|
    define_callback(model, callback_name, name, options)
    }
    end […]
    end

    View Slide

  5. Writing a good
    interface to like a
    YouTube video
    4/10
    https://developers.google.com/
    youtube/v3/docs/videos/rate

    View Slide

  6. curl
    5/10
    request = `curl -X POST -H 'Content-length: 0'
    -I -H 'Authorization: Bearer #{token}'
    'https://www.googleapis.com/youtube/v3/
    videos/rate?id=#{video_id}&rating=like'`
    request =~ %r{^HTTP/1.1 204}

    View Slide

  7. google-api-client
    6/10
    # gem install google_api_client
    require 'google/api_client'
    client = Google::APIClient.new(
    application_name: 'App',
    application_version: '1.0')
    client.authorization.access_token = token
    yt = client.discovered_api 'youtube', 'v3'
    response = client.execute!(
    api_method: yt.videos.rate,
    parameters: {id: video_id, rating: "like"})
    response.status == 204

    View Slide

  8. youtube_it
    7/10
    # gem install youtube_it
    require 'youtube_it'
    client = YouTubeIt::OAuth2Client.new(
    client_access_token: token,
    dev_key: dev_key)
    response = client.like_video video_id
    response[:code] == 204

    View Slide

  9. googol
    8/10
    # gem install googol
    require 'googol'
    account = Googol::YoutubeAccount.new(
    access_token: token)
    account.like! video_id: video_id

    View Slide

  10. yt
    9/10
    # gem install yt
    require 'yt'
    account = Yt::Account.new access_token: token
    video = Yt::Video.new id: video_id, auth: account
    video.like # also: video.liked?, video.dislike, …

    View Slide

  11. youtube_it vs. yt
    10/10
    class Yt::Models::Video
    has_many :annotations
    has_one :rating
    def liked? … end
    def like … end
    def dislike … end
    def unlike … end
    end
    class YouTubeIt::Client
    def videos_by(opts) … end
    def video_by(video) … end
    def profile(user) … end
    def activity(user) … end
    def watchlater(user) … end
    def playlist(id) … end
    def playlists(user) … end
    def current_user … end
    […50 more methods…]
    end

    View Slide

  12. API-driven
    development
    ❤ Ruby
    code snippets > git.io/Y6OS7Q
    yt > github.com/Fullscreen/yt

    View Slide