Class: Kiosk::WordPress::Resource

Inherits:
ActiveResource::Base
  • Object
show all
Includes:
Cacheable::Resource, Localizable::Resource, Prospector
Defined in:
lib/kiosk/word_press/resource.rb

Direct Known Subclasses

Attachment, Author, Category, Comment, Images, Page, Post, Tag

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cacheable::Resource

#expire

Class Method Details

.allObject

Returns all instances of the resource.



33
34
35
# File 'lib/kiosk/word_press/resource.rb', line 33

def all
  find(:all)
end

.collection_path(prefix_options = {}, query_options = nil) ⇒ Object

Reimplements the ActiveResource path constructor to work with the WordPress JSON-API plugin.



89
90
91
# File 'lib/kiosk/word_press/resource.rb', line 89

def collection_path(prefix_options = {}, query_options = nil)
  "#{api_path_to("get_#{element_name}_index")}#{query_string(query_options)}"
end

.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object

Reimplements the ActiveResource path constructor to work with the WordPress JSON-API plugin.



40
41
42
# File 'lib/kiosk/word_press/resource.rb', line 40

def element_path(id, prefix_options = {}, query_options = nil)
  "#{api_path_to("get_#{element_name}")}#{query_string({:id => id}.merge(query_options || {}))}"
end

.element_path_by_slug(slug, prefix_options = {}, query_options = nil) ⇒ Object



44
45
46
# File 'lib/kiosk/word_press/resource.rb', line 44

def element_path_by_slug(slug, prefix_options = {}, query_options = nil)
  "#{api_path_to("get_#{element_name}")}#{query_string({:slug => slug}.merge(query_options || {}))}"
end

.find(*arguments) ⇒ Object

Adds functionality to the ActiveResource.find method to allow for specifying the WordPress JSON API method that should be used. This simplifies definition of scopes in derived models.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kiosk/word_press/resource.rb', line 52

def find(*arguments)
  scope   = arguments.slice!(0)
  options = arguments.slice!(0) || {}

  if options.key?(:method)
    options[:from] = api_path_to(options[:method])
    options.delete(:method)
  end

  super(scope, options)
end

.find_by_associated(resource, params = {}) ⇒ Object

Finds all resources by the given related resource.

Example:

Post.find_by_associated(category)

Is the same as invoking:

Post.find(:all, :method => "get_category_posts", :params => {:id => category.id})


74
75
76
77
78
# File 'lib/kiosk/word_press/resource.rb', line 74

def find_by_associated(resource, params = {})
  find(:all,
       :method => "get_#{resource.class.element_name}_#{element_name.pluralize}",
       :params => params.merge({:id => resource.id}))
end

.find_by_slug(slug) ⇒ Object

Finds the resource by the given slug.



82
83
84
# File 'lib/kiosk/word_press/resource.rb', line 82

def find_by_slug(slug)
  find(:one, :method => "get_#{element_name}", :params => {:slug => slug})
end

.instantiate_collection(collection, prefix_options = {}) ⇒ Object

Reimplements the ActiveResource method to check for bad responses before instantiating a collection.



96
97
98
# File 'lib/kiosk/word_press/resource.rb', line 96

def instantiate_collection(collection, prefix_options = {})
  super(normalize_response(collection, true), prefix_options)
end

.instantiate_record(record, prefix_options = {}) ⇒ Object

Reimplements the ActiveResource method to check for bad responses before instantiating an object.



103
104
105
# File 'lib/kiosk/word_press/resource.rb', line 103

def instantiate_record(record, prefix_options = {})
  super(normalize_response(record), prefix_options)
end

.with_parameters(params = {}) ⇒ Object

Executes the given block within a scope where all requests for this content resource are appended with the given parameters.

class Post < Resource; end

Post.with_parameters(:language => 'en') do
  english_posts = Post.find(:all)
  english_pages = Page.find(:all)
end

Scopes can be nested.

Post.with_parameters(:language => 'es') do
  Post.with_parameters(:recent => true) do
    recent_spanish_posts = Post.find(:all)
  end
end

Scopes are inherited.

Resource.with_parameters(:language => 'fr') do
  french_posts = Post.find(:all)
end

However, nesting is still respected.

Resource.with_parameters(:language => 'fr') do
  Post.with_parameters(:language => 'en') do
    english_posts = Post.find(:all)
  end
end

Even with this nesting inverted.

Post.with_parameters(:language => 'fr') do
  Resource.with_parameters(:language => 'en') do
    english_posts = Post.find(:all)
  end
end


147
148
149
150
151
152
153
154
155
# File 'lib/kiosk/word_press/resource.rb', line 147

def with_parameters(params = {})
  push_to_query_scope_stack(params)

  begin
    yield
  ensure
    pop_from_query_scope_stack
  end
end

Instance Method Details

#contentObject

Returns the rewritten resource content. See raw_content for untouched content.



273
274
275
# File 'lib/kiosk/word_press/resource.rb', line 273

def content
  @content ||= raw_content && Kiosk.rewriter.rewrite(raw_content)
end

#content_documentObject

Returns the rewritten resource content as a Document.



279
280
281
# File 'lib/kiosk/word_press/resource.rb', line 279

def content_document
  raw_content && Kiosk.rewriter.rewrite_to_document(raw_content)
end

#destroyObject

Destroying is not supported.

Raises:

  • (NotImplementedError)


292
293
294
# File 'lib/kiosk/word_press/resource.rb', line 292

def destroy
  raise NotImplementedError
end

#excerptObject

Returns the rewritten resource excerpt. See raw_excerpt for untouched content.



286
287
288
# File 'lib/kiosk/word_press/resource.rb', line 286

def excerpt
  @excerpt ||= raw_excerpt && Kiosk.rewriter.rewrite(raw_excerpt)
end

#raw_contentObject

Returns the resource content, untouched by the content rewriter.



298
299
300
# File 'lib/kiosk/word_press/resource.rb', line 298

def raw_content
  attributes[:content]
end

#raw_excerptObject

Returns the resource excerpt, untouched by the content rewriter.



304
305
306
# File 'lib/kiosk/word_press/resource.rb', line 304

def raw_excerpt
  attributes[:excerpt]
end

#saveObject

Saving is not supported.

Raises:

  • (NotImplementedError)


310
311
312
# File 'lib/kiosk/word_press/resource.rb', line 310

def save
  raise NotImplementedError
end

#to_paramObject

Returns the value used in constructing a URL to this object.



316
317
318
# File 'lib/kiosk/word_press/resource.rb', line 316

def to_param
  attributes[:slug] || attributes[:id]
end