Class: OnsOpenApi::Collection
- Inherits:
-
Object
- Object
- OnsOpenApi::Collection
- Includes:
- Morph, DataHelper
- Defined in:
- lib/ons_openapi/collection.rb
Instance Method Summary collapse
-
#collection_detail ⇒ Object
Returns collection_detail object.
-
#data(geog_code, geog, stat = '.toTable()') ⇒ Object
Returns data for given geog_code, geog, and optional JSON-stat command.
-
#data_for(label_or_code) ⇒ Object
Returns data as array of arrays, for a geography that matches label_or_code.
-
#geographies ⇒ Object
Returns hash of geography code to list of geography items.
-
#geography(label_or_code) ⇒ Object
Returns geography item that matches given label_or_code.
-
#geography_codes ⇒ Object
Returns array of [geography_code, description] that are supported by this collection.
-
#title ⇒ Object
Returns title, i.e.
Methods included from UrlHelper
Methods included from NameHelper
Instance Method Details
#collection_detail ⇒ Object
Returns collection_detail object.
110 111 112 |
# File 'lib/ons_openapi/collection.rb', line 110 def collection_detail OnsOpenApi::get(url.first, url.last).collection_detail end |
#data(geog_code, geog, stat = '.toTable()') ⇒ Object
Returns data for given geog_code, geog, and optional JSON-stat command.
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 |
# File 'lib/ons_openapi/collection.rb', line 27 def data geog_code, geog, stat='.toTable()' @json_stats ||={} @json_stats[geog_code + geog] ||= OnsOpenApi::request("dataset/#{id}", context: context_name, geog: geog, "dm/#{geog}" => geog_code, totals: 'false', jsontype: 'json-stat') raw_json_stat = @json_stats[geog_code + geog].gsub("\n", ' ').gsub("'", "").squeeze(' ') if raw_json_stat.include?('ns1faultstring') raise "ONS Exception: #{raw_json_stat.gsub(/(<.?ns1XMLFault>)|(<.?ns1faultstring>)/,'')}" elsif raw_json_stat.include?('errorMessage') raise "ONS Error: #{raw_json_stat}" end begin table = js_context.eval( %Q| JSONstat( JSON.parse(' #{raw_json_stat} ') ).Dataset(0)#{stat} | ) rescue Encoding::UndefinedConversionError => e if e.to_s[/ASCII-8BIT to UTF-8/] raw_json_stat = raw_json_stat.force_encoding('ASCII-8BIT').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') table = js_context.eval( %Q| JSONstat( JSON.parse(' #{raw_json_stat} ') ).Dataset(0)#{stat} | ) else raise "#{e.to_s}: #{raw_json_stat}" end rescue ExecJS::RuntimeError, ExecJS::ProgramError => e raise "#{e.to_s}: #{raw_json_stat}" end if table.is_a?(Array) && (index = table[1].index('Segment_1')) table.each {|row| row.delete_at(index)} end table end |
#data_for(label_or_code) ⇒ Object
Returns data as array of arrays, for a geography that matches label_or_code. e.g. data_for(‘England’), data_for(‘Islington S’), data_for(‘E05002040’) Raises exception if no match or more than one match.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ons_openapi/collection.rb', line 14 def data_for label_or_code if geographies = geography(label_or_code) if geographies.size > 1 cmds = geographies.map {|g| "data_for('#{g.title}') or data_for('#{g.item_code}') see http://statistics.data.gov.uk/doc/statistical-geography/#{g.item_code}"} raise "more than one match, try one of:\n\n #{cmds.join(" \n\n ") }\n\n" else geo = geographies.first data geo.item_code, geo.geography_code end end end |
#geographies ⇒ Object
Returns hash of geography code to list of geography items.
115 116 117 118 119 120 |
# File 'lib/ons_openapi/collection.rb', line 115 def geographies codes = geography_codes.map(&:first) codes.each_with_object({}) do |code, hash| hash[code] = OnsOpenApi.context(context_name).geographies(code) end end |
#geography(label_or_code) ⇒ Object
Returns geography item that matches given label_or_code. Raises exception if there is no match.
101 102 103 104 105 106 107 |
# File 'lib/ons_openapi/collection.rb', line 101 def geography label_or_code if matches = geography_exact_match(label_or_code) || geography_partial_match(label_or_code) matches else raise "no geography match found for: #{label_or_code}" end end |
#geography_codes ⇒ Object
Returns array of [geography_code, description] that are supported by this collection.
e.g. 2011WARDH - 2011 Administrative Hierarchy 2011STATH - 2011 Statistical Geography Hierarchy 2011PCONH - 2011 Westminster Parliamentary Constituency Hierarchy 2011HTWARDH - 2011 Census Merged Ward Hierarchy 2011CMLADH - 2011 Census merged local authority district hierarchy 2011PARISH - 2011 Parish Hierarchy
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ons_openapi/collection.rb', line 75 def geography_codes gh ||= geographical_hierarchies @geographies_data ||= if gh.respond_to?(:geographical_hierarchy) && gh.geographical_hierarchy url, args = gh.geographical_hierarchy.url OnsOpenApi::get(url, args) elsif gh.respond_to?(:geographical_hierarchies) && gh.geographical_hierarchies url, args = gh.geographical_hierarchies.first.url OnsOpenApi::get(url, args) end if @geographies_data list = @geographies_data.geographical_hierarchy_list if list.respond_to?(:geographical_hierarchy) && list.geographical_hierarchy [ [list.geographical_hierarchy.id, list.geographical_hierarchy.name] ] elsif list.respond_to?(:geographical_hierarchies) && list.geographical_hierarchies list.geographical_hierarchies.map{|x| [x.id, x.name]} else [] end else [] end end |
#title ⇒ Object
Returns title, i.e. “id name”
7 8 9 |
# File 'lib/ons_openapi/collection.rb', line 7 def title [id, name].join(' ') end |