Class: HeyDan::JurisdictionFile

Inherits:
Object
  • Object
show all
Defined in:
lib/heydan/jurisdiction_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ JurisdictionFile

Returns a new instance of JurisdictionFile.



7
8
9
10
11
12
# File 'lib/heydan/jurisdiction_file.rb', line 7

def initialize(opts={})
  @name = opts[:name]
  convert_file_name if @name.include?('.json')
  @name = @name.gsub('jurisdictions/','').gsub('ocd-division/','').gsub(/\.\.\//,'')
  raise "Name is required" if @name.nil?
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



5
6
7
# File 'lib/heydan/jurisdiction_file.rb', line 5

def json
  @json
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/heydan/jurisdiction_file.rb', line 4

def name
  @name
end

Instance Method Details

#add_attribute(key, value) ⇒ Object



98
99
100
101
102
# File 'lib/heydan/jurisdiction_file.rb', line 98

def add_attribute(key, value)
  get_json
  @json['attributes'][key] = value
  @json
end

#add_dataset(value) ⇒ Object



75
76
77
78
79
# File 'lib/heydan/jurisdiction_file.rb', line 75

def add_dataset(value)
  get_json
  @json['datasets'] << value 
  @json
end

#add_identifier(key, value) ⇒ Object



69
70
71
72
73
# File 'lib/heydan/jurisdiction_file.rb', line 69

def add_identifier(key, value)
  get_json
  @json['identifiers'][key] = value
  @json
end

#add_property(key, value) ⇒ Object



91
92
93
94
95
96
# File 'lib/heydan/jurisdiction_file.rb', line 91

def add_property(key, value)
  return false if ['datasets', 'identifiers', 'id', 'entityType', 'attributes'].include?(key)
   get_json
  @json[key] = value
  @json
end

#convert_file_nameObject



35
36
37
38
# File 'lib/heydan/jurisdiction_file.rb', line 35

def convert_file_name
  @name = @name.split('/')[-1]
  @name = "#{@name.gsub('::','/').gsub('.json','')}"
end

#datasetsObject



86
87
88
89
# File 'lib/heydan/jurisdiction_file.rb', line 86

def datasets
  get_json
  @json['datasets']
end

#exists?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/heydan/jurisdiction_file.rb', line 48

def exists?
  File.exists?(file_path)
end

#file_nameObject



40
41
42
# File 'lib/heydan/jurisdiction_file.rb', line 40

def file_name
  "#{@name.gsub(/\//, '::').gsub('ocd-division::', '')}.json"
end

#file_pathObject



52
53
54
# File 'lib/heydan/jurisdiction_file.rb', line 52

def file_path
  File.expand_path(File.join(HeyDan.folders[:jurisdictions], file_name))
end

#get_attribute(key) ⇒ Object



104
105
106
107
# File 'lib/heydan/jurisdiction_file.rb', line 104

def get_attribute(key)
   get_json
  @json['attributes'][key]
end

#get_dataset(key) ⇒ Object



81
82
83
84
# File 'lib/heydan/jurisdiction_file.rb', line 81

def get_dataset(key)
  get_json
  @json['datasets'].select { |d| d['id']==key}[0]
end

#get_identifier(key) ⇒ Object



64
65
66
67
# File 'lib/heydan/jurisdiction_file.rb', line 64

def get_identifier(key)
  get_json
  @json['identifiers'][key]
end

#get_jsonObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/heydan/jurisdiction_file.rb', line 109

def get_json
  if !exists?
    @json ||= initial_json
  else
    file = File.read(file_path).force_encoding('UTF-8')
    @json ||= initial_json if file == ""
  end
  return @json if @json
  @json = JSON.parse(file)
end

#hash_idObject



44
45
46
# File 'lib/heydan/jurisdiction_file.rb', line 44

def hash_id
  HeyDan::Helper.md5_name(@name.gsub(/\.\.\//,''))
end

#idObject



18
19
20
21
# File 'lib/heydan/jurisdiction_file.rb', line 18

def id
  get_json
  @json['id']
end

#initial_jsonObject



60
61
62
# File 'lib/heydan/jurisdiction_file.rb', line 60

def initial_json
  {'id' => @name, 'entityType' => type, 'attributes'=> {}, 'identifiers' => {}, 'datasets' => []}
end

#match_type?(ocd_type) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/heydan/jurisdiction_file.rb', line 23

def match_type?(ocd_type)
  return true if ocd_type.nil?
  if ocd_type.include?(':')
    id_type = id
  else
    id_type = id.split('/').last
    return false if id_type.nil?
    id_type = id_type.split(':')[0]
  end
  !id_type.match(/#{ocd_type.gsub(':all', '.+')}/).nil?
end

#saveObject



120
121
122
123
124
# File 'lib/heydan/jurisdiction_file.rb', line 120

def save
  File.open(file_path, 'w') do |f|
    f.write(@json.to_json)
  end
end

#typeObject



14
15
16
# File 'lib/heydan/jurisdiction_file.rb', line 14

def type
  @name.split('/')[-1].split(':')[0]
end