Sha256: 842509f2d0acd53e00ccd5c45ca91f33a063311c77f6a5c968f0132b840bf217

Contents?: true

Size: 1.39 KB

Versions: 31

Compression:

Stored size: 1.39 KB

Contents

module Tickethub
  class User < Resource
    path '/user', singleton: true

    require_relative 'user/supplier'

    collection :suppliers, User::Supplier

    association :token, Tickethub::Token

    def self.create(attributes)
      response = Tickethub.endpoint['/user'].post(attributes)
      self[attributes.slice 'email', 'password']
    rescue Tickethub::ResourceInvalid => err
      new nil, Tickethub::Response.new(err.response).decoded
    end

    def self.reset(email)
      Tickethub.endpoint['/user/reset'].post email: email
    end

    def self.verify(attributes)
      response = Tickethub.endpoint['/user/verify'].post(attributes)
      self[email: response.decoded['email'], password: attributes['password']]
    rescue Tickethub::ResourceInvalid => err
      new nil, Tickethub::Response.new(err.response).decoded
    end

    def self.[](attributes)
      token = if attributes[:email]
        Tickethub.endpoint['/user/token'].post(grant_type: 'password',
          username: attributes[:email], password: attributes[:password])
            .decoded['access_token']
      else
        attributes[:token].is_a?(String) ? attributes[:token]
          : attributes[:token][:access_token]
      end

      endpoint = Tickethub.endpoint(auth_type: :bearer, password: token)['/user']
      self.new endpoint
    end

    def full_name
      [first_name, last_name].reject(&:nil?).join ' '
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
tickethub-0.3.100 lib/tickethub/user.rb
tickethub-0.3.99 lib/tickethub/user.rb
tickethub-0.3.98 lib/tickethub/user.rb
tickethub-0.3.97 lib/tickethub/user.rb
tickethub-0.3.96 lib/tickethub/user.rb
tickethub-0.3.95 lib/tickethub/user.rb
tickethub-0.3.94 lib/tickethub/user.rb
tickethub-0.3.93 lib/tickethub/user.rb
tickethub-0.3.92 lib/tickethub/user.rb
tickethub-0.3.91 lib/tickethub/user.rb
tickethub-0.3.90 lib/tickethub/user.rb
tickethub-0.3.89 lib/tickethub/user.rb
tickethub-0.3.88 lib/tickethub/user.rb
tickethub-0.3.87 lib/tickethub/user.rb
tickethub-0.3.86 lib/tickethub/user.rb
tickethub-0.3.85 lib/tickethub/user.rb
tickethub-0.3.84 lib/tickethub/user.rb
tickethub-0.3.82 lib/tickethub/user.rb
tickethub-0.3.81 lib/tickethub/user.rb
tickethub-0.3.80 lib/tickethub/user.rb