Class: Nytimes::Movies::Review

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

Constant Summary collapse

BATCH_SIZE =
20

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 = {}) ⇒ Review

Returns a new instance of Review.



17
18
19
20
21
# File 'lib/nytimes/movies/review.rb', line 17

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

Instance Attribute Details

#bylineObject (readonly) Also known as: critic_name

Returns the value of attribute byline.



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

def 
  @byline
end

#criticObject (readonly)

Returns the value of attribute critic.



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

def critic
  @critic
end

#critics_pickObject (readonly) Also known as: critics_pick?

Returns the value of attribute critics_pick.



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

def critics_pick
  @critics_pick
end

#date_updatedObject (readonly) Also known as: updated_on

Returns the value of attribute date_updated.



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

def date_updated
  @date_updated
end

#display_titleObject (readonly) Also known as: movie_title

Returns the value of attribute display_title.



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

def display_title
  @display_title
end

#dvd_release_dateObject (readonly)

Returns the value of attribute dvd_release_date.



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

def dvd_release_date
  @dvd_release_date
end

#headlineObject (readonly) Also known as: review_title

Returns the value of attribute headline.



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

def headline
  @headline
end

Returns the value of attribute link.



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

def link
  @link
end

#mpaa_ratingObject (readonly)

Returns the value of attribute mpaa_rating.



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

def mpaa_rating
  @mpaa_rating
end

#nyt_movie_idObject (readonly)

Returns the value of attribute nyt_movie_id.



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

def nyt_movie_id
  @nyt_movie_id
end

#opening_dateObject (readonly)

Returns the value of attribute opening_date.



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

def opening_date
  @opening_date
end

#publication_dateObject (readonly)

Returns the value of attribute publication_date.



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

def publication_date
  @publication_date
end

Returns the value of attribute related_links.



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

def related_links
  @related_links
end

#seo_nameObject (readonly)

Returns the value of attribute seo_name.



4
5
6
# File 'lib/nytimes/movies/review.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/review.rb', line 4

def sort_name
  @sort_name
end

#summary_shortObject (readonly)

Returns the value of attribute summary_short.



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

def summary_short
  @summary_short
end

#thousand_bestObject (readonly) Also known as: thousand_best?

Returns the value of attribute thousand_best.



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

def thousand_best
  @thousand_best
end

#thumbnailObject (readonly)

Returns the value of attribute thumbnail.



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

def thumbnail
  @thumbnail
end

Class Method Details

.boolean_to_query_arg(arg) ⇒ Object



56
57
58
59
# File 'lib/nytimes/movies/review.rb', line 56

def boolean_to_query_arg(arg)
	return nil if arg.nil?					
	arg ? 'Y' : 'N'
end

.coerce_key_boolean(hash, key) ⇒ Object



33
34
35
# File 'lib/nytimes/movies/review.rb', line 33

def coerce_key_boolean(hash, key)
	hash[key] = hash[key] == 'Y'
end

.create_from_api(hash) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nytimes/movies/review.rb', line 61

def create_from_api(hash)
	hash = hash.dup
	
	hash['dvd_release_date'] = parse_date(hash['dvd_release_date'])
	hash['opening_date'] = parse_date(hash['opening_date'])
	hash['publication_date'] = parse_date(hash['publication_date'])
	hash['date_updated'] = parse_date(hash['date_updated'])
	
	coerce_key_boolean hash, 'critics_pick'
	coerce_key_boolean hash, 'thousand_best'
	
	multimedia = hash.delete('multimedia')
	unless multimedia.nil?
		hash['thumbnail'] = MultimediaLink.create_from_api(multimedia)
	end
	
	if hash.has_key? 'link'
		hash['link'] = Link.create_from_api(hash['link'])
	end
	
	related = hash.delete('related_urls')
	unless related.nil?
		hash['related_links'] = related.map {|l| Link.create_from_api(l) }
	end
	
	rename_hash_key hash, 'seo-name', 'seo_name'
	new hash
