lib/knackhq/client.rb in knackhq-client-0.1.0 vs lib/knackhq/client.rb in knackhq-client-0.1.1

- old
+ new

@@ -11,63 +11,87 @@ @x_knack_application_id = x_knack_application_id @x_knack_rest_api_key = x_knack_rest_api_key end def objects - hash_request = request.objects.get.to_h[:objects] - hash_request.map { |hash| symbolize_hash_keys(hash) } + hash_request = request + .objects + .get + .to_h[:objects] + return [] if hash_request.empty? + symbolize_hash_keys!(hash_request) end def object(key) hash_request = request.objects.get(key).to_h return [] if hash_request.empty? transform_hash_keys = hash_request[:object]['fields'] - - transform_hash_keys.map { |hash| symbolize_hash_keys(hash) } + symbolize_hash_keys!(transform_hash_keys) end def fields(object) - hash_request = request.objects(object).fields.get.to_h + hash_request = request_object(object, :fields) return [] if hash_request.empty? transform_hash_keys = hash_request[:fields] - transform_hash_keys.map { |hash| symbolize_hash_keys(hash) } + symbolize_hash_keys!(transform_hash_keys) end def records(object) - hash_request = request.objects(object).records.get.to_h + hash_request = request_object(object, :records) return [] if hash_request.empty? transform_hash_keys = hash_request[:records] - transform_hash_keys.map { |hash| symbolize_hash_keys(hash) } + symbolize_hash_keys!(transform_hash_keys) end def records_by_page(object, page_number) - hash_request = request.objects(object).records + hash_request = request + .objects(object) + .records .get(:params => { :page => page_number, - :rows_per_page => 500 }).to_h + :rows_per_page => 500 }) + .to_h return [] if hash_request.empty? transform_hash_keys = hash_request[:records] - transform_hash_keys.map { |hash| symbolize_hash_keys(hash) } + symbolize_hash_keys!(transform_hash_keys) end def records_info(object) - hash_request = request.objects(object).records - .get(:params => { :rows_per_page => 500 }).to_h + hash_request = request + .objects(object) + .records + .get(:params => { :rows_per_page => 500 }) + .to_h return [] if hash_request.empty? hash_request.delete(:records) [hash_request] end + def update_record(object, knackhq_id, json) + hash_request = request + .objects(object) + .records(knackhq_id) + .put(:body => json) + .to_h + !hash_request.empty? + end + private def request headers = { 'x-knack-application-id' => @x_knack_application_id.dup, 'Content-Type' => 'application/json', 'x-knack-rest-api-key' => @x_knack_rest_api_key.dup } Blanket.wrap(@base_uri.dup, :headers => headers) end - def symbolize_hash_keys(value) - Hashie.symbolize_keys!(value) + def request_object(object, function) + request.objects(object).send(function).get.to_h + end + + def symbolize_hash_keys!(transform_hash_keys) + transform_hash_keys.map do |hash| + Hashie.symbolize_keys!(hash) + end end end end