Class: API::Story

Inherits:
Object
  • Object
show all
Includes:
ReadingTime
Defined in:
lib/quintype/api/story.rb,
lib/quintype/api/story/reading_time.rb

Defined Under Namespace

Modules: ReadingTime

Constant Summary

Constants included from ReadingTime

ReadingTime::ESTIMATED_TWEET_WORDS, ReadingTime::FINAL_IMAGE_READ_TIME, ReadingTime::INITIAL_IMAGE_READ_TIME, ReadingTime::SLOW_IMAGE_LIMIT, ReadingTime::WORDS_PER_MINUTE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReadingTime

#time_in_minutes, #time_in_seconds

Constructor Details

#initialize(story) ⇒ Story

Returns a new instance of Story.



95
96
97
# File 'lib/quintype/api/story.rb', line 95

def initialize(story)
  @story = story
end

Instance Attribute Details

#storyObject (readonly)

Returns the value of attribute story.



5
6
7
# File 'lib/quintype/api/story.rb', line 5

def story
  @story
end

Class Method Details

.allObject



89
90
91
92
# File 'lib/quintype/api/story.rb', line 89

def all
  stories = API.stories({})
  wrap_all(stories)
end

.all_video_storiesObject



84
85
86
87
# File 'lib/quintype/api/story.rb', line 84

def all_video_stories
  stories = API.videos
  wrap_all(stories['stories'])
end

.find(params, opts = {}) ⇒ Object



24
25
26
27
28
29
# 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 = {}) ⇒ Object



78
79
80
81
82
# 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 = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



51
52
53
54
55
56
57
58
# 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) ⇒ Object



60
61
62
63
64
65
66
67
# 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) ⇒ Object



69
70
71
72
73
74
75
76
# 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

.prepare_bulk(params) ⇒ Object



44
45
46
47
48
49
# 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 = {}) ⇒ Object



19
20
21
22
# File 'lib/quintype/api/story.rb', line 19

def where(params, opts={})
  stories = API.stories(params, opts)
  wrap_all(stories)
end

.wrap(story) ⇒ Object



15
16
17
# File 'lib/quintype/api/story.rb', line 15

def wrap(story)
  new(story) if story
end

.wrap_all(stories) ⇒ Object



8
9
10
11
12
13
# 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

Instance Method Details

#cardsObject



99
100
101
# File 'lib/quintype/api/story.rb', line 99

def cards
  @cards = story['cards'] || []
end

#to_h(config = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# 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