class GhostRb::Controllers::BaseController

@author Rene Hernandez @since 0.2

Attributes

client[R]
params[R]

Public Class Methods

new(client, params = nil) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 14
def initialize(client, params = nil)
  @client = client
  @params = Support::HashWithIndifferentAccess.new(params)
end

Public Instance Methods

all() click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 19
def all
  fetch_list.map { |r| @resource_klass.generate(r) }
end
fields(fields_str) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 35
def fields(fields_str)
  where(fields: fields_str)
end
filter(filter_query) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 39
def filter(filter_query)
  where(filter: filter_query)
end
find_by(kvp) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 51
def find_by(kvp)
  @params.keys.reject { |k| k == :include }.each do |key|
    @params.delete(key)
  end

  content = fetch_single(kvp)
  resource_klass.generate(content)
end
include(resources_str) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 43
def include(resources_str)
  where(include: resources_str)
end
limit(limit) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 23
def limit(limit)
  where(limit: limit)
end
order(order_str) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 31
def order(order_str)
  where(order: order_str)
end
page(page) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 27
def page(page)
  where(page: page)
end
where(hash) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 47
def where(hash)
  self.class.new(client, @params.merge(hash))
end

Private Instance Methods

error?(status) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 83
def error?(status)
  status >= 400
end
fetch_list() click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 74
def fetch_list
  query = client.default_query.merge(@params)
  status, content = client.get(endpoint, query)

  raise_fetch_list_error(status, content['errors']) if error?(status)

  content[@endpoint]
end
fetch_single(kvp) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 62
def fetch_single(kvp)
  endpoint = format_endpoint(kvp)
  query = client.default_query.merge(@params)
  status, content = client.get(endpoint, query)

  if error?(status)
    raise_fetch_single_error(kvp, status, content['errors'])
  end

  content
end
format_endpoint(kvp) click to toggle source
# File lib/ghost_rb/controllers/base_controller.rb, line 87
def format_endpoint(kvp)
  return [endpoint, kvp[:id]].join('/') if kvp.key?(:id)
  return [endpoint, 'slug', kvp[:slug]].join('/') if kvp.key?(:slug)

  raise Errors::InvalidEndpointError,
        "Invalid endpoint for #{endpoint}. Should be either :id or :slug"
end