Class: Facebooker::Rails::Publisher::FacebookTemplate

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/facebooker/rails/publisher.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bundle_id_for_class_and_method(klass, method) ⇒ Object



147
148
149
# File 'lib/facebooker/rails/publisher.rb', line 147

def bundle_id_for_class_and_method(klass,method)
  for_class_and_method(klass,method).bundle_id
end

.cache(klass, method, template) ⇒ Object



151
152
153
# File 'lib/facebooker/rails/publisher.rb', line 151

def cache(klass,method,template)
  template_cache[template_name(klass,method)] = template
end

.clear_cache!Object



155
156
157
# File 'lib/facebooker/rails/publisher.rb', line 155

def clear_cache!
  self.template_cache = {}
end

.find_cached(klass, method) ⇒ Object



159
160
161
# File 'lib/facebooker/rails/publisher.rb', line 159

def find_cached(klass,method)
  template_cache[template_name(klass,method)] || find_in_db(klass,method)
end

.find_in_db(klass, method) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/facebooker/rails/publisher.rb', line 163

def find_in_db(klass,method)
  template = find_by_template_name(template_name(klass,method))
  if template and template.template_changed?(hashed_content(klass,method))
    template.destroy
    template = nil
  end
  
  if template.nil?
    template = register(klass,method)
  end
  template
end

.for_class_and_method(klass, method) ⇒ Object



144
145
146
# File 'lib/facebooker/rails/publisher.rb', line 144

def for_class_and_method(klass,method)
  find_cached(klass,method) 
end

.hashed_content(klass, method) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/facebooker/rails/publisher.rb', line 182

def hashed_content(klass, method)
  publisher = setup_publisher(klass,method)
  # sort the Hash elements (in the short_story and full_story) before generating MD5
  Digest::MD5.hexdigest [publisher.one_line_story_templates,
     (publisher.short_story_templates and publisher.short_story_templates.collect{|ss| ss.to_a.sort_by{|e| e[0].to_s}}),
     (publisher.full_story_template and publisher.full_story_template.to_a.sort_by{|e| e[0].to_s})
     ].to_json
end

.inspect(*args) ⇒ Object



117
118
119
# File 'lib/facebooker/rails/publisher.rb', line 117

def self.inspect(*args)
  "FacebookTemplate"
end

.register(klass, method) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/facebooker/rails/publisher.rb', line 133

def register(klass,method)
  publisher = setup_publisher(klass,method)            
  template_id = Facebooker::Session.create.register_template_bundle(publisher.one_line_story_templates,publisher.short_story_templates,publisher.full_story_template,publisher.action_links)
  template = find_or_initialize_by_template_name(template_name(klass,method))
  template.bundle_id = template_id
  template.content_hash = hashed_content(klass,method) if template.respond_to?(:content_hash)
  template.save!
  cache(klass,method,template)
  template
end

.setup_publisher(klass, method) ⇒ Object



176
177
178
179
180
# File 'lib/facebooker/rails/publisher.rb', line 176

def setup_publisher(klass,method)
  publisher = klass.new
  publisher.send method + '_template'
  publisher
end

.template_name(klass, method) ⇒ Object



191
192
193
# File 'lib/facebooker/rails/publisher.rb', line 191

def template_name(klass,method)
  "#{Facebooker.api_key}: #{klass.name}::#{method}"
end

Instance Method Details

#template_changed?(hash) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/facebooker/rails/publisher.rb', line 121

def template_changed?(hash)
  if respond_to?(:content_hash)
    content_hash != hash 
  else
    false
  end
end