class SisRuby::Endpoint

Attributes

client[R]
id_field[R]
url[R]

Public Class Methods

new(client, endpoint_name, id_field = DEFAULT_ID_FIELDNAME) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 19
def initialize(client, endpoint_name, id_field = DEFAULT_ID_FIELDNAME)
  @client = client
  @url = "#{client.base_url}/api/v#{client.api_version}/#{endpoint_name}"
  @id_field = id_field
end

Public Instance Methods

==(other) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 97
def ==(other)
  client.equal?(other.client) && url == other.url && id_field == other.id_field
end
count(filter = {}) click to toggle source

Gets the total count of records, with optional filter

# File lib/sis_ruby/endpoint.rb, line 40
def count(filter = {})
  params = Params.new.filter(filter).limit(1).to_hash
  response = Typhoeus::Request.new(@url, params: params, headers: create_headers(true)).run
  response.headers['x-total-count'].to_i
end
create(obj) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 69
def create(obj)
  http_response = Typhoeus.post(@url, { body: obj.to_json, headers: get_headers(true) } )
  validate_response_success(http_response)
  JSON.parse(http_response.body)
end
create_enumerable(params = {}, chunk_size = ResultEnumerable::DEFAULT_CHUNK_RECORD_COUNT) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 34
def create_enumerable(params = {}, chunk_size = ResultEnumerable::DEFAULT_CHUNK_RECORD_COUNT)
  ResultEnumerable.new(self, params, chunk_size)
end
delete(id) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 76
def delete(id)
  id = id_from_param(id)
  http_response = Typhoeus.delete("#{@url}/#{id}", headers: get_headers(false))
  validate_response_success(http_response)
  JSON.parse(http_response.body)
end
get(id) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 61
def get(id)
  request = Typhoeus::Request.new("#{url}/#{id}", headers: create_headers(true))
  response = request.run
  validate_response_success(response)
  JSON.parse(response.body)
end
id_from_param(object) click to toggle source

This method is used to allow callers to pass either the id itself, or the record including an id key/value pair.

# File lib/sis_ruby/endpoint.rb, line 28
def id_from_param(object)
  id = object.is_a?(Hash) ? object[@id_field] : object
  id ? id : raise(MissingIdError.new("Missing required id field #{@id_field}}"))
end
list(params = {}) click to toggle source

Anything implementing a to_hash method can be passed as the query. This enables the passing in of SisParams objects.

# File lib/sis_ruby/endpoint.rb, line 49
def list(params = {})
  create_enumerable(params).each.to_a
end
list_as_openstructs(query = {}) click to toggle source

Anything implementing a to_hash method can be passed as the query. This enables the passing in of SisParams objects.

# File lib/sis_ruby/endpoint.rb, line 56
def list_as_openstructs(query = {})
  list(query).map { |h| OpenStruct.new(h) }
end
to_s() click to toggle source
# File lib/sis_ruby/endpoint.rb, line 92
def to_s
  self.class.name + ": endpoint = #{@url}, client = #{@client}"
end
update(obj) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 84
def update(obj)
  id = id_from_param(obj)
  http_response = self.class.put("#{@url}/#{id}", {body: obj.to_json, headers: get_headers(true) })
  validate_response_success(http_response)
  JSON.parse(http_response.body)
end

Private Instance Methods

get_headers(specify_content_type) click to toggle source
# File lib/sis_ruby/endpoint.rb, line 103
def get_headers(specify_content_type)
  create_headers(specify_content_type, @client.auth_token)
end