Sha256: d0020f2ef18ee2aca78fe143d749b074ee0ef76b5b7464d6f69cc623ef060851

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'addressable/uri'
require 'faraday'

module Spearly
  module Auth
    class User
      attr_reader :data

      def initialize(token)
        @token = token
      end

      def method_missing(name, *_args)
        @data[name.to_s]
      end

      def find
        user_profile_uri = "#{ENV['SPEARLY_API_URL']}/profile"
        url = Addressable::URI.parse(user_profile_uri).normalize.to_s
        client = Faraday.default_connection

        res = client.get(url,
                         nil,
                         'Accept' => 'application/vnd.spearly.v2+json',
                         'Authorization' => @token)

        return unless res.status == 200

        @data = JSON.parse(res.body)['data']
      end

      def teams
        teams_uri = "#{ENV['SPEARLY_API_URL']}/teams"
        url = Addressable::URI.parse(teams_uri).normalize.to_s
        client = Faraday.default_connection

        res = client.get(url,
                         nil,
                         'Accept' => 'application/vnd.spearly.v2+json',
                         'Authorization' => @token)

        return [] unless res.status == 200

        team_hashes = JSON.parse(res.body)['data']

        team_hashes.map do |team_hash|
          Spearly::Auth::Team.new(team_hash)
        end
      end

      def to_json(*_args)
        @data.to_json
      end

      def as_json
        @data.as_json
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spearly-sdk-ruby-0.8.0 lib/spearly/auth/user.rb
spearly-sdk-ruby-0.7.0 lib/spearly/auth/user.rb
spearly-sdk-ruby-0.6.0 lib/spearly/auth/user.rb
spearly-sdk-ruby-0.5.0 lib/spearly/auth/user.rb
spearly-sdk-ruby-0.4.0 lib/spearly/auth/user.rb