module Tatooine::Resource::ClassMethods

Public Instance Methods

connection() click to toggle source
# File lib/tatooine/resource.rb, line 82
def connection
  options = {
    :headers => {
      "User-Agent" => "Tatooine Ruby Gem - #{Tatooine::VERSION}"
    },
    :url => "#{Tatooine::API_BASE}#{@resource_path}/"
  }
  @connection ||= Faraday.new(options) do |faraday|
    faraday.response :raise_http_error
    faraday.response :dates
    faraday.response :json
    faraday.adapter  Faraday.default_adapter
  end
end
count() click to toggle source
# File lib/tatooine/resource.rb, line 60
def count
  @count || get_count
end
get(id) click to toggle source
# File lib/tatooine/resource.rb, line 72
def get(id)
  load_schema
  result = connection.get("#{id}/")
  new(result.body)
end
list(opts={}) click to toggle source
# File lib/tatooine/resource.rb, line 50
def list(opts={})
  load_schema
  url = opts.delete("url") || ""
  body = connection.get(url, opts).body
  @count = body["count"]
  @next = body["next"]
  @previous = body["previous"]
  body["results"].map { |result| new(result) }
end
next() click to toggle source
# File lib/tatooine/resource.rb, line 64
def next
  list "url" => @next if @next
end
previous() click to toggle source
# File lib/tatooine/resource.rb, line 68
def previous
  list "url" => @previous if @previous
end
schema() click to toggle source
# File lib/tatooine/resource.rb, line 78
def schema
  load_schema
end

Private Instance Methods

get_count() click to toggle source
# File lib/tatooine/resource.rb, line 116
def get_count
  @count = connection.get("").body["count"]
end
load_schema() click to toggle source
# File lib/tatooine/resource.rb, line 99
def load_schema
  if !@schema
    @schema = connection.get("schema").body
    @schema["properties"].keys.each do |property|
      define_method property.to_sym do
        if @properties.nil? || @properties.keys.length <= 1
          result = self.class.connection.get(@url)
          @properties = result.body
          set_properties
        end
        instance_variable_get("@#{property}")
      end
    end
  end
  @schema
end
resource_path(path) click to toggle source
# File lib/tatooine/resource.rb, line 120
def resource_path(path)
  @resource_path = path
end