Sha256: fddb291acac51aeab41c4c1bfc509ed982aa1ccdabeb71d1e73b7ecd4e9dde1f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'json'
require 'open-uri'
require 'classes/coursehistory'

#PCR class handles token and api url, so both are easily changed
module PCR
  class Client
    attr_reader :token

    def initialize(token, api_endpt = "http://api.penncoursereview.com/v1/")
      @token = token
      @api_endpt = api_endpt
    end

    def get_json(path)
      #TODO: Error handling for bad/no token
      JSON.parse(open("#{@api_endpt + path}?token=#{@token}").read)
    end

    def coursehistory(course_code)
      CourseHistory.new(course_code)
    end

    def instructor(id)
      raise NotImplementedError.new("Instructors have not yet been implemented.")
    end

    def dept(code)
      raise NotImplementedError.new("Departments have not yet been implemented.")
    end
  end

  class << self
    attr_accessor :token

    def client
      @client = PCR::Client.new(token) unless @client && token == @client.token
      @client
    end

    def respond_to_missing?(method_name, include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
    def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"

    private
    def method_missing(method_name, *args, &block)
      return super unless client.respond_to?(method_name)
      client.send(method_name, *args, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pcr-0.5 lib/pcr.rb