Class: Stellar::Client

Inherits:
Object
  • Object
show all
Includes:
Auth
Defined in:
lib/stellar/client.rb

Overview

Client session for accessing the Stellar API.

Instance Method Summary collapse

Methods included from Auth

#auth, get_certificate, #mitca_path

Constructor Details

#initializeClient

Client for accessing public information.

Call auth to authenticate as a user and access restricted functionality.



11
12
13
14
15
# File 'lib/stellar/client.rb', line 11

def initialize
  @mech = mech
  
  @courses = nil
end

Instance Method Details

#course(number, year, semester) ⇒ Object



64
65
66
# File 'lib/stellar/client.rb', line 64

def course(number, year, semester)
  Stellar::Course.for number, year, semester, self
end

#coursesStellar::Courses

A Stellar client specialized to answer course queries.

Returns:



59
60
61
# File 'lib/stellar/client.rb', line 59

def courses
  @courses ||= Stellar::Courses.new self
end

#get(path) ⇒ Mechanize::Page

Fetches a page from the Stellar site.

Parameters:

  • path (String)

    relative URL of the page to be fetched

Returns:

  • (Mechanize::Page)

    the desired page, wrapped in the Mechanize API



32
33
34
35
# File 'lib/stellar/client.rb', line 32

def get(path)
  uri = URI.join('https://stellar.mit.edu', path)
  page_bytes = @mech.get uri
end

#get_file(path) ⇒ String

Fetches a file from the Stellar site.

Parameters:

  • path (String)

    relative URL of the file to be fetched

Returns:

  • (String)

    raw contents of the file



51
52
53
54
# File 'lib/stellar/client.rb', line 51

def get_file(path)
  uri = URI.join('https://stellar.mit.edu', path)
  @mech.get_file uri
end

#get_nokogiri(path) ⇒ Nokogiri::HTML::Document

Fetches a page from the Stellar site.

Parameters:

  • path (String)

    relative URL of the page to be fetched

Returns:

  • (Nokogiri::HTML::Document)

    the desired page, parsed with Nokogiri



41
42
43
44
45
# File 'lib/stellar/client.rb', line 41

def get_nokogiri(path)
  uri = URI.join('https://stellar.mit.edu', path)
  raw_html = @mech.get_file uri
  Nokogiri.HTML raw_html, uri.to_s
end

#mech(&block) ⇒ Object

New Mechanize instance.



18
19
20
21
22
23
24
25
26
# File 'lib/stellar/client.rb', line 18

def mech(&block)
  m = Mechanize.new do |m|
    # m.ca_file = mitca_path
    m.user_agent_alias = 'Linux Firefox'
    yield m if block
    m
  end
  m
end