Method: Osm::Term.create

Defined in:
lib/osm/term.rb

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

Create a term in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • options (Hash) (defaults to: {})
    • the configuration of the new term

    @option options [Osm::Section, Fixnum] :section (required) section or section_id to add the term to @option options [String] :name (required) the name for the term @option options [Date] :start (required) the date for the start of term @option options [Date] :finish (required) the date for the finish of term

Returns:

  • (Boolean)

    if the operation suceeded or not

Raises:

  • (ArgumentError)


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

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