Class: OnsOpenApi::Context

Inherits:
Object
  • Object
show all
Includes:
Morph
Defined in:
lib/ons_openapi/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x) ⇒ Context

Returns a new instance of Context.



11
12
13
14
# File 'lib/ons_openapi/context.rb', line 11

def initialize x
  self.id = x.context_id
  self.name = x.context_name
end

Class Method Details

.allObject



6
7
8
# File 'lib/ons_openapi/context.rb', line 6

def all
  @contexts ||= OnsOpenApi::get('contexts').context_list.statistical_contexts.map {|x| new x}
end

Instance Method Details

#classification_namesObject



56
57
58
# File 'lib/ons_openapi/context.rb', line 56

def classification_names
  names_for classifications
end

#classificationsObject



28
29
30
# File 'lib/ons_openapi/context.rb', line 28

def classifications
  @classifications ||= OnsOpenApi::get('classifications', context: @name).classification_list.classifications
end

#collection(id_or_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ons_openapi/context.rb', line 32

def collection id_or_name
  collection = collections.detect{|c| (c.id == id_or_name || c.title == id_or_name) }

  unless collection
    list = collections.select{|c| c.name == id_or_name}
    if list.size > 1
      cmds = list.map{|c| [c.id,c.title]}.flatten.map{|n| "collection('#{n}')"}
      raise "more than one match, try one of:\n\n  #{cmds.join("  \n\n  ") }\n\n"
    else
      collection = list.first
    end
  end

  collection
end

#collection_namesObject



52
53
54
# File 'lib/ons_openapi/context.rb', line 52

def collection_names
  names_for collections
end

#collectionsObject



20
21
22
23
24
25
26
# File 'lib/ons_openapi/context.rb', line 20

def collections
  @collections ||= OnsOpenApi::get('collections', context: @name).collection_list.collections
  unless @collections.first.respond_to?(:context_name) && @collections.first.context_name
    @collections.each {|c| c.context_name = @name}
  end
  @collections
end

#concept_namesObject



48
49
50
# File 'lib/ons_openapi/context.rb', line 48

def concept_names
  names_for concepts
end

#conceptsObject



16
17
18
# File 'lib/ons_openapi/context.rb', line 16

def concepts
  @concepts ||= OnsOpenApi::get('concepts', context: @name).concept_list.concepts
end

#electoral_divisionsObject

Returns geography objects from the 2011 Administrative Hierarchy with area type ‘Electoral Division’



90
91
92
# File 'lib/ons_openapi/context.rb', line 90

def electoral_divisions
  geographies('2011WARDH').select {|z| z.area_type.codename['Electoral Division']}
end

#electoral_wardsObject

Returns geography objects from the 2011 Administrative Hierarchy with area type ‘Electoral Ward/Division’



96
97
98
# File 'lib/ons_openapi/context.rb', line 96

def electoral_wards
  geographies('2011WARDH').select {|z| z.area_type.codename['Electoral Ward/Division']}
end

#geographies(code = '2011WARDH') ⇒ Object

Returns geography objects for given geography code.

Parameter code defaults to ‘2011WARDH’ for the 2011 Administrative Hierarchy, if no code supplied.

Codes include: 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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ons_openapi/context.rb', line 72

def geographies code='2011WARDH'
  @geographies ||= {}
  unless @geographies[code]
    params = { context: @name }

    if code == '2011STATH'
      params.merge!({ levels: '0,1,2,3,4,5' }) # restrict levels to reduce delay
    end

    result = OnsOpenApi::get "hierarchies/hierarchy/#{code}", params
    @geographies[code] = result.geography_list.items.items
  end
  @geographies[code].each {|g| g.geography_code = code }
  @geographies[code]
end