Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Extract errors to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Mar 26, 2017
1 parent d788ebe commit d7ad221
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/robinet/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Robinet
module Errors
class RateLimitExceeded < StandardError
def http_response
[429, {}, ['Rate Limit Exceeded']]
end
end

class InvalidRateLimit < StandardError
end
end
end
5 changes: 1 addition & 4 deletions lib/robinet/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ class Parser
SINGLE_EXPR = /\s*([0-9]+)\s*(\/|\s*per\s*)\s*([0-9]+)*\s*(hour|minute|second|day|month|year)s?\s*/i
EXPR = /^#{SINGLE_EXPR}(:?#{SEPARATORS}#{SINGLE_EXPR})*$/i

class InvalidRateLimit < StandardError

end

def self.extract(limit_string)
raise InvalidRateLimit unless EXPR.match(limit_string)
raise Errors::InvalidRateLimit unless EXPR.match(limit_string)
limit_string.split(SEPARATORS).map do |limit|
_, amount, _, multiples, granularity = *SINGLE_EXPR.match(limit)
Robinet::Limit.new(amount, multiples, granularity)
Expand Down
2 changes: 1 addition & 1 deletion spec/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe Robinet::Parser do
describe '#test' do
it 'raises an error for an empty limit' do
expect{Robinet::Parser.extract('')}.to raise_error Robinet::Parser::InvalidRateLimit
expect{Robinet::Parser.extract('')}.to raise_error Robinet::Errors::InvalidRateLimit
end
it 'returns an array of a single limit' do
expect(Robinet::Parser.extract('5 per minute')).to eq([Robinet::Limit.new(5, 1, :minute)])
Expand Down

0 comments on commit d7ad221

Please sign in to comment.