end

.date_to_query_arg(date_or_range) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/nytimes/movies/review.rb', line 47

def date_to_query_arg(date_or_range)
	return nil if date_or_range.nil?
	if date_or_range.respond_to?(:first) && date_or_range.respond_to?(:last)
		"#{format_date_arg(date_or_range.first)};#{format_date_arg(date_or_range.last)}"
	else
		format_date_arg(date_or_range)
	end
end

.find(in_params = {}) ⇒ Object

In addition, you can also specify the following order and pagination values:

  • :offset - a maximum of 20 result are returned for queries. To retrieve further pages, an offset from the first result can be specified. This must be a multiple of 20. So 20 means results 21 - 40

  • :page - as a convenience, page will calculate the offset for here. This is not an API parameter and is translated into an offset.

  • :order - ordering for the results. The following four sorting options are available: :title (ascending), :publication_date, :opening_date, :dvd_release_date (most recent first). If you do not specify a sort order, the results will be ordered by closest match.

  • :load_critics - if true, automatically load the Critic object for each record’s reviewer. Note that this requires N additional API calls (where N is the unique number of critics in the result set)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/nytimes/movies/review.rb', line 106

def find(in_params={})
	params = {}
	
	if in_params[:text]
		params['query'] = in_params[:text]
	end

	params['thousand-best'] = boolean_to_query_arg(in_params[:thousand_best])
	params['critics-pick'] = boolean_to_query_arg(in_params[:critics_pick])
	params['dvd'] = boolean_to_query_arg(in_params[:dvd])
	
	params['publication-date'] = date_to_query_arg(in_params[:publication_date])
	params['opening-date'] = date_to_query_arg(in_params[:opening_date])
						
	if in_params.has_key? :reviewer
		params['reviewer'] = Critic.escape_critic_name(in_params[:reviewer])
	end
	
	params['offset'] = in_params[:offset]
	
	if in_params.has_key? :page
		params['offset'] = BATCH_SIZE * (in_params[:page] - 1)
	end
	
	if in_params.has_key? :order
		params['order'] = case in_params[:order]
		when :title, :publication_date, :opening_date, :dvd_release_date
			"by-#{in_params[:order].to_s.gsub('_', '-')}"
		else
			raise ArgumentError, "Order can only be :title, :publication_date, :opening_date, or :dvd_release_date"
		end
	end
	
	params.delete_if {|k, v| v.nil? }
	
	reply = invoke 'reviews/search', params
	out = ResultSet.new(params, reply, Review)			

	if in_params[:load_critics]
		critics_hash = {}
		out.results.each do |r|
			name = r.
			
			if critics_hash.has_key? name
				critic = critics_hash[name]
			else
				critic = Critic.find_by_name(name)
				critics_hash[name] = critic
			end
			
			r.instance_variable_set '@critic', critic 
		end
	end	
	
	out				
end

.find_by_reviewer(params = {}) ⇒ Object



166
167
# File 'lib/nytimes/movies/review.rb', line 166

def find_by_reviewer(params={})
end

.find_by_type(params = {}) ⇒ Object



163
164
# File 'lib/nytimes/movies/review.rb', line 163

def find_by_type(params={})
end

.format_date_arg(arg) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/nytimes/movies/review.rb', line 37

def format_date_arg(arg)
	return arg if arg.is_a? String
		
	unless arg.respond_to? :strftime
		raise "Date argument must respond to strftime"
	end
	
	arg.strftime "%Y-%m-%d"
end

.parse_date(value) ⇒ Object



24
25
26
27
# File 'lib/nytimes/movies/review.rb', line 24

def parse_date(value)
	return nil if value.nil?
	Date.parse(value)
end

.rename_hash_key(hash, key, new_key) ⇒ Object



29
30
31
# File 'lib/nytimes/movies/review.rb', line 29

def rename_hash_key(hash, key, new_key)
	hash[new_key] = hash.delete(key)
end