Class: SCB::DB::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/scb/db/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, api = nil) ⇒ Table

Returns a new instance of Table.



11
12
13
14
# File 'lib/scb/db/table.rb', line 11

def initialize(name, api = nil)
  @name = name
  @api  = api || SCB.api
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



9
10
11
# File 'lib/scb/db/table.rb', line 9

def api
  @api
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/scb/db/table.rb', line 8

def name
  @name
end

Instance Method Details

#codesObject



54
55
56
# File 'lib/scb/db/table.rb', line 54

def codes
  variables.map(&:code)
end

#dataObject



70
71
72
# File 'lib/scb/db/table.rb', line 70

def data
  @data ||= api.get_and_parse(name) || []
end

#inspectObject



66
67
68
# File 'lib/scb/db/table.rb', line 66

def inspect
  name
end

#json_query(search_query = {}) ⇒ Object



20
21
22
23
# File 'lib/scb/db/table.rb', line 20

def json_query(search_query = {})
  body = post_query(search_query, "json")
  api.load_json(body) unless body.nil?
end

#png_query(simple_query, filter = "item") ⇒ Object



25
26
27
# File 'lib/scb/db/table.rb', line 25

def png_query(simple_query, filter = "item")
  post_query parse_simple_query(simple_query, filter), "png"
end

#post_query(search_query, format = "json") ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/scb/db/table.rb', line 29

def post_query(search_query, format = "json")
  api_post(name, {
    query: search_query,
    response: {
      format: format
    }
  })
rescue SCB::HTTP::Exception
  raise SCB::DB::InvalidQuery, "Invalid query"
end

#query(simple_query, filter = "item") ⇒ Object



16
17
18
# File 'lib/scb/db/table.rb', line 16

def query(simple_query, filter = "item")
  json_query parse_simple_query(simple_query, filter)
end

#titleObject



40
41
42
# File 'lib/scb/db/table.rb', line 40

def title
  data['title']
end

#uriObject



44
45
46
# File 'lib/scb/db/table.rb', line 44

def uri
  api_uri
end

#variables(klass = Variable) ⇒ Object



48
49
50
51
52
# File 'lib/scb/db/table.rb', line 48

def variables(klass = Variable)
  @variables ||= data['variables'].map do |v|
    klass.new(v)
  end
end

#write_png_query(filename, simple_query, filter = "item") ⇒ Object



58
59
60
# File 'lib/scb/db/table.rb', line 58

def write_png_query(filename, simple_query, filter = "item")
  write_file filename, png_query(simple_query, filter)
end

#write_png_query_raw(filename, search_query) ⇒ Object



62
63
64
# File 'lib/scb/db/table.rb', line 62

def write_png_query_raw(filename, search_query)
  write_file filename, post_query(search_query, "png")
end