Class: TruliaAPI

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ TruliaAPI

Returns a new instance of TruliaAPI.



5
6
7
# File 'lib/trulia_api.rb', line 5

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#get_cities(state) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/trulia_api.rb', line 22

def get_cities(state)
  cities = {}
  cities_in_state_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=LocationInfo&function=getCitiesInState&state=#{state}&apikey=#{@api_key}"))
  cities_in_state_xml.css("city").each do |city|
    cities[city.css("name").text.downcase] = {
      zillow_id: city.css("cityid").text.to_i,
      longitude: city.css("longitude").text.to_d,
      latitude: city.css("latitude").text.to_d
    }
  end
  return cities
end

#get_city_stats(city, state, startdate, enddate) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/trulia_api.rb', line 71

def get_city_stats(city, state, startdate, enddate)
  city_stats = {}
  bedrooms = {}
  city_stats_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=TruliaStats&function=getCityStats&city=#{CGI::escape(city)}&state=#{state}&startDate=#{startdate}&endDate=#{enddate}&apikey=#{@api_key}"))

  city_stats_xml.css("listingstat").each do |listingstat|
    weekend_date          = listingstat.css("weekendingdate").text.downcase
    listingstat.css("subcategory").each do |subcategory|
      type                  = subcategory.css("type").text.downcase

      bedrooms[type] = {
        properties:           subcategory.css("numberofproperties").text,
        medianlistingprice:   subcategory.css("medianlistingprice").text,
        averagelistingprice:  subcategory.css("averagelistingprice").text
      }
      city_stats[weekend_date] = bedrooms
    end
  end
  return city_stats
end

#get_counties(state) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trulia_api.rb', line 47

def get_counties(state)
  counties = {}
  counties_in_state_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=LocationInfo&function=getCountiesInState&state=#{state}&apikey=#{@api_key}"))
  counties_in_state_xml.css("county").each do |county|
    counties[county.css("name").text.downcase] = {
      zillow_id: county.css("countyid").text.to_i,
      longitude: county.css("longitude").text.to_d,
      latitude: county.css("latitude").text.to_d
    }
  end
  return counties
end

#get_county_stats(county, state, startdate, enddate) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/trulia_api.rb', line 155

def get_county_stats(county, state, startdate, enddate)
  county_stats = {}
  bedrooms = {}
  county_stats_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=TruliaStats&function=getCountyStats&county=#{CGI::escape(county)}&state=#{state}&startDate=#{startdate}&endDate=#{enddate}&apikey=#{@api_key}"))

  county_stats_xml.css("listingstat").each do |listingstat|
    weekend_date          = listingstat.css("weekendingdate").text.downcase
    listingstat.css("subcategory").each do |subcategory|
      type                  = subcategory.css("type").text.downcase

      bedrooms[type] = {
        properties:           subcategory.css("numberofproperties").text,
        medianlistingprice:   subcategory.css("medianlistingprice").text,
        averagelistingprice:  subcategory.css("averagelistingprice").text
      }
      county_stats[weekend_date] = bedrooms
    end
  end
  return county_stats
end

#get_neighborhood_stats(nid, startdate, enddate) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/trulia_api.rb', line 134

def get_neighborhood_stats(nid, startdate, enddate)
  neighborhood_stats = {}
  bedrooms = {}
  neighborhood_stats_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=TruliaStats&function=getNeighborhoodStats&neighborhoodId=#{nid}&startDate=#{startdate}&endDate=#{enddate}&apikey=#{@api_key}"))

  neighborhood_stats_xml.css("listingstat").each do |listingstat|
    weekend_date          = listingstat.css("weekendingdate").text.downcase
    listingstat.css("subcategory").each do |subcategory|
      type                  = subcategory.css("type").text.downcase

      bedrooms[type] = {
        properties:           subcategory.css("numberofproperties").text,
        medianlistingprice:   subcategory.css("medianlistingprice").text,
        averagelistingprice:  subcategory.css("averagelistingprice").text
      }
      neighborhood_stats[weekend_date] = bedrooms
    end
  end
  return neighborhood_stats
end

#get_neighborhoods(city, state) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/trulia_api.rb', line 60

def get_neighborhoods(city, state)
  neighborhoods = {}
  neighborhoods_in_city_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=LocationInfo&function=getNeighborhoodsInCity&city=#{CGI::escape(city)}&state=#{state}&apikey=#{@api_key}"))
  neighborhoods_in_city_xml.css("neighborhood").each do |neighborhood|
    neighborhoods[neighborhood.css("name").text.downcase] = {
      zillow_id: neighborhood.css("id").text.to_i,
    }
  end
  return neighborhoods
end

#get_state_stats(state, startdate, enddate) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/trulia_api.rb', line 92

def get_state_stats(state, startdate, enddate)
  state_stats = {}
  bedrooms = {}
  state_stats_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=TruliaStats&function=getStateStats&state=#{state}&startDate=#{startdate}&endDate=#{enddate}&apikey=#{@api_key}"))

  state_stats_xml.css("listingstat").each do |listingstat|
    weekend_date          = listingstat.css("weekendingdate").text.downcase
    listingstat.css("subcategory").each do |subcategory|
      type                  = subcategory.css("type").text.downcase

      bedrooms[type] = {
        properties:           subcategory.css("numberofproperties").text,
        medianlistingprice:   subcategory.css("medianlistingprice").text,
        averagelistingprice:  subcategory.css("averagelistingprice").text
      }
      state_stats[weekend_date] = bedrooms
    end
  end
  return state_stats
end

#get_statesObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/trulia_api.rb', line 9

def get_states
  states = {}
  states_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=LocationInfo&function=getStates&apikey=#{@api_key}"))
  states_xml.css("state").each do |state|
    states[state.css("name").text.downcase] = {
      abbreviation: state.css("statecode").text.downcase,
      longitude: state.css("longitude").text.to_d,
      latitude: state.css("latitude").text.to_d
    }
  end
  states
end

#get_zip_codes(state) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trulia_api.rb', line 35

def get_zip_codes(state)
  zips = {}
  zip_codes_in_state_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=LocationInfo&function=getZipCodesInState&state=#{state}&apikey=#{@api_key}"))
  zip_codes_in_state_xml.css("zipcode").each do |zip|
    zips[zip.css("name").text.downcase] = {
      longitude: zip.css("longitude").text.to_d,
      latitude: zip.css("latitude").text.to_d
    }
  end
  return zips
end

#get_zip_stats(zip, startdate, enddate) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/trulia_api.rb', line 113

def get_zip_stats(zip, startdate, enddate)
  zip_stats = {}
  bedrooms = {}
  zip_stats_xml = Nokogiri::HTML(open("http://api.trulia.com/webservices.php?library=TruliaStats&function=getZipCodeStats&zipCode=#{zip}&startDate=#{startdate}&endDate=#{enddate}&apikey=#{@api_key}"))

  zip_stats_xml.css("listingstat").each do |listingstat|
    weekend_date          = listingstat.css("weekendingdate").text.downcase
    listingstat.css("subcategory").each do |subcategory|
      type                  = subcategory.css("type").text.downcase

      bedrooms[type] = {
        properties:           subcategory.css("numberofproperties").text,
        medianlistingprice:   subcategory.css("medianlistingprice").text,
        averagelistingprice:  subcategory.css("averagelistingprice").text
      }
      zip_stats[weekend_date] = bedrooms
    end
  end
  return zip_stats
end