class MiddlemanHeadless::Interface

Attributes

build[R]
options[R]

Public Class Methods

new(options, build) click to toggle source
# File lib/middleman-headless/interface.rb, line 8
def initialize(options, build)
  @options = options
  @build = build

  @entries_cache = {}
  @asset_cache = {}

  @client = OAuth2::Client.new(
    @options.app_key,
    @options.app_secret,
    site: @options.address,
    token_url: '/auth/token',
    auth_scheme: :basic_auth,
    ssl: {
      verify: @options.verify
    }
  )

  @access_token = @client.client_credentials.get_token scope: ''
end

Public Instance Methods

asset(id) click to toggle source
# File lib/middleman-headless/interface.rb, line 47
def asset(id)
  return nil if id.blank?
  path = "asset/#{@options.space}/#{id}"
  @asset_cache[id.to_sym] ||= Asset.new(get(path).with_indifferent_access, self)
end
entries(content_type) click to toggle source
# File lib/middleman-headless/interface.rb, line 33
def entries(content_type)
  content_type = content_type[:slug] if content_type.is_a?(Hash)
  path = "entries/#{@options.space}/#{content_type}?render-documents=true"
  @entries_cache[content_type.to_sym] ||= get(path).map do |item|
    Entry.new(item.with_indifferent_access, self)
  end
end
entry(content_type, id) click to toggle source
# File lib/middleman-headless/interface.rb, line 41
def entry(content_type, id)
  entries(content_type).find do |item|
    item.id == id
  end
end
method_missing(key) click to toggle source
# File lib/middleman-headless/interface.rb, line 57
def method_missing(key)
  entries(key.to_s)
end
space() click to toggle source
# File lib/middleman-headless/interface.rb, line 29
def space
  @space ||= Space.new(get("space/#{@options.space}").with_indifferent_access, self)
end
token() click to toggle source
# File lib/middleman-headless/interface.rb, line 53
def token
  @access_token.token
end

Protected Instance Methods

get(path) click to toggle source
# File lib/middleman-headless/interface.rb, line 63
def get(path)
  path = "/content/#{path.to_s}"
  path = "#{path}?preview=enabled" if @options.preview
  JSON.parse(@access_token.get(path).body)
end