Sha256: 42d0b41d2bb76423faef3e527410b5cc4e5c7678003d08daf818e97dd7d2df1a

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'excon'
require 'json'
require 'rubygems/text'

module WWDC
  YEARS = (2010..2018)

  module Helpers
    include Gem::Text

    def get(options = {}, &block)
      response = client.get(options)
      say_error "Error #{response.status}" and abort unless response.status == 200

      yield response if block_given?

      JSON.parse(response.body)
    end

    def describe(*sessions)
      enable_paging if sessions.length > 5

      sessions.each do |session|
        puts %{\033[1mWWDC #{session['year']}\033[0m}
        puts %{\033[1mSession #{session['number']}: "#{session['title']}"\033[0m}
        puts
        puts format_text(session['description'], 80)
        puts
      end
    end

    private

    def determine_session!(args = [], options = {})
      @number = (Integer(args.first).nonzero? rescue nil)
      say_error "Missing session number" and abort unless @number
    end

    def determine_year!(args = [], options = {})
      @year = (Integer(options.year).nonzero? rescue nil) || YEARS.last
      say_error "Invalid year: #{@year}" and abort unless YEARS.include?(@year)
    end

    def determine_query!(args = [], options = {})
      @query = args.join(" ")
      say_error "Missing query" and abort if @query.empty?
    end

    def client
      @client ||= Excon.new('https://asciiwwdc.com', headers: {'Accept' => "application/json"})
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wwdc-1.1.1 ./lib/wwdc/helpers.rb
wwdc-1.1.0 ./lib/wwdc/helpers.rb