Sha256: bfd5ffdd4f8da83cdf085d1c6bda3c6cb91aab818b07477ac06ab2673810f62f

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Locomotive::Coal

  class Client

    attr_reader :uri, :credentials

    attr_accessor :scoped_by_site

    def initialize(uri, credentials)
      if uri.blank? || credentials.blank?
        raise MissingURIOrCredentialsError.new('URI and/or credentials are missing')
      else
        @uri, @credentials = URI(uri), credentials
      end
    end

    def token
      @token ||= Resources::Token.new(uri, credentials).get
    end

    def my_account
      @my_account ||= Resources::MyAccount.new(uri, token)
    end

    def sites
      @sites ||= Resources::Sites.new(uri, token)
    end

    def contents
      @contents ||= Resources::Contents.new(uri, token)
    end

    def scope_by(site)
      if site.domains.include?(domain)
        self
      else
        new_uri = site.domains.first
        self.class.new(new_uri, self.credentials).tap do |_client|
          _client.scoped_by_site = true
        end
      end
    end

    private

    def domain
      self.uri.to_s =~ /^https?:\/\/([^\/:]+)*/
      $1
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms_coal-1.0.0.pre.alpha.1 lib/locomotive/coal/client.rb
locomotivecms_coal-1.0.0.pre.alpha lib/locomotive/coal/client.rb