Class: PlateApi::ObjectHandler
- Inherits:
-
Object
- Object
- PlateApi::ObjectHandler
- Defined in:
- lib/plate_api/object_handler.rb
Instance Attribute Summary collapse
-
#api_connector ⇒ Object
readonly
Returns the value of attribute api_connector.
-
#handling_class ⇒ Object
readonly
Returns the value of attribute handling_class.
Instance Method Summary collapse
- #create(parent, attributes, create_method = :post) ⇒ Object
- #delete(id) ⇒ Object
- #find(id) ⇒ Object
- #index(parent_class, parent_id, extra_params = {}) ⇒ Object
- #index_total_count(parent) ⇒ Object
-
#initialize(handling_class, api_connector) ⇒ ObjectHandler
constructor
A new instance of ObjectHandler.
- #update(id, attributes) ⇒ Object
Constructor Details
#initialize(handling_class, api_connector) ⇒ ObjectHandler
Returns a new instance of ObjectHandler.
7 8 9 10 11 12 |
# File 'lib/plate_api/object_handler.rb', line 7 def initialize(handling_class, api_connector) raise ArgumentError.new("`handling_class` given for #new is not valid") unless handling_class raise ArgumentError.new("`api_connector` given for #new is not valid") unless api_connector @handling_class = handling_class @api_connector = api_connector end |
Instance Attribute Details
#api_connector ⇒ Object (readonly)
Returns the value of attribute api_connector.
4 5 6 |
# File 'lib/plate_api/object_handler.rb', line 4 def api_connector @api_connector end |
#handling_class ⇒ Object (readonly)
Returns the value of attribute handling_class.
5 6 7 |
# File 'lib/plate_api/object_handler.rb', line 5 def handling_class @handling_class end |
Instance Method Details
#create(parent, attributes, create_method = :post) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/plate_api/object_handler.rb', line 38 def create(parent, attributes, create_method=:post) raise ArgumentError.new("`parent` given for #create is not valid") unless parent raise ArgumentError.new("`attributes` given for #create is not valid") unless attributes.is_a? Hash parameters = case create_method when :post {"data" => attributes} when :post_multipart attributes end result = @api_connector.send(create_method, collection_path(parent.class, parent.id), parameters) if result["data"] return new_object(result["data"]) else puts "PlateApi: No success: #{result}" return nil end end |
#delete(id) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/plate_api/object_handler.rb', line 58 def delete(id) raise ArgumentError.new("`id` given for #find is not valid") unless id result = @api_connector.delete(resource_path(id)) if result["data"] return new_object(result["data"]) else puts "PlateApi: No success: #{result}" return nil end end |
#find(id) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/plate_api/object_handler.rb', line 14 def find(id) raise ArgumentError.new("`id` given for #find is not valid") unless id result = @api_connector.get(resource_path(id)) if result["data"] return new_object(result["data"]) else puts "PlateApi: No success: #{result}" return nil end end |
#index(parent_class, parent_id, extra_params = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/plate_api/object_handler.rb', line 69 def index(parent_class, parent_id, extra_params={}) raise ArgumentError.new("`parent_id` given for #index is not valid") unless parent_id raise ArgumentError.new("`parent_class` given for #index is not valid") unless parent_class result = @api_connector.get(collection_path(parent_class, parent_id), extra_params) if result["data"] return result["data"].map{|x| new_object(x)} else puts "PlateApi: No success: #{result}" return nil end end |
#index_total_count(parent) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/plate_api/object_handler.rb', line 82 def index_total_count(parent) result = @api_connector.get(collection_path(parent.class, parent.id), per_page: 1) if result["meta"] return result["meta"]["pagination"]["total_records"] else puts "PlateApi: No success: #{result}" return nil end end |
#update(id, attributes) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/plate_api/object_handler.rb', line 25 def update(id, attributes) raise ArgumentError.new("`id` given for #update is not valid") unless id raise ArgumentError.new("`attributes` given for #update is not valid") unless attributes.is_a? Hash result = @api_connector.put(resource_path(id), {"data" => attributes}) if result["data"] return new_object(result["data"]) else puts "PlateApi: No success: #{result}" return nil end end |