Class: AP::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/ap/article.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Article

Creates a new AP::Article object given the following attributes

  • id: the article id as reported by the AP

  • title: the title of the article

  • authors: an Array of the name(s) of the author(s) of this article

  • tags: and array of tags or categories that have been attached to this article

  • link: string with the hosted version on the AP site

  • content: the article content

  • updated: Time object of when the article was last updated



13
14
15
16
17
18
19
20
21
# File 'lib/ap/article.rb', line 13

def initialize(opts = {})
  @id = opts[:id]
  @title = opts[:title]
  @tags = opts[:tags] || []
  @authors = opts[:authors] || []
  @link = opts[:link]
  @content = opts[:content]
  @updated = opts[:updated]
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



3
4
5
# File 'lib/ap/article.rb', line 3

def authors
  @authors
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/ap/article.rb', line 3

def content
  @content
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/ap/article.rb', line 3

def id
  @id
end

Returns the value of attribute link.



3
4
5
# File 'lib/ap/article.rb', line 3

def link
  @link
end

#tagsObject

Returns the value of attribute tags.



3
4
5
# File 'lib/ap/article.rb', line 3

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/ap/article.rb', line 3

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



3
4
5
# File 'lib/ap/article.rb', line 3

def updated
  @updated
end

Class Method Details

.new_from_api_data(data) ⇒ Object

Creates a new object from data returned by the API



24
25
26
27
28
29
30
31
32
# File 'lib/ap/article.rb', line 24

def self.new_from_api_data(data)
  if data["author"].is_a?(Hash)
    authors = [ data["author"]["name"] ]
  elsif data["author"].is_a?(Array)
    authors = data["author"].collect{ |x| x["name"] }
  end
  categories = data["category"] ? data["category"].collect { |x| x["label"] } : []
  return new(:id => data["id"].split(":").last, :title => data["title"], :authors => authors, :tags => categories, :link => data["link"]["href"], :content => data["content"], :updated => Time.parse(data["updated"]))
end

Instance Method Details

#similarObject

Returns a search object that when fetched, will find articles similar to this one. Refer to AP::Search for more information



36
37
38
# File 'lib/ap/article.rb', line 36

def similar
  return AP::Search.similar(@id)
end