Module: Model::Template

Extended by:
ActiveSupport::Concern
Included in:
Wechat::Template
Defined in:
app/models/wechat/model/template.rb

Instance Method Summary collapse

Instance Method Details

#data_keysObject



69
70
71
# File 'app/models/wechat/model/template.rb', line 69

def data_keys
  content.gsub(/(?<={{)\w+(?=.DATA}})/).to_a
end

#data_mappingsObject



73
74
75
76
77
78
79
# File 'app/models/wechat/model/template.rb', line 73

def data_mappings
  if template_config
    template_config.data_hash
  else
    {}
  end
end

#del_to_wechatObject



64
65
66
67
# File 'app/models/wechat/model/template.rb', line 64

def del_to_wechat
  r = app.api.del_template(template_id)
  logger.debug(r['errmsg'])
end

#init_template_configObject



22
23
24
25
26
27
28
29
# File 'app/models/wechat/model/template.rb', line 22

def init_template_config
  if app.is_a?(PublicApp)
    config = TemplatePublic.find_or_initialize_by(content: content)
    config.title = title
    self.template_config = config
    self.save
  end
end

#sync_from_template_configObject



31
32
33
# File 'app/models/wechat/model/template.rb', line 31

def sync_from_template_config
  self.template_id ||= template_config.tid
end

#sync_from_wechatObject



48
49
50
51
52
53
54
55
56
57
# File 'app/models/wechat/model/template.rb', line 48

def sync_from_wechat
  r_content = app.api.templates.find do |i|
    tid = app.is_a?(PublicApp) ? i['template_id'] : i['priTmplId']
    tid == self.template_id
  end
  return if r_content.blank?
  self.template_type = r_content['type']
  self.assign_attributes r_content.slice('title', 'content', 'example')
  self
end

#sync_to_wechatObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/wechat/model/template.rb', line 35

def sync_to_wechat
  sync_from_wechat
  return if content.present?
  r = app.api.add_template(template_config.tid, template_config.kid_list)
  if r['errcode'] == 0
    self.template_id = r['priTmplId'] || r['template_id']
  else
    logger.debug("  =========> Error is #{r['errmsg']}")
    return
  end
  sync_from_wechat
end

#sync_to_wechat!Object



59
60
61
62
# File 'app/models/wechat/model/template.rb', line 59

def sync_to_wechat!
  sync_to_wechat
  save
end