Class: ResponseType

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ ResponseType

Returns a new instance of ResponseType.



2
3
4
5
6
7
8
# File 'lib/response_type.rb', line 2

def initialize(attrs)
  attrs.each do |key, value|
    self.instance_eval { class << self; self end }.send(:attr_accessor, key.to_sym)
    self.send(key+"=", parse_value(key, value))
  end
  self
end

Class Method Details

.timestamped_keysObject



18
19
20
# File 'lib/response_type.rb', line 18

def self.timestamped_keys
  ["created_at", "started_at", "updated_at", "finished_at"]
end

Instance Method Details

#attribute_namesObject



33
34
35
# File 'lib/response_type.rb', line 33

def attribute_names
  instance_variables.collect{|ivar| ivar.to_s.gsub("@", "")}
end

#attributesObject



29
30
31
# File 'lib/response_type.rb', line 29

def attributes
  Hash[useful_attribute_names.zip(useful_attribute_names.map{|attribute| self.send(attribute)})]
end

#parse_value(key, value) ⇒ Object



10
11
12
# File 'lib/response_type.rb', line 10

def parse_value(key, value)
  set_as_time_if_timestamp(key, value)
end

#set_as_time_if_timestamp(key, value) ⇒ Object



14
15
16
# File 'lib/response_type.rb', line 14

def set_as_time_if_timestamp(key, value)
  self.class.timestamped_keys.include?(key)  && !value.nil? ? Time.parse(value) : value
end

#to_json(*a) ⇒ Object



22
23
24
25
26
27
# File 'lib/response_type.rb', line 22

def to_json(*a)
  {
    'json_class'   => self.class.name,
    'data'         => attributes
  }.to_json(*a)
end

#useful_attribute_namesObject



37
38
39
# File 'lib/response_type.rb', line 37

def useful_attribute_names
  attribute_names-["client"]
end