Class: XQueue
- Inherits:
-
Object
- Object
- XQueue
- Defined in:
- lib/xqueue_ruby/xqueue_ruby.rb
Defined Under Namespace
Classes: AuthenticationError, IOError, NoSuchQueueError, UpdateFailedError, XQueueError
Constant Summary collapse
- XQUEUE_DEFAULT_BASE_URI =
The base URI of the production Xqueue server.
'https://xqueue.edx.org'
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
The base URI used for this queue; won’t change for this queue even if you later change the value of
XQueue.base_uri
. -
#error ⇒ Object
readonly
Error message, if any, associated with last unsuccessful operation.
-
#queue_name ⇒ Object
readonly
Queue from which to pull, established in constructor.
Class Method Summary collapse
-
.base_uri ⇒ Object
The base URI used when new queue instances are created.
- .base_uri=(uri) ⇒ Object
Instance Method Summary collapse
-
#authenticate ⇒ Object
Authenticates to the server.
-
#authenticated? ⇒ Boolean
Returns
true
if the session has been properly authenticated to server, that is, after a successful call toauthenticate
or to any of the request methods that may have calledauthenticate
automatically. -
#get_submission ⇒ Object
Retrieve a submission from this queue.
-
#initialize(django_name, django_pass, user_name, user_pass, queue_name) ⇒ XQueue
constructor
Creates a new instance and attempts to authenticate to the queue server.
- #list_queues ⇒ Object
-
#put_result(header, score, correct = true, message = '') ⇒ Object
Record a result of grading something.
-
#queue_length ⇒ Object
Returns length of the queue as an integer >= 0.
Constructor Details
#initialize(django_name, django_pass, user_name, user_pass, queue_name) ⇒ XQueue
Creates a new instance and attempts to authenticate to the queue server.
-
django_name
,django_pass
: first set of auth credentials (see
above)
-
user_name
,user_pass
: second set of auth credentials (see
above)
-
queue_name
: logical name of the queue
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 75 def initialize(django_name, django_pass, user_name, user_pass, queue_name) @queue_name = queue_name @base_uri = XQueue.base_uri @django_auth = {'username' => django_name, 'password' => django_pass} @session = Mechanize.new @session.add_auth(@base_uri, user_name, user_pass) @valid_queues = nil @error = nil @authenticated = nil end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
The base URI used for this queue; won’t change for this queue even if you later change the value of XQueue.base_uri
46 47 48 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 46 def base_uri @base_uri end |
#error ⇒ Object (readonly)
Error message, if any, associated with last unsuccessful operation
38 39 40 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 38 def error @error end |
#queue_name ⇒ Object (readonly)
Queue from which to pull, established in constructor. You need a new XQueue
object if you want to use a different queue.
42 43 44 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 42 def queue_name @queue_name end |
Class Method Details
.base_uri ⇒ Object
The base URI used when new queue instances are created
49 50 51 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 49 def self.base_uri @@base_uri ||= URI(XQUEUE_DEFAULT_BASE_URI) end |
.base_uri=(uri) ⇒ Object
52 53 54 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 52 def self.base_uri=(uri) @@base_uri = URI(uri) end |
Instance Method Details
#authenticate ⇒ Object
Authenticates to the server. You can call this explicitly, but it is called automatically if necessary on the first request in a new session.
89 90 91 92 93 94 95 96 97 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 89 def authenticate response = request :post, '/xqueue/login/', @django_auth if response['return_code'] == 0 @authenticated = true else raise(AuthenticationError, "Authentication failure: #{response['content']}") end end |
#authenticated? ⇒ Boolean
Returns true
if the session has been properly authenticated to server, that is, after a successful call to authenticate
or to any of the request methods that may have called authenticate
automatically.
102 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 102 def authenticated? ; @authenticated ; end |
#get_submission ⇒ Object
Retrieve a submission from this queue. Returns nil if queue is empty, otherwise a new XQueue::Submission
instance.
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 129 def get_submission authenticate unless authenticated? if queue_length > 0 begin json_response = request(:get, '/xqueue/get_submission/', {:queue_name => @queue_name}) XQueueSubmission.parse_JSON(self, json_response) rescue StandardError => e # TODO: do something more interesting with the error. raise e end else nil end end |
#list_queues ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 118 def list_queues authenticate unless authenticated? if @valid_queues.nil? old, @queue_name = @queue_name, 'I_AM_NOT_A_QUEUE' begin queue_length rescue nil end end @valid_queues end |
#put_result(header, score, correct = true, message = '') ⇒ Object
Record a result of grading something. It may be easier to use XQueue::Submission#post_back, which marshals the information needed here automatically.
-
header
: secret header key (from ‘xqueue_header’ slot in the
‘content’ object of the original retrieved submission)
-
score
: integer number of points (not scaled) -
correct
: true (default) means show green checkmark, else red ‘x’ -
message
: (optional) plain text feedback; will be coerced to UTF-8
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 152 def put_result(header, score, correct=true, ='') payload = JSON.generate({ :xqueue_header => header, :xqueue_body => { :correct => (!!correct).to_s.capitalize, :score => score, :message => .encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?'), } }) response = request :post, '/xqueue/put_result', payload if response['return_code'] != 0 raise UpdateFailedError, response['content'] end end |
#queue_length ⇒ Object
Returns length of the queue as an integer >= 0.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/xqueue_ruby/xqueue_ruby.rb', line 105 def queue_length authenticate unless authenticated? response = request(:get, '/xqueue/get_queuelen/', {:queue_name => @queue_name}) if response['return_code'] == 0 # success response['content'].to_i elsif response['return_code'] == 1 && response['content'] =~ /^Valid queue names are: (.*)/i @valid_queues = $1.split(/,\s+/) raise NoSuchQueueError, "No such queue: valid queues are #{$1}" else raise IOError, response['content'] end end |