Sha256: a47278974ae5e448e7d7c87f2a416128daa9c1c3ea1d1e1bfd3fa4ec0c1bef94

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

class Dailycred
  class Auth
    include ActiveModel::Validations
    include ActiveModel::Serialization

    validates_presence_of :email, :pass

    attr_accessor :client, :email, :pass, :authorized

    def initialize client, user = {}
      self.client = client
      self.authorized = false
      user.each do |k,v|
        self[k] = v if self.respond_to(k)
      end
    end

    def login
      if !self.valid?
        #it didn't work already, return false
        return false
      end

      response = JSON.parse client.login(self.to_hash)
      err_parser response

      return false if !self.valid?
      true
    end

    def to_hash
      {
        :email => self.email,
        :pass => self.pass
      }
    end

    private

    #response is a hash, which is
    #a json-parsed http response body
    def err_parser response
      if !response["worked"]
        self.authorized = false
        response["errors"].each do |err|
          attrib = err["attribute"]
          message = err["message"]
          if attrib == "form"
            self.errors.add_to_base message
          else
            if attrib == "user"
              self.errors.add :email, message
            elsif self.respond_to attrib
              self.errors.add attrib, message
            end
          end
        end
      end
    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dailycred-0.1.30 lib/dailycred/user.rb
dailycred-0.1.29 lib/dailycred/user.rb
dailycred-0.1.28 lib/dailycred/user.rb
dailycred-0.1.27 lib/user/user.rb
dailycred-0.1.26 lib/user/user.rb
dailycred-0.1.25 lib/user/user.rb
dailycred-0.1.24 lib/user/user.rb
dailycred-0.1.23 lib/user/user.rb
dailycred-0.1.22 lib/user/user.rb