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



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

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

#coursesStellar::Courses

A Stellar client specialized to answer course queries.

Returns:



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

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



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

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



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

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



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

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
27
# File 'lib/stellar/client.rb', line 18

def mech(&block)
  m = Mechanize.new do |m|
    m.cert_store = OpenSSL::X509::Store.new
    m.cert_store.add_file mitca_path
    m.user_agent_alias = 'Linux Firefox'
    yield m if block
    m
  end
  m
end