Class: SisRuby::Endpoint
- Inherits:
-
Object
- Object
- SisRuby::Endpoint
- Includes:
- GetHelper
- Defined in:
- lib/sis_ruby/endpoint.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id_field ⇒ Object
readonly
Returns the value of attribute id_field.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#count(filter = {}) ⇒ Object
Gets the total count of records, with optional filter.
- #create(obj) ⇒ Object
- #create_enumerable(params = {}, chunk_size = ResultEnumerable::DEFAULT_CHUNK_RECORD_COUNT) ⇒ Object
- #delete(id) ⇒ Object
- #get(id) ⇒ Object
-
#id_from_param(object) ⇒ Object
This method is used to allow callers to pass either the id itself, or the record including an id key/value pair.
-
#initialize(client, endpoint_name, id_field = DEFAULT_ID_FIELDNAME) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#list(params = {}) ⇒ Object
Anything implementing a to_hash method can be passed as the query.
-
#list_as_openstructs(query = {}) ⇒ Object
Anything implementing a to_hash method can be passed as the query.
- #to_s ⇒ Object
- #update(obj) ⇒ Object
Methods included from GetHelper
#create_headers, #typhoeus_get, #validate_response_success
Constructor Details
#initialize(client, endpoint_name, id_field = DEFAULT_ID_FIELDNAME) ⇒ Endpoint
Returns a new instance of Endpoint.
19 20 21 22 23 |
# 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 |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
16 17 18 |
# File 'lib/sis_ruby/endpoint.rb', line 16 def client @client end |
#id_field ⇒ Object (readonly)
Returns the value of attribute id_field.
16 17 18 |
# File 'lib/sis_ruby/endpoint.rb', line 16 def id_field @id_field end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
16 17 18 |
# File 'lib/sis_ruby/endpoint.rb', line 16 def url @url end |
Instance Method Details
#==(other) ⇒ Object
97 98 99 |
# 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 = {}) ⇒ Object
Gets the total count of records, with optional filter
40 41 42 43 44 |
# 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) ⇒ Object
69 70 71 72 73 |
# 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) ⇒ Object
34 35 36 |
# 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) ⇒ Object
76 77 78 79 80 81 |
# 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) ⇒ Object
61 62 63 64 65 66 |
# 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) ⇒ Object
This method is used to allow callers to pass either the id itself, or the record including an id key/value pair.
28 29 30 31 |
# 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 = {}) ⇒ Object
Anything implementing a to_hash method can be passed as the query. This enables the passing in of SisParams objects.
49 50 51 |
# File 'lib/sis_ruby/endpoint.rb', line 49 def list(params = {}) create_enumerable(params).each.to_a end |
#list_as_openstructs(query = {}) ⇒ Object
Anything implementing a to_hash method can be passed as the query. This enables the passing in of SisParams objects.
56 57 58 |
# File 'lib/sis_ruby/endpoint.rb', line 56 def list_as_openstructs(query = {}) list(query).map { |h| OpenStruct.new(h) } end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/sis_ruby/endpoint.rb', line 92 def to_s self.class.name + ": endpoint = #{@url}, client = #{@client}" end |
#update(obj) ⇒ Object
84 85 86 87 88 89 |
# 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 |