Class: NPR::Entity::Story

Inherits:
Base
  • Object
show all
Includes:
Concern::LinksAssociation
Defined in:
lib/npr/entity/story.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern::LinksAssociation

included, #link_for

Methods included from Concern::ShallowAttributes

included

Methods included from Concern::Relation

included

Constructor Details

#initialize(json) ⇒ Story




124
125
126
127
128
129
130
131
# File 'lib/npr/entity/story.rb', line 124

def initialize(json)
  @_json = json
  @id    = @_json["id"].to_i

  build_text
  extract_shallow_attributes(@_json)
  create_relations(@_json)
end

Instance Attribute Details

#containerObject




83
84
85
# File 'lib/npr/entity/story.rb', line 83

def container
  @container
end

#idObject




83
84
85
# File 'lib/npr/entity/story.rb', line 83

def id
  @id
end

#textObject




83
84
85
# File 'lib/npr/entity/story.rb', line 83

def text
  @text
end

#textWithHtmlObject




83
84
85
# File 'lib/npr/entity/story.rb', line 83

def textWithHtml
  @textWithHtml
end

#thumbnailObject




83
84
85
# File 'lib/npr/entity/story.rb', line 83

def thumbnail
  @thumbnail
end

Class Method Details

.find(id) ⇒ Object


Find a story based on ID

This method is meant to be a quick, easy way to find a story just by its ID.

If you need more control over what gets fetched and how, use NPR::Client directly, or the other chainable methods (where, order, etc.)

It is not possible to pass options into this method. Therefore, you must globally configure at least the apiKey using NPR.configure.

Example:

NPR::Entity::Story.find(1000)


28
29
30
31
32
33
34
35
36
# File 'lib/npr/entity/story.rb', line 28

def find(id)
  response = query_by_id(id)

  if !response.messages.empty?
    response.messages
  else
    response.list.stories.first
  end
end

.find_by_id(id) ⇒ Object


Same as above, but returns nil if a single story isn’t returned.



41
42
43
44
45
46
47
48
49
# File 'lib/npr/entity/story.rb', line 41

def find_by_id(id)
  response = query_by_id(id)

  if !response.messages.empty?
    nil
  else
    response.list.stories.first
  end
end

Instance Method Details

#primary_imageObject


The primary image.

Looks at the “type” attribute on an image and finds any with type “primary”. If none are found, then return the first image of any type.



140
141
142
143
144
145
# File 'lib/npr/entity/story.rb', line 140

def primary_image
  @primary_image ||= begin
    primary = self.images.find(&:primary?)
    primary || self.images.first
  end
end