Sha256: e09c0a5011171334ed2e34401e3bc68398f8d671f71be5c52c14dda9a783cbce

Contents?: true

Size: 1.06 KB

Versions: 14

Compression:

Stored size: 1.06 KB

Contents

module Tickethub
  class User < Resource
    path '/user'

    require_relative 'user/app'
    require_relative 'user/supplier'

    collection :apps, User::App
    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.[](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['/user', auth_type: :bearer, password: token]
      self.new endpoint
    end

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
tickethub-0.1.4 lib/tickethub/user.rb
tickethub-0.1.3 lib/tickethub/user.rb
tickethub-0.1.2 lib/tickethub/user.rb
tickethub-0.0.14 lib/tickethub/user.rb
tickethub-0.0.12 lib/tickethub/user.rb
tickethub-0.0.11 lib/tickethub/user.rb
tickethub-0.0.10 lib/tickethub/user.rb
tickethub-0.0.9 lib/tickethub/user.rb
tickethub-0.0.8 lib/tickethub/user.rb
tickethub-0.0.7 lib/tickethub/user.rb
tickethub-0.0.6 lib/tickethub/user.rb
tickethub-0.0.5 lib/tickethub/user.rb
tickethub-0.0.4 lib/tickethub/user.rb
tickethub-0.0.3 lib/tickethub/user.rb