Sha256: ce1139d0b5d3cb3625109ff42ac7fe591c1c0a6ca502293048e2982d4f9329b0

Contents?: true

Size: 1.07 KB

Versions: 45

Compression:

Stored size: 1.07 KB

Contents

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

    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(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

45 entries across 45 versions & 1 rubygems

Version Path
tickethub-0.2.4 lib/tickethub/user.rb
tickethub-0.2.3 lib/tickethub/user.rb
tickethub-0.2.2 lib/tickethub/user.rb
tickethub-0.2.1 lib/tickethub/user.rb
tickethub-0.2.0 lib/tickethub/user.rb