Method: Osm::Term.create

Defined in:
lib/osm/term.rb

.create(api, options = {}) ⇒ Boolean

Create a term in OSM

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/osm/term.rb', line 130

def self.create(api, options={})
  raise ArgumentError, ":section can't be nil" if options[:section].nil?
  raise ArgumentError, ":name can't be nil" if options[:name].nil?
  raise ArgumentError, ":start can't be nil" if options[:start].nil?
  raise ArgumentError, ":finish can't be nil" if options[:finish].nil?
  require_access_to_section(api, options[:section])

  api_data = {
    'term' => options[:name],
    'start' => options[:start].strftime(Osm::OSM_DATE_FORMAT),
    'end' => options[:finish].strftime(Osm::OSM_DATE_FORMAT),
    'termid' => '0'
  }

  data = api.perform_query("users.php?action=addTerm&sectionid=#{options[:section].to_i}", api_data)

  # The cached terms for the section will be out of date - remove them
  get_all(api, options).each do |term|
    cache_delete(api, ['term', term.id]) if term.section_id == section_id
  end
  cache_delete(api, ['terms', api.user_id])

  return data.is_a?(Hash) && data['terms'].is_a?(Hash)
end