class API::Story

Attributes

story[R]

Public Class Methods

all() click to toggle source
# File lib/quintype/api/story.rb, line 89
def all
  stories = API.stories({})
  wrap_all(stories)
end
all_video_stories() click to toggle source
# File lib/quintype/api/story.rb, line 84
def all_video_stories
  stories = API.videos
  wrap_all(stories['stories'])
end
find(params, opts={}) click to toggle source
# File lib/quintype/api/story.rb, line 24
def find(params, opts={})
  if stories = API.stories(params, opts).presence
    story = stories.first
    wrap(story)
  end
end
find_by_slug(slug, params = {}) click to toggle source
# File lib/quintype/api/story.rb, line 78
def find_by_slug(slug, params = {})
  if story = API.story_by_slug(slug, params).presence
    wrap(story['story'])
  end
end
find_by_stacks(stacks, options={}) click to toggle source
# File lib/quintype/api/story.rb, line 31
def find_by_stacks(stacks, options={})
  if stacks.present?
    requests = stacks.inject({}) do |hash, stack|
      options.reject! {|k,v| k == 'section' }
      hash[stack['story_group']] = { 'story_group' => stack['story_group'] }.merge(options)
      hash
    end

    stories = find_in_bulk(requests)
    stories
  end
end
find_in_bulk(params) click to toggle source
# File lib/quintype/api/story.rb, line 51
def find_in_bulk(params)
  if params.present?
    response = API.bulk_post(requests: prepare_bulk(params))
    response['results']
  else
    []
  end
end
find_in_bulk_cached(params) click to toggle source
# File lib/quintype/api/story.rb, line 60
def find_in_bulk_cached(params)
  if params.present?
    response = API.bulk_cached(requests: prepare_bulk(params))
    response['results']
  else
    []
  end
end
find_in_bulk_v1_cached(params) click to toggle source
# File lib/quintype/api/story.rb, line 69
def find_in_bulk_v1_cached(params)
  if params.present?
    response = API.bulk_v1_cached(requests: prepare_bulk(params))
    response['results']
  else
    []
  end
end
new(story) click to toggle source
# File lib/quintype/api/story.rb, line 95
def initialize(story)
  @story = story
end
prepare_bulk(params) click to toggle source
# File lib/quintype/api/story.rb, line 44
def prepare_bulk(params)
  params.inject({}) do |hash, param|
    hash[param.first] = param.last.merge(_type: param.last[:_type] || 'stories')
    hash
  end
end
where(params, opts={}) click to toggle source
# File lib/quintype/api/story.rb, line 19
def where(params, opts={})
  stories = API.stories(params, opts)
  wrap_all(stories)
end
wrap(story) click to toggle source
# File lib/quintype/api/story.rb, line 15
def wrap(story)
  new(story) if story
end
wrap_all(stories) click to toggle source
# File lib/quintype/api/story.rb, line 8
def wrap_all(stories)
  stories ||= []
  stories.is_a?(Array) ?
    stories.map { |s| wrap(s) } :
    wrap(stories)
end

Public Instance Methods

cards() click to toggle source
# File lib/quintype/api/story.rb, line 99
def cards
  @cards = story['cards'] || []
end
to_h(config={}) click to toggle source
# File lib/quintype/api/story.rb, line 103
def to_h(config={})
  hash = story.merge({
    'url' => URL.story(story),
    'time_in_minutes' => time_in_minutes,
    'tags' => add_urls_to_tags
  })
  if config.present?
    hash.merge!({ 'sections' => add_display_names_to_sections(config),
                  'canonical_url' => URL.story_canonical(config['root_url'], story)
                })
  end
  hash
end

Private Instance Methods

add_display_names_to_sections(config) click to toggle source
# File lib/quintype/api/story.rb, line 118
def add_display_names_to_sections(config)
  return story unless story['sections'].present?

  sections = story['sections'].map do |section|
    display_section = config['sections'].find { |s| s['id'] == section['id'] } || {}

    display_name = display_section['display_name'] || display_section['name'] || section['name']

    section.merge({ 'display_name' => display_name })
  end
end
add_urls_to_tags() click to toggle source
# File lib/quintype/api/story.rb, line 130
def add_urls_to_tags
  if story['tags'].present?
    tags = story['tags'].map do |tag|
      tag.merge('url' => URL.topic(tag['name']))
    end
  else
    story['tags']
  end
end