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

#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



69
70
71
72
73
# File 'lib/presdocs/document.rb', line 69

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



36
37
38
39
40
# File 'lib/presdocs/document.rb', line 36

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



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

def self.create_document(result, coordinates, full=false)
  if full
    detail = result
    result = result['searchResult']
  end
  city, state = result['location'].split(', ')
  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



87
88
89
90
91
92
93
# File 'lib/presdocs/document.rb', line 87

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

.date(date) ⇒ Object



54
55
56
57
58
59
# File 'lib/presdocs/document.rb', line 54

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



61
62
63
64
65
66
67
# File 'lib/presdocs/document.rb', line 61

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



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

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



24
25
26
27
28
# File 'lib/presdocs/document.rb', line 24

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



48
49
50
51
52
# File 'lib/presdocs/document.rb', line 48

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/presdocs/document.rb', line 75

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



42
43
44
45
46
# File 'lib/presdocs/document.rb', line 42

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



30
31
32
33
34
# File 'lib/presdocs/document.rb', line 30

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