Class: Osm::Api
- Inherits:
-
Object
- Object
- Osm::Api
- Defined in:
- lib/osm/api.rb
Constant Summary collapse
- BASE_URLS =
{ :osm => 'https://www.onlinescoutmanager.co.uk', :ogm => 'http://www.onlineguidemanager.co.uk', }
- @@site =
Default options
nil
- @@debug =
Used to make requests from an instance
false
- @@api_details =
Puts helpful information whilst connected to OSM/OGM
{:osm=>{}, :ogm=>{}}
Class Method Summary collapse
-
.authorize(site = @@site, email, password) ⇒ Hash
Get the userid and secret to be able to act as a certain user on the OSM/OGM system.
-
.configure(options) ⇒ Object
Configure the API options used by all instances of the class.
-
.debug ⇒ Object
The debug option.
-
.debug=(debug) ⇒ Object
Configure the debug option.
Instance Method Summary collapse
-
#api_id ⇒ String
Get the API ID.
-
#api_name ⇒ String
Get the API name.
-
#get_user_permissions(options = {}) ⇒ Object
Get API user’s permissions.
-
#initialize(user_id, secret, site = @@site) ⇒ Object
constructor
Initialize a new API connection.
-
#perform_query(url, api_data = {}) ⇒ Hash, ...
Make a query to the OSM/OGM API.
-
#set_user(user_id, secret) ⇒ Osm::Api
Set the OSM user to make future requests as.
-
#set_user_permissions(section, permissions) ⇒ Object
Set access permission for an API user for a given Section.
-
#site ⇒ Symbol
Get the site this Api currently uses.
- #to_i ⇒ Object
-
#user_id ⇒ String
Get the current user_id.
Constructor Details
#initialize(user_id, secret, site = @@site) ⇒ Object
Initialize a new API connection
71 72 73 74 75 76 77 78 79 |
# File 'lib/osm/api.rb', line 71 def initialize(user_id, secret, site=@@site) raise ArgumentError, 'You must pass a secret (get this by using the authorize method)' if secret.nil? raise ArgumentError, 'You must pass a user_id (get this by using the authorize method)' if user_id.nil? raise ArgumentError, 'site is invalid, if passed it should be either :osm or :ogm, if not passed then you forgot to run Api.configure' unless [:osm, :ogm].include?(site) @site = site set_user(user_id, secret) nil end |
Class Method Details
.authorize(site = @@site, email, password) ⇒ Hash
Get the userid and secret to be able to act as a certain user on the OSM/OGM system
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/osm/api.rb', line 119 def self.(site=@@site, email, password) api_data = { 'email' => email, 'password' => password, } data = perform_query(site, 'users.php?action=authorise', api_data) return { :user_id => data['userid'], :secret => data['secret'], } end |
.configure(options) ⇒ Object
Configure the API options used by all instances of the class
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/osm/api.rb', line 31 def self.configure() raise ArgumentError, ':default_site does not exist in options hash or is invalid, this should be set to either :osm or :ogm' unless [:osm, :ogm].include?([:default_site]) raise ArgumentError, ':osm and/or :ogm must be present' if [:osm].nil? && [:ogm].nil? [:osm, :ogm].each do |api_key| if [api_key] api_data = [api_key] raise ArgumentError, ":#{api_key} must be a Hash" unless api_data.is_a?(Hash) [:id, :token, :name].each do |key| raise ArgumentError, ":#{api_key} must contain a key :#{key}" if api_data[key].nil? end end end @@site = [:default_site] @@debug = !![:debug] @@api_details = { :osm => ([:osm] || {}), :ogm => ([:ogm] || {}), } nil end |
.debug ⇒ Object
The debug option
62 63 64 |
# File 'lib/osm/api.rb', line 62 def self.debug @@debug end |
.debug=(debug) ⇒ Object
Configure the debug option
56 57 58 |
# File 'lib/osm/api.rb', line 56 def self.debug=(debug) @@debug = !!debug end |
Instance Method Details
#api_id ⇒ String
Get the API ID
90 91 92 |
# File 'lib/osm/api.rb', line 90 def api_id @@api_details[@site][:id] end |
#api_name ⇒ String
Get the API name
84 85 86 |
# File 'lib/osm/api.rb', line 84 def api_name @@api_details[@site][:name] end |
#get_user_permissions(options = {}) ⇒ Object
Get API user’s permissions
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/osm/api.rb', line 158 def (={}) cache_key = ['permissions', user_id] if ![:no_cache] && Osm::Model.cache_exist?(self, cache_key) return Osm::Model.cache_read(self, cache_key) end data = perform_query('api.php?action=getUserRoles') = Hash.new data.each do |item| unless item['section'].eql?('discount') # It's not an actual section .merge!(Osm::to_i_or_nil(item['sectionid']) => Osm.(item['permissions'])) end end Osm::Model.cache_write(self, cache_key, ) return end |
#perform_query(url, api_data = {}) ⇒ Hash, ...
Make a query to the OSM/OGM API
147 148 149 150 151 152 |
# File 'lib/osm/api.rb', line 147 def perform_query(url, api_data={}) self.class.perform_query(@site, url, api_data.merge({ 'userid' => @user_id, 'secret' => @user_secret, })) end |
#set_user(user_id, secret) ⇒ Osm::Api
Set the OSM user to make future requests as
136 137 138 139 140 |
# File 'lib/osm/api.rb', line 136 def set_user(user_id, secret) @user_id = user_id @user_secret = secret return self end |
#set_user_permissions(section, permissions) ⇒ Object
Set access permission for an API user for a given Section
181 182 183 184 185 |
# File 'lib/osm/api.rb', line 181 def (section, ) key = ['permissions', user_id] = .merge(section.to_i => ) Osm::Model.cache_write(self, key, ) end |
#site ⇒ Symbol
Get the site this Api currently uses
100 101 102 |
# File 'lib/osm/api.rb', line 100 def site @site end |
#to_i ⇒ Object
93 94 95 |
# File 'lib/osm/api.rb', line 93 def to_i api_id end |
#user_id ⇒ String
Get the current user_id
107 108 109 |
# File 'lib/osm/api.rb', line 107 def user_id @user_id end |