Class: Presdocs::Document
- Inherits:
-
Object
- Object
- Presdocs::Document
- Defined in:
- lib/presdocs/document.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#fdsys_url ⇒ Object
readonly
Returns the value of attribute fdsys_url.
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#lat ⇒ Object
readonly
Returns the value of attribute lat.
-
#lng ⇒ Object
readonly
Returns the value of attribute lng.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#package_id ⇒ Object
readonly
Returns the value of attribute package_id.
-
#president ⇒ Object
readonly
Returns the value of attribute president.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
- .category(category) ⇒ Object
- .city(city, state) ⇒ Object
- .create_document(result, coordinates, full = false) ⇒ Object
- .create_from_search_results(results, coordinates) ⇒ Object
- .date(date) ⇒ Object
- .date_range(start_date, end_date) ⇒ Object
- .detail(package_id) ⇒ Object
- .latest ⇒ Object
- .location_with_distance(lat, lng, distance) ⇒ Object
- .process_date(date) ⇒ Object
- .state(state) ⇒ Object
- .with_locations ⇒ Object
Instance Method Summary collapse
- #add_attributes(params) ⇒ Object
- #detail ⇒ Object
-
#initialize(params = {}) ⇒ Document
constructor
A new instance of Document.
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
#category ⇒ Object (readonly)
Returns the value of attribute category.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def category @category end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def city @city end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def date @date end |
#fdsys_url ⇒ Object (readonly)
Returns the value of attribute fdsys_url.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def fdsys_url @fdsys_url end |
#html ⇒ Object (readonly)
Returns the value of attribute html.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def html @html end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def id @id end |
#lat ⇒ Object (readonly)
Returns the value of attribute lat.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def lat @lat end |
#lng ⇒ Object (readonly)
Returns the value of attribute lng.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def lng @lng end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def location @location end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def notes @notes end |
#package_id ⇒ Object (readonly)
Returns the value of attribute package_id.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def package_id @package_id end |
#president ⇒ Object (readonly)
Returns the value of attribute president.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def president @president end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def source @source end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def state @state end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
4 5 6 |
# File 'lib/presdocs/document.rb', line 4 def subjects @subjects end |
#title ⇒ Object (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 |
.latest ⇒ Object
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_locations ⇒ Object
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 |