class Feathr::Api::FeathrEndpoint

Attributes

client[RW]
prepended_path[RW]

Public Class Methods

api_path(path) click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 13
def api_path(path)
  define_method(:api_path) do
    [
      prepended_path,
      path.gsub(/\/?$/, '/')
    ].join('/').gsub(/^\//,'')
  end
end
define_all() click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 34
def define_all
  define_method(:all) do
    client.request(method: :get, path: api_path) do |data|
      data.map do |resource|
        if feathr_object == Feathr::Api::Membership
          feathr_object.new(resource)
        else
          id = resource["url"][/\/(\d*)\/$/, 1]
          feathr_object.new(resource, id)
        end
      end
    end
  end

  define_method(:first) do |count=1|
    count > 1 ? all.first(count) : all.first
  end

  define_method(:last) do |count=1|
    count > 1 ? all.last(count) : all.last
  end
end
define_create() click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 70
def define_create
  define_method(:create) do |query|
    client.request(method: :post, path: api_path, query: query) do |data|
      if feathr_object == Feathr::Api::Membership
        feathr_object.new(data)
      else
        id = data["url"][/\/(\d*)\/$/, 1]
        feathr_object.new(data, id)
      end
    end
  end
end
define_destroy() click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 98
def define_destroy
  define_method(:destroy) do |id|
    path = "#{ api_path }#{ id }/"
    client.request(method: :delete, path: path) do |data|
      data
    end
  end
end
define_find() click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 57
def define_find
  define_method(:find) do |id|
    path = "#{ api_path }#{ id }/"
    client.request(method: :get, path: path) do |data|
      if feathr_object == Feathr::Api::Membership
        feathr_object.new(data)
      else
        feathr_object.new(data, id)
      end
    end
  end
end
define_update() click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 83
def define_update
  define_method(:update) do |id, query|
    path = "#{ api_path }#{ id }/"
    client.request(method: :patch, path: path, query: query) do |data|
      if feathr_object == Feathr::Api::Membership
        feathr_object.new(data)
      else
        id = data["url"][/\/(\d*)\/$/, 1]
        feathr_object.new(data, id)
      end

    end
  end
end
feathr_endpoints(*endpoints) click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 28
def feathr_endpoints(*endpoints)
  endpoints.each do |name|
    self.send("define_#{ name }")
  end
end
feathr_object(klass) click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 22
def feathr_object(klass)
  define_method(:feathr_object) do
    klass
  end
end
new(client: Feathr::Client.default, prepended_path: '') click to toggle source
# File lib/feathr/api/feathr_endpoint.rb, line 7
def initialize(client: Feathr::Client.default, prepended_path: '')
  @client         = client
  @prepended_path = prepended_path
end