Sha256: 9bfe8fa25d59098ea96757f45b9f08985c2130dbb9d2b353e1ef32f19cbd42f4

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Gatleon
  module Rails
    module Authform
      class User
        PERMITTED_CHARS = /\A[a-zA-Z0-9_)]*\z/

        def initialize(json:, _form_secret_key:, _authform_base_url:)
          @json = json

          @_form_secret_key = _form_secret_key
          @_authform_base_url = _authform_base_url
        end

        # Getters
        #
        def _id
          @json["_id"]
        end

        def _email
          @json["_email"]
        end

        # Getters
        #
        def [](key)
          @json[key.to_s]
        end

        # Setters
        #
        def []=(key, value)
          key = _clean_key(key)

          raise ArgumentError, "can't set reserved field name #{key}" if key[0] == "_" # anything starting with _

          raise ArgumentError, "can't set empty field name" if key == ""

          raise ArgumentError, "only characters a-z, A-Z, 0-9, and _ permitted in field name" unless key.match?(PERMITTED_CHARS)

          @json[key] = value.to_s
        end

        private

        def _persist(key, value)
          uri = _persist_url(key, vlue)
          
          Net::HTTP.get_response(uri) # TODO: move to post request
        end

        def _persist_url(key, value)
          URI("#{@_authform_base_url}/v1/setUser?_id=#{_id}&_secretKey=#{@_form_secret_key}&#{key}=#{value}")
        end

        def _clean_key(k_or_v)
          k_or_v.to_s.strip
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gatleon-rails-0.3.1 lib/gatleon/rails/authform/user.rb
gatleon-rails-0.3.0 lib/gatleon/rails/authform/user.rb
gatleon-rails-0.2.0 lib/gatleon/rails/authform/user.rb