Class: Presdocs::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/presdocs/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
# File 'lib/presdocs/document.rb', line 6

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

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def category
  @category
end

#cityObject (readonly)

Returns the value of attribute city.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def city
  @city
end

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def date
  @date
end

#fdsys_urlObject (readonly)

Returns the value of attribute fdsys_url.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def fdsys_url
  @fdsys_url
end

#htmlObject (readonly)

Returns the value of attribute html.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def html
  @html
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def id
  @id
end

#latObject (readonly)

Returns the value of attribute lat.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def lng
  @lng
end

#locationObject (readonly)

Returns the value of attribute location.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def location
  @location
end

#notesObject (readonly)

Returns the value of attribute notes.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def notes
  @notes
end

#package_idObject (readonly)

Returns the value of attribute package_id.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def package_id
  @package_id
end

#presidentObject (readonly)

Returns the value of attribute president.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def president
  @president
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def source
  @source
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def state
  @state
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def subjects
  @subjects
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/presdocs/document.rb', line 4

def title
  @title
end

Class Method Details

.category(category) ⇒ Object



73
74
75
76
77
# File 'lib/presdocs/document.rb', line 73

def self.category(category)
  url = "http://m.gpo.gov/wscpd/mobilecpd/category/#{category}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], nil)
end

.city(city, state) ⇒ Object



40
41
42
43
44
# File 'lib/presdocs/document.rb', line 40

def self.city(city, state)
  url = "http://m.gpo.gov/wscpd/mobilecpd/location/#{city}/#{state}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], results['coordinates'])
end

.create_document(result, coordinates, full = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/presdocs/document.rb', line 99

def self.create_document(result, coordinates, full=false)
  if full
    detail = result
    result = result['searchResult']
  end
  city, state = result['location'].split(', ') if result['location']
  if coordinates
    locations = coordinates.map{|l| {"state" => l['state'], "city" => l['city'], "lat" => l["lat"], "lng" => l["lang"]}}.uniq
    lat = locations.detect{|l| l['city'] == city && l['state'] == state}['lat']
    lng = locations.detect{|l| l['city'] == city && l['state'] == state}['lng']
  end
  doc = self.new(:id => result['packageId'],
            :city => city,
            :state => state,
            :lat => lat,
            :lng => lng,
            :title => result['line1'],
            :source => result['line2'],
            :president => result['president'],
            :date => Date.parse(result['eventDate']))
  
  if full
    h = {:category => detail['category'], :notes => detail['notes'], :subjects => detail['subject'].map{|s| s.strip}.reject!(&:empty?), :fdsys_url => detail['fdsysUrl'], :html => detail['fullText']}
    doc.add_attributes(h)
  end
  doc
end

.create_from_search_results(results, coordinates) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/presdocs/document.rb', line 91

def self.create_from_search_results(results, coordinates)
  docs = []
  results.each do |result|
    docs << create_document(result, coordinates)
  end
  docs
end

.date(date) ⇒ Object



58
59
60
61
62
63
# File 'lib/presdocs/document.rb', line 58

def self.date(date)
  d = process_date(date)
  url = "http://m.gpo.gov/wscpd/mobilecpd/date/#{d}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], nil)
end

.date_range(start_date, end_date) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/presdocs/document.rb', line 65

def self.date_range(start_date, end_date)
  s = process_date(start_date)
  e = process_date(end_date)
  url = "http://m.gpo.gov/wscpd/mobilecpd/date/#{s}/#{e}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], nil)
end

.detail(package_id) ⇒ Object



22
23
24
25
26
# File 'lib/presdocs/document.rb', line 22

def self.detail(package_id)
  url = "http://m.gpo.gov/wscpd/mobilecpd/detailwgc/#{package_id}.json"
  result = Oj.load(open(url).read)
  create_document(result, nil, full=true)
end

.latestObject



28
29
30
31
32
# File 'lib/presdocs/document.rb', line 28

def self.latest
  url = "http://m.gpo.gov/wscpd/mobilecpd/home.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], nil)
end

.location_with_distance(lat, lng, distance) ⇒ Object



52
53
54
55
56
# File 'lib/presdocs/document.rb', line 52

def self.location_with_distance(lat, lng, distance)
  url = "http://m.gpo.gov/wscpd/mobilecpd/location/#{lat}/#{lng}/#{distance}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], results['coordinates'])
end

.process_date(date) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/presdocs/document.rb', line 79

def self.process_date(date)
  begin
    if date.is_a?(Date)
      d = date.strftime("%-m-%-d-%Y")
    else
      d = Date.strptime(date, '%m/%d/%Y').strftime("%-m-%-d-%Y")
    end
  rescue
    raise "Dates must be Ruby Date objects or a Date string such as '2/14/2013'"
  end
end

.state(state) ⇒ Object



46
47
48
49
50
# File 'lib/presdocs/document.rb', line 46

def self.state(state)
  url = "http://m.gpo.gov/wscpd/mobilecpd/location/#{state}.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], results['coordinates'])
end

.with_locationsObject



34
35
36
37
38
# File 'lib/presdocs/document.rb', line 34

def self.with_locations
  url = "http://m.gpo.gov/wscpd/mobilecpd/location.json"
  results = Oj.load(open(url).read)
  create_from_search_results(results['searchResults'], results['coordinates'])
end

Instance Method Details

#add_attributes(params) ⇒ Object



12
13
14
15
16
# File 'lib/presdocs/document.rb', line 12

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

#detailObject



18
19
20
# File 'lib/presdocs/document.rb', line 18

def detail
  Document.detail(id)
end