Slide 1

Slide 1 text

API-driven development in Ruby claudiob.github.io

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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}

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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, …

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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