Sha256: 780401ddd1c18f2f5fe31f446ff3353283c33440fc092d86643062033c43563a

Contents?: true

Size: 947 Bytes

Versions: 4

Compression:

Stored size: 947 Bytes

Contents

require 'httpclient'
require 'uri'
require 'json'

module Glman
  module Repos
    class UsersRepo
      PER_PAGE = 10000
      def initialize(opts={})
        @gitlab_url    = opts.fetch(:url)
        @private_token = opts.fetch(:private_token)
      end
      def list
        all
      end

      def get(id)
        JSON.parse(client.get(url(id)).body)
      end

      def find(opts={})
        opts = Hash[opts.map{ |k, v| [k.to_s, v] }]
        all.each do |user|
          return user if user.eql?(user.merge(opts))
        end
        nil
      end

      private
      attr_reader :gitlab_url, :private_token

      def all
        JSON.parse(client.get(url).body)
      end

      def url(id=nil)
        url = [gitlab_url, 'api', 'v3', 'users', id, "?private_token=#{private_token}&per_page=#{PER_PAGE}"].compact.join('/')
        URI.join(url)
      end

      def client
        @client ||= HTTPClient.new
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glman-0.1.3 lib/glman/repos/users_repo.rb
glman-0.1.2 lib/glman/repos/users_repo.rb
glman-0.1.1 lib/glman/repos/users_repo.rb
glman-0.1.0 lib/glman/repos/users_repo.rb