class GatherContent::Api::Base

Public Class Methods

new() click to toggle source
# File lib/gather_content/api/base.rb, line 5
def initialize
  raise RuntimeError, "Cannot initialize this interface!"
end

Public Instance Methods

fetch() click to toggle source
# File lib/gather_content/api/base.rb, line 32
def fetch
  @data ||= parse(get.body)
end
get(path_override = nil) click to toggle source
# File lib/gather_content/api/base.rb, line 9
def get(path_override = nil)
  connection.get do |request|
    request.url(path_override || path)
    request.params = params unless params.nil?
    request.headers['Accept'] = "application/vnd.gathercontent.v0.5+json"
  end
end
post(data = {}, path_override = nil) { |request| ... } click to toggle source
# File lib/gather_content/api/base.rb, line 17
def post(data = {}, path_override = nil)
  connection.post do |request|
    request.url(path_override || path)
    request.body = data
    request.headers['Accept'] = "application/vnd.gathercontent.v0.5+json"
    yield request if block_given?
  end
end
post_json(data = {}, path_override = nil) click to toggle source
# File lib/gather_content/api/base.rb, line 26
def post_json(data = {}, path_override = nil)
  post(data.to_json, path_override) do |request|
    request.headers['Content-type'] = "application/json"
  end
end
reset() click to toggle source
# File lib/gather_content/api/base.rb, line 36
def reset
  @data = nil
end

Protected Instance Methods

params() click to toggle source
# File lib/gather_content/api/base.rb, line 41
def params
  raise RuntimeError, "Expected this to be implemented in a subclass!"
end
path() click to toggle source
# File lib/gather_content/api/base.rb, line 45
def path
  raise RuntimeError, "Expected this to be implemented in a subclass!"
end

Private Instance Methods

connection() click to toggle source
# File lib/gather_content/api/base.rb, line 55
def connection
  config = GatherContent::Api::Config.instance

  @connection ||= Faraday.new(url: "#{config.host}:#{config.port}") do |faraday|
    faraday.request  :url_encoded
    faraday.request  :basic_auth, config.username, config.api_key
    # faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end
end
parse(data) click to toggle source
# File lib/gather_content/api/base.rb, line 50
def parse(data)
  parsed = JSON.parse(data)
  parsed['data']
end