Module: TicketEvolution::Modules::Update

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ticket_evolution/modules/update.rb', line 4

def self.included(klass)
  Class.new{extend SingularClass}.singular_class(klass.name).send(:include, Module.new{
    def update_attributes(params)
      handle_update_response(endpoint.update(params))
    end

    def save
      atts = self.attributes
      id = atts.delete(:id)
      handle_update_response(endpoint.update(atts))
    end

    def handle_update_response(response)
      if response.is_a?(TicketEvolution::ApiError)
        response
      else
        self.attributes = response.attributes
        self
      end
    end
    private :handle_update_response
  })
end

Instance Method Details

#build_for_update(response) ⇒ Object



34
35
36
37
38
39
# File 'lib/ticket_evolution/modules/update.rb', line 34

def build_for_update(response)
  singular_class.new(response.body.merge({
    :status_code => response.response_code,
    :server_message => response.server_message
  }))
end

#update(params = nil, &handler) ⇒ Object



28
29
30
31
32
# File 'lib/ticket_evolution/modules/update.rb', line 28

def update(params = nil, &handler)
  ensure_id
  handler ||= method(:build_for_update)
  request(:PUT, nil, params, &handler)
end