Class: OnsOpenApi::Collection

Inherits:
Object
  • Object
show all
Includes:
Morph, DataHelper
Defined in:
lib/ons_openapi/collection.rb

Instance Method Summary collapse

Methods included from UrlHelper

#url

Methods included from NameHelper

#name

Instance Method Details

#collection_detailObject

Returns collection_detail object.



113
114
115
# File 'lib/ons_openapi/collection.rb', line 113

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.



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
# File 'lib/ons_openapi/collection.rb', line 28

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.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ons_openapi/collection.rb', line 15

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

#geographiesObject

Returns hash of geography code to list of geography items.



118
119
120
121
122
123
# File 'lib/ons_openapi/collection.rb', line 118

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.



104
105
106
107
108
109
110
# File 'lib/ons_openapi/collection.rb', line 104

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_codesObject

Returns array of [geography_code, description] that are supported by this collection.

e.g. 2011STATH - 2011 Statistical Geography Hierarchy 2014WARDH - 2014 Administrative Hierarchy 2011WARDH - 2011 Administrative Hierarchy 2011PARISH - 2011 Parish Hierarchy 2011PCONH - 2011 Westminster Parliamentary Constituency Hierarchy 2011NAWH - 2011 National Assembley for Wales Electoral Hierarchy 2013HEALTHH - 2013 Health Area Hierarchy 2011BUAH - 2011 Built-up area hierarchy



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ons_openapi/collection.rb', line 78

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

#titleObject

Returns title, i.e. “id name”



7
8
9
# File 'lib/ons_openapi/collection.rb', line 7

def title
  [id, name].join(' ')
end