Class: Osm::ApiAccess

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ApiAccess

Initialize a new API Access using the hash returned by the API call

Parameters:

  • data

    the hash of data for the object returned by the API



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/osm/api_access.rb', line 15

def initialize(data)
  @id = data['apiid'].to_i
  @name = data['name']
  @permissions = data['permissions'].is_a?(Hash) ? data['permissions'] : {}

  # Rubyfy permissions hash
  @permissions.keys.each do |key|
    @permissions[key] = @permissions[key].to_i
    @permissions[(key.to_sym rescue key) || key] = @permissions.delete(key) # Symbolize key
  end
end

Instance Attribute Details

#idFixnum (readonly)

Returns the id for the API.

Returns:

  • (Fixnum)

    the id for the API



6
7
8
# File 'lib/osm/api_access.rb', line 6

def id
  @id
end

#nameString (readonly)

Returns the name of the API.

Returns:

  • (String)

    the name of the API



6
7
8
# File 'lib/osm/api_access.rb', line 6

def name
  @name
end

#permissionsHash (readonly)

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



6
7
8
# File 'lib/osm/api_access.rb', line 6

def permissions
  @permissions
end

Instance Method Details

#can_read?(key) ⇒ Boolean

Determine if this API has read access for the provided permission

Parameters:

  • key (Symbol)

    the permission being queried

Returns:

  • (Boolean)

    if this API can read the passed permission



30
31
32
# File 'lib/osm/api_access.rb', line 30

def can_read?(key)
  return [20, 10].include?(@permissions[key])
end

#can_write?(key) ⇒ Boolean

Determine if this API has write access for the provided permission

Parameters:

  • key (Symbol)

    the permission being queried

Returns:

  • (Boolean)

    if this API can write the passed permission



37
38
39
# File 'lib/osm/api_access.rb', line 37

def can_write?(key)
  return [20].include?(@permissions[key])
end

#our_api?Boolean

Determine if this API is the API being used to make requests

Returns:

  • (Boolean)

    if this is the API being used



43
44
45
# File 'lib/osm/api_access.rb', line 43

def our_api?
  return @id == Osm::Api.api_id.to_i
end