Module: Query::Nyc

Defined in:
lib/query-nyc.rb,
lib/query-nyc/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/query-nyc.rb', line 3

def address
  @address
end

Class Method Details

.building_at(houseno, street, boro) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/query-nyc.rb', line 13

def self.building_at(houseno, street, boro)
  require "nokogiri"
  
  boro_option = name_to_number(boro)
  
  uri = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?" + "boro=#{boro_option}" + "&houseno=#{houseno}" + "&street=#{CGI.escape(street)}"

  @page = Nokogiri::HTML(open(uri))

  retObj = {:landlords=>[],:building=>[]}

  msg = @page.css(".errormsg").first.content rescue nil
  if msg == nil
    tax_block = @page.xpath("//table[3]/tr[2]/td[9]/text()").to_s.sub(/:/,'').to_i
    tax_lot = @page.xpath("//table[3]/tr[3]/td[9]/text()").to_s.sub(/:/,'').to_i

    violations_count = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()").to_s
    open_violations = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]/text()").to_s
    violations_link = "http://a810-bisweb.nyc.gov/bisweb/" + @page.xpath("//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//b//a").attribute("href").to_s rescue ""
  
    complaints_count = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()")[0].to_s
    open_complaints = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]/text()").to_s
    complaints_link = "http://a810-bisweb.nyc.gov/bisweb/" + @page.xpath("//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//b//a").attribute("href").to_s rescue ""
  
    landmark_status = @page.xpath('//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//*[contains(concat( " ", @class, " " ), concat( " ", "content", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()').to_s
  
    @page = Nokogiri::HTML(open('http://api.blocksandlots.com/blankslate/json/data/743cd788-eb98-4fb6-af18-0811261ad168/records/search?apikey=cvq842zthjdvr25cq9p5s6db&Block='+tax_block.to_s+'&Lot='+tax_lot.to_s+'&rp=350&em=true&_=1289069608577'))
  
    arr_of_landlords = retObj[:landlords]
    arr_of_building = retObj[:building]
    jdata = JSON.parse(@page.content) 
    owners_array = jdata["application"][0]["entity"][0]["record"]
    owners_array.each do |owner|
      ret = {}
      skip = false
      owner["field"].each { |f|
        case f["fieldName"] 
          when "Owner Name"
            if f["fieldValue"].strip.present?
              ret[:owner] = f["fieldValue"].split(/\s+/).each{ |word| word.capitalize! }.join(' ')
            else
              skip = true  
            end
        end
      }
      arr_of_landlords.push(ret) unless skip
    end
    
    r = {}
    owners_array[0]["field"].each do |f|
      case f["fieldName"]
        when "Actual Land Value"; r[:land_value] = f["fieldValue"]
        when "Actual Total Value"; r[:total_value] = f["fieldValue"]
        when "Market Value"; r[:market_value] = f["fieldValue"]
        when "Assessment Year"; r[:assessment_year] = f["fieldValue"]
      end
    end
    arr_of_building.push(r)
    
    arr_of_building[0][:tax_block] = tax_block
    arr_of_building[0][:tax_lot] = tax_lot
    arr_of_building[0][:violations_count] = violations_count
    arr_of_building[0][:open_violations] = open_violations
    arr_of_building[0][:violations_link] = violations_link
    arr_of_building[0][:complaints_count] = complaints_count
    arr_of_building[0][:open_complaints] = open_complaints
    arr_of_building[0][:complaints_link] = complaints_link
    arr_of_building[0][:landmark_status] = landmark_status.present?
  end
  # Returns a hash of landlords, and the building.
  retObj
end

.check_existence_with_google(address) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/query-nyc.rb', line 104

def self.check_existence_with_google(address)
  base = "http://maps.googleapis.com/maps/api/geocode/json?address="
  addr = "#{address},NY".gsub(/\s+/,'+')
  uri = "#{base}#{addr}&sensor=false"
  
  google = open(uri).read
  json = JSON.parse(google)
  
  partial_match = json["results"][0]["partial_match"] rescue false
  approximate = json["results"][0]["geometry"]["location_type"] == "APPROXIMATE"
  
  if (approximate.present? or partial_match.present?)
    false
  else
    # Scoop the address here
    self.address =   json["results"][0]["formatted_address"] rescue ""
  
    true
  end
  # Returns true or false
end

.fetch_uri(houseno, street, boro) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/query-nyc.rb', line 5

def self.fetch_uri(houseno, street, boro)
  
  boro_option = name_to_number(boro)
  
  uri = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?" + "boro=#{boro_option}" + "&houseno=#{houseno}" + "&street=#{CGI.escape(street)}"
  
end

.geocode(address) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/query-nyc.rb', line 126

def self.geocode(address)
  require 'uri'
  require 'rest-client'
  
  j = ActiveSupport::JSON
  base_url="http://maps.googleapis.com/maps/api/geocode/json?address="
  url = "#{base_url}#{CGI.escape(address)}&sensor=false"
  retObj = {:lat=>"",:lng=>""}
  RestClient.get(url) do |response, request, result| 
    case response.code
    when 200
      res = j.decode(response)["results"][0]
      loc = res["geometry"]["location"] rescue ""
      addr = res["address_components"] rescue ""
      
      retObj[:lat] = loc["lat"] rescue ""
      retObj[:lng] = loc["lng"] rescue ""  
      retObj[:sublocality] = addr[2]["long_name"] rescue ""  
    else 
      "BAD RESPONSE"
    end   
  end   
  retObj
end

.name_to_number(name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/query-nyc.rb', line 86

def self.name_to_number(name)
  
  case name
    when "Manhattan"
      1
    when "The Bronx"
      2
    when "Brooklyn"
      3
    when "Queens"
      4
    when "Staten Island"
      5
    else
      0  
  end
end