Class: PlateApi::ObjectHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/plate_api/object_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handling_class, api_connector) ⇒ ObjectHandler

Returns a new instance of ObjectHandler.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/plate_api/object_handler.rb', line 7

def initialize(handling_class, api_connector)
  raise ArgumentError, "`handling_class` given for #new is not valid" unless handling_class
  raise ArgumentError, "`api_connector` given for #new is not valid" unless api_connector

  @handling_class = handling_class
  @api_connector = api_connector
end

Instance Attribute Details

#api_connectorObject (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_classObject (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

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/plate_api/object_handler.rb', line 39

def create(parent, attributes, create_method = :post)
  raise ArgumentError, "`parent` given for #create is not valid" unless parent
  raise ArgumentError, "`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"]
    new_object(result["data"])
  else
    raise ResponseError, result
  end
end

#delete(id) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
# File 'lib/plate_api/object_handler.rb', line 59

def delete(id)
  raise ArgumentError, "`id` given for #find is not valid" unless id

  result = @api_connector.delete(resource_path(id))
  if result["data"]
    new_object(result["data"])
  else
    raise ResponseError, result
  end
end

#find(id) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/plate_api/object_handler.rb', line 15

def find(id)
  raise ArgumentError, "`id` given for #find is not valid" unless id

  result = @api_connector.get(resource_path(id))
  if result["data"]
    new_object(result["data"])
  else
    raise ResponseError, result
  end
end

#index(parent_class, parent_id, extra_params = {}, return_raw = false) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
# File 'lib/plate_api/object_handler.rb', line 70

def index(parent_class, parent_id, extra_params = {}, return_raw = false)
  raise ArgumentError, "`parent_id` given for #index is not valid" unless parent_id
  raise ArgumentError, "`parent_class` given for #index is not valid" unless parent_class

  result = @api_connector.get(collection_path(parent_class, parent_id), extra_params)
  raise ResponseError, result unless result["data"]
  return result if return_raw

  result["data"].map { |x| new_object(x) }
end

#index_block(parent_class, parent_id, extra_params = {}, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/plate_api/object_handler.rb', line 81

def index_block(parent_class, parent_id, extra_params = {}, &block)
  extra_params[:page] = extra_params.delete("page") if extra_params["page"]
  extra_params[:page] = 1 unless extra_params[:page]

  pagination = index_block_iteration(parent_class, parent_id, extra_params, &block)
  return unless pagination

  while pagination["page"] < pagination["total_pages"]
    extra_params.merge!(page: (pagination["page"] + 1))
    pagination = index_block_iteration(parent_class, parent_id, extra_params, &block)
    break unless pagination
  end
end

#index_total_count(parent) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/plate_api/object_handler.rb', line 95

def index_total_count(parent)
  result = @api_connector.get(collection_path(parent.class, parent.id), per_page: 1)
  if result["meta"]
    result["meta"]["pagination"]["total_records"]
  else
    raise ResponseError, result
  end
end

#update(id, attributes) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plate_api/object_handler.rb', line 26

def update(id, attributes)
  raise ArgumentError, "`id` given for #update is not valid" unless id
  raise ArgumentError, "`attributes` given for #update is not valid" unless attributes.is_a?(Hash)

  result = @api_connector.put(resource_path(id), { "data" => attributes })

  if result["data"]
    new_object(result["data"])
  else
    raise ResponseError, result
  end
end