Class: Nytimes::Articles::Facet

Inherits:
Object
  • Object
show all
Defined in:
lib/nytimes_articles/facet.rb

Overview

This class represents a Facet used in the ArticleSearch API. Facets can be used to both search for matching articles (see Article#search) and are also returned as article and search metadata. Facets are made up of 3 parts:

  • facet_type - a string; see Article#search for a list of facet types

  • term - a string as well

  • count - Facets returned as search metadata (via the :facets parameter to Article#search) also include a non-nil count of matching articles for that facet

Constant Summary collapse

CLASSIFIERS =

Facet name constants

'classifiers_facet'
COLUMN =
'column_facet'
DATE =
'date'
DAY_OF_WEEK =
'day_of_week_facet'
DBPEDIA_RESOURCE =
'dbpedia_resource'
DBPEDIA_URL =
'dbpedia_resource_url'
DESCRIPTION =
'des_facet'
DESK =
'desk_facet'
FACET_TERM =
'facet_terms'
GEO =
'geo_facet'
MATERIAL_TYPE =
'material_type_facet'
ORGANIZATION =
'org_facet'
PAGE =
'page_facet'
PERSON =
'per_facet'
PUB_DAY =
'publication_day'
PUB_MONTH =
'publication_month'
PUB_YEAR =
'publication_year'
SECTION_PAGE =
'section_page_facet'
SOURCE =
'source_facet'
WORKS_MENTIONED =
'works_mentioned_facet'
NYTD_BYLINE =

Facets of content formatted for nytimes.com

'nytd_byline'
NYTD_DESCRIPTION =
'nytd_des_facet'
NYTD_GEO =
'nytd_geo_facet'
NYTD_ORGANIZATION =
'nytd_org_facet'
NYTD_PERSON =
'nytd_per_facet'
NYTD_SECTION =
'nytd_section_facet'
NYTD_WORKS_MENTIONED =
'nytd_works_mentioned_facet'
DEFAULT_RETURN_FACETS =

The default 5 facets to return

[DESCRIPTION, GEO, ORGANIZATION, PERSON, DESK]
ALL_FACETS =
[CLASSIFIERS, COLUMN, DATE, DAY_OF_WEEK, DESCRIPTION, DBPEDIA_RESOURCE, DBPEDIA_URL, DESK, GEO, MATERIAL_TYPE, ORGANIZATION, PAGE, PERSON, PUB_DAY,
PUB_MONTH, PUB_YEAR, SECTION_PAGE, SOURCE, WORKS_MENTIONED, NYTD_BYLINE, NYTD_DESCRIPTION, NYTD_GEO,
NYTD_ORGANIZATION, NYTD_PERSON, NYTD_SECTION, NYTD_WORKS_MENTIONED]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet_type, term, count) ⇒ Facet

Initializes the facet. There is seldom a reason for you to call this.



63
64
65
66
67
# File 'lib/nytimes_articles/facet.rb', line 63

def initialize(facet_type, term, count)
	@facet_type = facet_type
	@term = term
	@count = count
end

Instance Attribute Details

#countObject (readonly)

The number of times this facet has appeared in the search results (note: this only applies for facets returned in the facets header on an Article#search)



17
18
19
# File 'lib/nytimes_articles/facet.rb', line 17

def count
  @count
end

#facet_typeObject (readonly)

The facet type



21
22
23
# File 'lib/nytimes_articles/facet.rb', line 21

def facet_type
  @facet_type
end

#termObject (readonly)

The term for the facet



13
14
15
# File 'lib/nytimes_articles/facet.rb', line 13

def term
  @term
end

Class Method Details

.init_from_api(api_hash) ⇒ Object

Initializes a selection of Facet objects returned from the API. Used for marshaling Facets in articles and metadata from search results (Note: some facets are returned as scalar values)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/nytimes_articles/facet.rb', line 118

def self.init_from_api(api_hash)
	return nil if api_hash.nil?
	
	unless api_hash.is_a? Hash
		raise ArgumentError, "expecting a Hash only"
	else
		return nil if api_hash.empty?
	end
	
	out = {}
	
	api_hash.each_pair do |k,v|
		out[k] = v.map {|f| Facet.new(k, f['term'], f['count'])}
	end
	
	out
end

.symbol_name(facet) ⇒ Object

Takes a symbol name and subs it to a string constant



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nytimes_articles/facet.rb', line 71

def self.symbol_name(facet)
	case facet
	when String
		return facet
	when Facet
		return facet.facet_type
	when Symbol
		# fall through
	else
		raise ArgumentError, "Unsupported type to Facet#symbol_to_api_name"
	end
	
	case facet
	when :geography
		GEO
	when :org, :orgs
		ORGANIZATION
	when :people
		PERSON
	when :nytd_geography
		NYTD_GEO
	when :nytd_org, :nytd_orgs
		NYTD_ORGANIZATION
	when :nytd_people
		NYTD_PERSON
	when :dbpedia, :dbpedia_res
	  DBPEDIA_RESOURCE
	when :dbpedia_url, :dbpedia_resource_url
	  DBPEDIA_URL
	when :term, :terms, :facet_terms, :facet_term
	  FACET_TERM
	else
		name = facet.to_s.upcase
		
		if const_defined?(name)
			const_get(name)
		elsif name =~ /S$/ && const_defined?(name.gsub(/S$/, ''))
			const_get(name.gsub(/S$/, ''))
		else
			raise ArgumentError, "Unable to find a matching facet key for symbol :#{facet}"
		end
	end
end