Class: Nytimes::Movies::Critic

Inherits:
Base
  • Object
show all
Defined in:
lib/nytimes/movies/critic.rb

Constant Summary

Constants inherited from Base

Base::API_BASE, Base::API_NAME, Base::API_SERVER, Base::API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key, api_key=, build_request_url, copyright, invoke

Constructor Details

#initialize(params = {}) ⇒ Critic

Returns a new instance of Critic.



6
7
8
9
10
# File 'lib/nytimes/movies/critic.rb', line 6

def initialize(params={})
	params.each_pair do |k,v|
		instance_variable_set("@#{k}", v)
	end
end

Instance Attribute Details

#bioObject (readonly)

Returns the value of attribute bio.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def bio
  @bio
end

#display_nameObject (readonly)

Returns the value of attribute display_name.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def display_name
  @display_name
end

#photoObject (readonly)

Returns the value of attribute photo.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def photo
  @photo
end

#seo_nameObject (readonly)

Returns the value of attribute seo_name.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def seo_name
  @seo_name
end

#sort_nameObject (readonly)

Returns the value of attribute sort_name.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def sort_name
  @sort_name
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/nytimes/movies/critic.rb', line 4

def status
  @status
end

Class Method Details

.create_from_api(params = {}) ⇒ Object

Create a Critic object from a hash snippet returned from the API. You should never need to call this.



20
21
22
23
24
25
26
27
# File 'lib/nytimes/movies/critic.rb', line 20

def self.create_from_api(params={})
	self.new :display_name => params['display_name'],
					 :sort_name => params['sort_name'],
					 :status => params['status'],
					 :bio => params['bio'],
					 :seo_name => params['seo_name'],
					 :photo => MultimediaLink.create_from_api(params['multimedia'])
end

.escape_critic_name(name) ⇒ Object

Escapes a critic’s name to the SEO variation used for name searches. This is automatically used by Critic#find_by_name, so you don’t need to call it.



53
54
55
56
# File 'lib/nytimes/movies/critic.rb', line 53

def self.escape_critic_name(name)
	return name if name =~ /^[a-z\-]+$/  # might be escaped already
	name.downcase.gsub(/[^a-z\s]/, ' ').gsub(/\s+/, '-')
end

.find_by_name(name) ⇒ Object

Find a critic record matching a given name. Both the English name (eg, ‘A. O. Scott’) and the SEO name (‘a-o-scott’) are valid keys, although the SEO name is suggested if you want to avoid ambiguity. Only exact matches are used (no last name searches). If a single matching record is found, returns a single Critic instance. In the admittedly rare chance of 2 distinct critics having the same name, returns an array. Returns nil if no matches are found.



62
63
64
65
66
67
68
69
70
# File 'lib/nytimes/movies/critic.rb', line 62

def self.find_by_name(name)
	name = escape_critic_name(name)
	reply = invoke("critics/#{name}")
	return nil if reply.nil? || reply['results'].nil?
	results = reply['results']
	critics = results.map {|r| self.create_from_api(r)}
	return critics.first if critics.length == 1
	critics
end

.find_by_type(type) ⇒ Object

Returns a list of critics that match a given type. The following types are supported:

  • :full_time - fulltime critics at the New York Times

  • :part_time - part-time critics at the New York Times

  • :all - all critics



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nytimes/movies/critic.rb', line 34

def self.find_by_type(type)
	key = case type
	when :full_time
		'full-time'
	when :part_time
		'part-time'
	when :all
		'all'
	else
		raise ArgumentError, "Type can be :full_time, :part_time, or :all"
	end
	
	reply = invoke("critics/#{key}")
	results = reply['results']
	results.map {|r| self.create_from_api(r)}
end

Instance Method Details

#reviews(params = {}) ⇒ Object

Returns reviews made by the critic. Please refer to Review#find for other optional flags that can be applied.



14
15
16
# File 'lib/nytimes/movies/critic.rb', line 14

def reviews(params={})
	Review.find(params.merge :reviewer => seo_name)
end