Class: TheCity::ApiWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/writers/api_writer.rb

Overview

This adapter is the standard for all saving objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



5
6
7
# File 'lib/writers/api_writer.rb', line 5

def error_messages
  @error_messages
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



5
6
7
# File 'lib/writers/api_writer.rb', line 5

def response_code
  @response_code
end

Instance Method Details

#delete_objectObject

Deletes this object.

Returns:

  • True or ID on success, otherwise false.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/writers/api_writer.rb', line 41

def delete_object
  success = true

  if @url_data_delete_path.nil?
    @error_messages = ["DELETE not implemented for #{self.class.to_s}"]
    return false
  end

  begin
    # @url_data_path should be the same as :put if this object is already
    # setup and mapped to an object that exists
    response = TheCity::admin_request(:delete, @url_data_delete_path)           
    success = response.code == 204 ? true : false # No content but is a success
  rescue Exception => e  
    @error_messages = e.message.split(',')
    success = false
  end 

  return success
end

#save_objectObject

Saves this object.

Returns:

  • True or ID on success, otherwise false.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/writers/api_writer.rb', line 10

def save_object
  @url_data_params ||= {}
  success = true

  if @url_data_path.nil?
    @error_messages = ["#{@url_action.to_s.upcase} not implemented for #{self.class.to_s}"]
    return false
  end

  if @updatable_fields and !@updatable_fields.empty?
    fields_to_remove = @url_data_params.keys - @updatable_fields  
    fields_to_remove.each { |ftr| @url_data_params.delete(ftr) }
  end

  begin
    response = TheCity::admin_request(@url_action, @url_data_path, nil, @url_data_params.to_json)   
    @response_code = response.code
    # No content but is a success
    success = response.code == 204 ? {'success' => true} : JSON.parse(response.body)  
  rescue Exception => e  
    @error_messages = e.message.split(',')
    success = false
  end 

  return success
end