Module: Nytimes::Style

Defined in:
lib/nytimes-style.rb,
lib/nytimes-style/version.rb

Constant Summary collapse

VERSION =
"0.6.0"

Instance Method Summary collapse

Instance Method Details

#nytimes_date(date, opts = {}) ⇒ Object

> “Abbreviate the names of months from August through > February in news copy when they are followed by numerals: Aug. 1; Sept. > 2; Oct. 3; Nov. 4; Dec. 5; Jan. 6; Feb. 7. Do not abbreviate March, > April, May, June and July except as a last resort in a chart or table.”



26
27
28
29
30
31
32
# File 'lib/nytimes-style.rb', line 26

def nytimes_date(date, opts={})
  str = ""
  str << date.strftime('%A, ') if opts[:day_of_week]
  str << nytimes_month_and_day(date)
  str << ", #{date.year}" unless opts[:hide_current_year] && date.year == Date.today.year
  return str
end

#nytimes_month(month) ⇒ Object



38
39
40
41
# File 'lib/nytimes-style.rb', line 38

def nytimes_month(month)
  raise ArgumentError.new "Unknown month: #{month}" unless (1..12).include? month
  %w(Jan. Feb. March April May June July Aug. Sept. Oct. Nov. Dec.)[month - 1]
end

#nytimes_month_and_day(date) ⇒ Object



34
35
36
# File 'lib/nytimes-style.rb', line 34

def nytimes_month_and_day(date)
  "#{nytimes_month date.month} #{date.day}"
end

#nytimes_number(n) ⇒ Object

> “In general, spell out the first nine cardinal and > ordinal numbers [but] spell any number that begins a sentence…” Exceptions include “ages of people and animals,” “sums of money,” “degrees of temperature” and “mentions of the Twelve Apostles and the Ten Commandments.”



14
15
16
17
18
19
20
# File 'lib/nytimes-style.rb', line 14

def nytimes_number(n)
  if n < 10
    %w(one two three four five six seven eight nine)[n - 1]
  else
    n.to_s
  end
end

#nytimes_state_abbrev(state_name_or_code) ⇒ Object

> “The abbreviation to be used for each state, after the names of cities, > towns and counties&hellip; Use no > spaces between initials like N.H. Do not abbreviate Alaska, Hawaii, Idaho, > Iowa, Ohio and Utah. (Do not ordinarily use the Postal Service’s > two-letter abbreviations; some are hard to tell apart on quick reading.)”



59
60
61
62
63
# File 'lib/nytimes-style.rb', line 59

def nytimes_state_abbrev(state_name_or_code)
  state = states[state_name_or_code]
  raise ArgumentError.new "Unknown postal code, state name or FIPS code: #{state_name_or_code}" unless state
  state['nytimes_abbrev']
end

#nytimes_state_name(state_abbrev_or_code) ⇒ Object



65
66
67
68
69
# File 'lib/nytimes-style.rb', line 65

def nytimes_state_name(state_abbrev_or_code)
  state = states[state_abbrev_or_code]
  raise ArgumentError.new "Unknown postal code, abbreviation or FIPS code: #{state_abbrev_or_code}" unless state
  state['name']
end

#nytimes_time(time, opts = {}) ⇒ Object

> “Use numerals in giving clock time: 10:30 a.m.; 10:30. > Do not use half-past 10 except in a direct quotation. > Also avoid the redundant 10:30 a.m. yesterday morning and Monday afternoon at 2 p.m.”



46
47
48
49
50
51
52
# File 'lib/nytimes-style.rb', line 46

def nytimes_time(time, opts={})
  raise ArgumentError.new "Time or DateTime required" unless time.class == DateTime || time.class == Time
  str = ""
  str << time.strftime("%l:%M").strip
  str << time.strftime(" %p").sub('PM','p.m.').sub('AM','a.m.') unless opts[:hide_abbreviation]
  str
end