lib/clavem/authorizer.rb in clavem-2.1.0 vs lib/clavem/authorizer.rb in clavem-2.2.0

- old
+ new

@@ -32,23 +32,22 @@ # The block must accept a querystring hash (which all values are arrays) and return a token or `nil` if the authentication was denied. # @attribute token # @return [String] The token obtained by the remote endpoint. # @attribute status # @return [Symbol] The status of the request. Can be `:succeeded`, `:denied`, `:failed` and `:waiting`. - # @attribute :i18n - # @return [R18N::Translation] A localizer object. + # @attribute [r] :i18n + # @return [Bovem::I18n] A localizer object. class Authorizer - include R18n::Helpers attr_accessor :url attr_accessor :host attr_accessor :port attr_accessor :command attr_accessor :timeout attr_accessor :response_handler attr_accessor :token attr_accessor :status - attr_accessor :i18n + attr_reader :i18n # Returns a unique (singleton) instance of the authorizer. # # @param host [String] The host address on which listening for replies. Default is `localhost`. # @param port [Fixnum] The port on which listening for replies. Default is `7772`. @@ -56,13 +55,13 @@ # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. # Default is `0`, which means *disabled*. # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}. # @param force [Boolean] If to force recreation of the instance. # @return [Authorizer] The unique (singleton) instance of the authorizer. - def self.instance(host = "localhost", port = 7772, command = nil, timeout = 0, force = false, &response_handler) + def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler) @instance = nil if force - @instance ||= Clavem::Authorizer.new(host, port, command, timeout, &response_handler) + @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler) @instance end # Creates a new authorizer. # @@ -71,12 +70,12 @@ # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`. # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. # Default is `0`, which means *disabled*. # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}. # @return [Authorizer] The new authorizer. - def initialize(host = "localhost", port = 7772, command = nil, timeout = 0, &response_handler) - @i18n = self.localize + def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler) + @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") @host = host.ensure_string @port = port.to_integer @command = command.ensure_string @timeout = timeout.to_integer @response_handler = response_handler @@ -103,11 +102,11 @@ begin perform_request process_response rescue => e @status = :failed - raise Clavem::Exceptions::Failure.new(@i18n.errors.response_failure(e.to_s)) + raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s)) end succeeded? end @@ -119,22 +118,13 @@ end # Returns the response handler for the authorizer. # def response_handler - @response_handler || Proc.new {|querystring| (querystring || {})["oauth_token"] } + @response_handler || ->(querystring) { (querystring || {})["oauth_token"] } end - # Set the current locale for messages. - # - # @param locale [String] The new locale. Default is the current system locale. - # @return [R18n::Translation] The new translations object. - def localize(locale = nil) - @i18n_locales_path ||= ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") - R18n::I18n.new([locale || :en, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path).t.clavem - end - # Checks if authentication succeeded. # # @return [Boolean] `true` if authorization succeeded, `false otherwise`. def succeeded? @status == :succeeded @@ -160,37 +150,34 @@ def waiting? @status == :waiting end private - # sanitize_arguments - def sanitize_arguments - @host = "localhost" if @host.blank? - @port = 7772 if @port.to_integer < 1 - @command = "open \"{{URL}}\"" if @command.blank? - @timeout = 0 if @timeout < 0 - end - # Performs the authentication request. - def perform_request - # Open the oAuth endpoint into the browser - begin - Kernel.system(@command.gsub("{{URL}}", @url.ensure_string)) - rescue => e - raise Clavem::Exceptions::Failure.new(@i18n.errors.open_failure(@url.ensure_string, e.to_s)) - end - end + # :nodoc: + def sanitize_arguments + @host = "localhost" if @host.blank? + @port = 7772 if @port.to_integer < 1 + @command = "open \"{{URL}}\"" if @command.blank? + @timeout = 0 if @timeout < 0 + end - # Processes the authentication response. - def process_response - begin - server = Thread.new do - Clavem::Server.new(self) - end + # :nodoc: + def perform_request + # Open the oAuth endpoint into the browser + Kernel.system(@command.gsub("{{URL}}", @url.ensure_string)) + rescue => e + raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s)) + end - @timeout > 0 ? server.join(@timeout) : server.join - rescue Interrupt - raise Clavem::Exceptions::Failure.new(@i18n.errors.interrupted) - end + # :nodoc: + def process_response + server = Thread.new do + Clavem::Server.new(self) end + + @timeout > 0 ? server.join(@timeout) : server.join + rescue Interrupt + raise(Clavem::Exceptions::Failure, @i18n.interrupted) + end end -end \ No newline at end of file +end