Class: PolicyMap::Client

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

Constant Summary collapse

BOUNDARY_TYPES =
{
  :state                  => 2,
  :county                 => 4,
  :census_tract           => 6,
  :zip                    => 8,
  :block_group            => 15,
  :city                   => 16,
  :congressional_district => 23,
  :assembly_district      => 25,
  :senate_district        => 24
}.freeze
INDICATORS =
{
  :average_vehicles_per_household                   => 9873779,
  :broadcasting                                     => 9584691,
  :distressed_community                             => 9629156,
  :percent_homeowners                               => 9873049,
  :total_population                                 => 9876593,
  :independent_artists                              => 9618303,
  :median_gross_rent                                => 9873661,
  :median_gross_rent_2009                           => 9873663,
  :median_home_value                                => 9873606,
  :median_household_income                          => 9871831,
  :movie_and_sound_industries                       => 9584731,
  :museums_and_historical_sites                     => 9584676,
  :other_info_services                              => 9584624,
  :percent_moved_in_since_1990                      => 9873776,
  :percent_who_commute_to_work_using_public_transit => 9873811,
  :percent_african_american                         => 9876222,
  :percent_native_american                          => 9876623,
  :percent_asian                                    => 9876202,
  :percent_foreign_born                             => 9869060,
  :percent_hispanic                                 => 9876280,
  :percent_pacific_islander                         => 9876468,
  :poverty_rate                                     => 9871807,
  :percent_disabled                                 => 9869050,
  :percent_65_or_older                              => 9869059,
  :percent_under_18                                 => 9869063,
  :percent_college_degree                           => 9873916,
  :percent_high_school_or_less                      => 9873913,
  :percent_mixed_race                               => 9876437,
  :percent_vacant_units                             => 9631221,
  :percent_white                                    => 9876415,
  :percent_graduate_degree                          => 9873904,
  :performing_arts_and_spectator_sports             => 9584608,
  :publishing_industries                            => 9584638,
  :unemployment_rate                                => 9841103,
  :vacancy_rate                                     => 9876608
}.freeze
@@connection =
nil
@@debug =
false
@@default_options =
nil
@@boundary_types_by_id =
nil

Class Method Summary collapse

Class Method Details

.boundary_search(*args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/policymap_wrap/client.rb', line 107

def boundary_search(*args)
  default_options = @@default_options
  default_options[:t] = "bnd"

  options = extract_options!(args)

  raise InsufficientArgsForSearch unless options.has_key?(:boundary_types) || options.has_key?(:boundary_ids)

  options[:boundary_types] = sanitized_boundary_types(options[:boundary_types])
  options[:boundary_ids] = Array(options[:boundary_ids]).join(',') if options.has_key?(:boundary_ids)
  HashUtils.rename_key!(options, :boundary_types, :bt) if options.has_key?(:boundary_types)
  HashUtils.rename_key!(options, :boundary_ids, :bi) if options.has_key?(:boundary_ids)
  HashUtils.rename_key!(options, :lng, :lon) if options.has_key?(:lng)

  options = default_options.merge(options)

  result = get(Endpoint.endpoint_url, options)
  result["bnd"]
end

.boundary_typesObject



78
79
80
# File 'lib/policymap_wrap/client.rb', line 78

def boundary_types
  BOUNDARY_TYPES
end

.containment_search(*args) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/policymap_wrap/client.rb', line 149

def containment_search(*args)
  default_options = @@default_options
  default_options[:t]  = 'bnd'

  options = extract_options!(args)

  raise InsufficientArgsForSearch unless options.has_key?(:boundary_types) && options.has_key?(:boundary_id)

  options[:boundary_types] = sanitized_boundary_types(options[:boundary_types])
  HashUtils.rename_key!(options, :boundary_types, :cbt)
  HashUtils.rename_key!(options, :boundary_id, :bi)


  options = default_options.merge(options)

  result = get(Endpoint.endpoint_url, options)
  result['bnd']
end

.debugObject



74
75
76
# File 'lib/policymap_wrap/client.rb', line 74

def debug
  @@debug
end

.debug=(debug_flag) ⇒ Object



69
70
71
72
# File 'lib/policymap_wrap/client.rb', line 69

def debug=(debug_flag)
  @@debug = debug_flag
  @@connection.debug = @@debug if @@connection
end

.get(endpoint, data = nil) ⇒ Object



168
169
170
171
# File 'lib/policymap_wrap/client.rb', line 168

def get(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  @@connection.get endpoint, data
end

.indicator_search(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/policymap_wrap/client.rb', line 127

def indicator_search(*args)
  default_options = @@default_options
  default_options[:t] = "ind"

  options = extract_options!(args)

  raise InsufficientArgsForSearch unless options.has_key?(:indicators) && (options.has_key?(:boundary_types) || options.has_key?(:boundary_ids))

  options[:indicators] = sanitized_indicators(options[:indicators])
  options[:boundary_types] = sanitized_boundary_types(options[:boundary_types])
  options[:boundary_ids] = Array(options[:boundary_ids]).join(',') if options.has_key?(:boundary_ids)
  HashUtils.rename_key!(options, :indicators, :ii)
  HashUtils.rename_key!(options, :boundary_types, :bt) if options.has_key?(:boundary_types)
  HashUtils.rename_key!(options, :boundary_ids, :bi) if options.has_key?(:boundary_ids)
  HashUtils.rename_key!(options, :lng, :lon) if options.has_key?(:lng)

  options = default_options.merge(options)

  result = get(Endpoint.endpoint_url, options)
  result["ind"]
end

.indicatorsObject



82
83
84
# File 'lib/policymap_wrap/client.rb', line 82

def indicators
  INDICATORS
end

.query_search(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/policymap_wrap/client.rb', line 86

def query_search(*args)
  default_options = @@default_options
  default_options[:t] = "sch"

  options = extract_options!(args)

  raise InsufficientArgsForSearch unless options.has_key?(:boundary_types) && options.has_key?(:query)

  options[:boundary_types] = sanitized_boundary_types(options[:boundary_types])
  HashUtils.rename_key!(options, :boundary_types, :bt)
  HashUtils.rename_key!(options, :query, :ss)
  HashUtils.rename_key!(options, :state, :sst) if options.has_key?(:state)
  HashUtils.rename_key!(options, :county, :sco) if options.has_key?(:county)
  HashUtils.rename_key!(options, :census_tract, :sct) if options.has_key?(:census_tract)

  options = default_options.merge(options)

  result = get(Endpoint.endpoint_url, options)
  result['sch']
end

.set_credentials(client_id, username, password, proxy_url = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/policymap_wrap/client.rb', line 62

def set_credentials(client_id, username, password, proxy_url=nil)
  @@default_options = { :id => client_id, :ty => 'data', :f => 'j', :af => '1' }
  @@connection = Connection.new(client_id, username, password, proxy_url)
  @@connection.debug = @@debug
  true
end