lib/keepcon/entity.rb
require 'gyoku'
require 'action_view'
require 'keepcon/entity/response'
module Keepcon
class Entity
include ActionView::Helpers::TagHelper
attr_accessor :context, :instance
def initialize(params = {})
params.each { |k, v| send("#{k}=", v) }
end
def translate
translation = context.attribute_names.map do |attr_name|
[context.translate(attr_name), instance.send(attr_name)]
end
Hash[translation]
end
def to_xml
Gyoku.xml(xml_hash)
end
def send_data(mode)
parse_response(context.client.content_request(to_xml, mode))
end
private
def parse_response(response)
Response.new(response)
end
def xml_hash
{
import: {
contenttype: context.user,
contents: {
content: [
{ :@id => instance.id }.merge(translate_to_xml_hash)
]
}
}
}
end
def translate_to_xml_hash
translation = translate.map do |k, v|
case
when k == :author then [k, { :@type => :author, :content! => v }]
when v.acts_like?(:time) then [k, { content!: (v.to_f * 1_000).to_i }]
when v.is_a?(Fixnum) then [k, { content!: v }]
else ["#{k}!", { content!: cdata_section(v) }]
end
end
Hash[translation]
end
end
end