Class: CollegiateLink::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/collegiatelink/request.rb

Overview

Represents a single HTTP request to the CollegiateLink API.

Instance Method Summary collapse

Constructor Details

#initialize(action, params = {}, opts = {}) ⇒ Request

Create a new Request instance. See CollegiateLink::ACTIONS for a list of eligible actions.

Parameters:

All parameters are provided in the URL of the request to CollegiateLink.

  • :page - The desired page number of results. (default = 1)

  • :pagesize - The desired number of records to return (default = 100)

  • :modelformatting - Either “normal” or “humanreadable”. (default = “normal”)

Options:

Options are ways to pass in additional parameters about the request. The following options are supported.

  • :sharedkey - (Required) The shared key which is used, but not included in the URL of requests

  • :debug - Print debug information as the request happens.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/collegiatelink/request.rb', line 24

def initialize(action, params = {}, opts = {})
  raise UnknownAction unless ACTIONS.include?(action)
  raise AuthenticationException unless opts.include?(:sharedkey)

  @action = action
  @params = params
  @opts = opts

  # URL Query String Parameters
  @params[:page]            ||= 1
  @params[:pagesize]        ||= 100
  @params[:modelformatting] ||= 'normal'

  # Default values for optional parameters
  @opts[:url_base]  ||= 'https://%s.collegiatelink.net/ws/' % @params[:apikey]
end

Instance Method Details

#performObject

Execute the request by sending the HTTP request.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/collegiatelink/request.rb', line 44

def perform
  # First, add the time-sensitive bits...
  request_params = @params.merge({
    time:   Time.now.to_i * 1000,
    random: Guid.new
  })
  request_params[:hash] = hash(request_params.merge(sharedkey: @opts[:sharedkey]))

  # Then, compute the URL...
  url = URI([
    @opts[:url_base],
    @action,
    '?',
    URI.encode_www_form(request_params),
  ].join)

  puts "requesting: \n#{url}"   if @opts[:debug]

  # Make the Request!
  res = CollegiateLink::Client.proxy.new(url.host, url.port)
  res.use_ssl = true
  res.verify_mode = OpenSSL::SSL::VERIFY_NONE

  resp = res.start { |h|
    h.request_get(url.to_s);
  }

  case resp
  when Net::HTTPSuccess
    return parse_response(resp.body)
  when Net::HTTPError
    raise NetworkException
  else
    raise UnknownException
  end
end

#request_for_next_pageObject

Increment the page number and return a request instance that would receive the next bunch of pages.



85
86
87
# File 'lib/collegiatelink/request.rb', line 85

def request_for_next_page
  Request.new(@action, @params.merge(page: @params[:page] + 1), @opts)
end