Class: Osm::ApiAccess

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/api_access.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Term

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)


# File 'lib/osm/api_access.rb', line 108

Instance Attribute Details

#idFixnum

Returns the id for the API.

Returns:

  • (Fixnum)

    the id for the API


12
# File 'lib/osm/api_access.rb', line 12

attribute :id, :type => Integer

#nameString

Returns the name of the API.

Returns:

  • (String)

    the name of the API


12
# File 'lib/osm/api_access.rb', line 12

attribute :id, :type => Integer

#permissionsHash

Returns the permissions assigned to this API by the user in OSM.

Returns:

  • (Hash)

    the permissions assigned to this API by the user in OSM


12
# File 'lib/osm/api_access.rb', line 12

attribute :id, :type => Integer

Class Method Details

.get(api, section, for_api, options = {}) ⇒ Osm::ApiAccess

Get API Access for a given API

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the details for

  • for_api (Osm::Api)

    The api (or its ID) to get access for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/osm/api_access.rb', line 90

def self.get(api, section, for_api, options={})
  section_id = section.to_i
  for_api_id = for_api.to_i
  cache_key = ['api_access', api.user_id, section_id, for_api]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = get_all(api, section_id, options)

  data.each do |item|
    return item if item.id == for_api_id
  end
  return nil
end

.get_all(api, section, options = {}) ⇒ Array<Osm::ApiAccess>

Get API access details for a given section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the details for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osm/api_access.rb', line 31

def self.get_all(api, section, options={})
  section_id = section.to_i
  cache_key = ['api_access', api.user_id, section_id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    ids = cache_read(api, cache_key)
    return get_from_ids(api, ids, cache_key, section, options, :get_all)
  end

  data = api.perform_query("ext/settings/access/?action=getAPIAccess&sectionid=#{section_id}")

  permissions_map = {
    10  => [:read],
    20  => [:read, :write],
    100 => [:read, :write, :administer]
  }
  result = Array.new
  ids = Array.new
  data['apis'].each do |item|
    attributes = {}
    attributes[:id] = item['apiid'].to_i
    attributes[:name] = item['name']
    attributes[:permissions] = item['permissions'].is_a?(Hash) ? item['permissions'] : {}
  
    # Rubyify permissions hash
    attributes[:permissions].keys.each do |old_key|
      new_key = (old_key.to_sym rescue old_key)    # Get symbol of the key
      attributes[:permissions][new_key] = attributes[:permissions].delete(old_key)  # Change the key
      attributes[:permissions][new_key] = permissions_map[attributes[:permissions][new_key].to_i] || [] # Translate permissions value
    end
    attributes[:permissions].freeze

    this_item = new(attributes)
    result.push this_item
    ids.push this_item.id
    cache_write(api, [*cache_key, this_item.id], this_item)
  end
  cache_write(api, cache_key, ids)

  return result
end

.get_ours(api, section, options = {}) ⇒ Osm::ApiAccess

Get our API access details for a given section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the details for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:


79
80
81
# File 'lib/osm/api_access.rb', line 79

def self.get_ours(api, section, options={})
  get(api, section, api.api_id, options)
end