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', :osm_staging => 'http://staging.onlinescoutmanager.co.uk', :migration => 'https://migration.onlinescoutmanager.com' }
- @@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.
-
.base_url(site = @@site) ⇒ String
Get the base URL for requests to OSM/OGM.
-
.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.
-
.get_osm_error(data) ⇒ Object
Get the error returned by OSM.
-
.perform_query(site, url, api_data = {}) ⇒ Hash, ...
Make a query to the OSM/OGM API.
Instance Method Summary collapse
-
#api_id ⇒ String
Get the API ID.
-
#api_name ⇒ String
Get the API name.
-
#base_url(site = @site) ⇒ String
Get the base URL for requests to OSM.OGM.
-
#get_user_permissions(options = {}) ⇒ Object
Get API user’s permissions.
-
#get_user_roles(*args) ⇒ Array<Hash>
Get API user’s roles in OSM.
-
#get_user_roles!(options = {}) ⇒ Array<Hash>
Get API user’s roles in OSM.
-
#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
75 76 77 78 79 80 81 82 83 |
# File 'lib/osm/api.rb', line 75 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::Api::BASE_URLS.keys.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
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/osm/api.rb', line 123 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 |
.base_url(site = @@site) ⇒ String
Get the base URL for requests to OSM/OGM
149 150 151 152 |
# File 'lib/osm/api.rb', line 149 def self.base_url(site=@@site) raise ArgumentError, "Invalid site" unless Osm::Api::BASE_URLS.keys.include?(site) BASE_URLS[site] end |
.configure(options) ⇒ Object
Configure the API options used by all instances of the class
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/osm/api.rb', line 33 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::Api::BASE_URLS.keys.include?([:default_site]) raise ArgumentError, ":#{options[:default_site]} does not exist in options hash" if [[:default_site]].nil? Osm::Api::BASE_URLS.keys.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] || {}), :osm_staging => ([:osm_staging] || {}), :migration => ([:migration] || {}) } nil end |
.debug ⇒ Object
The debug option
66 67 68 |
# File 'lib/osm/api.rb', line 66 def self.debug @@debug end |
.debug=(debug) ⇒ Object
Configure the debug option
60 61 62 |
# File 'lib/osm/api.rb', line 60 def self.debug=(debug) @@debug = !!debug end |
.get_osm_error(data) ⇒ Object
Get the error returned by OSM
301 302 303 304 305 306 307 |
# File 'lib/osm/api.rb', line 301 def self.get_osm_error(data) return false unless data.is_a?(Hash) return false if data['ok'] to_return = data['error'] || data['err'] || false to_return = false if to_return.blank? return to_return end |
.perform_query(site, url, api_data = {}) ⇒ Hash, ...
Make a query to the OSM/OGM API
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/osm/api.rb', line 250 def self.perform_query(site, url, api_data={}) raise ArgumentError, 'site is invalid, this should be set to either :osm or :ogm' unless Osm::Api::BASE_URLS.keys.include?(site) data = api_data.merge({ 'apiid' => @@api_details[site][:id], 'token' => @@api_details[site][:token], }) if @@debug puts "Making :#{site} API request to #{url}" hide_values_for = ['secret', 'token'] api_data_as_string = api_data.sort.map{ |key, value| "#{key} => #{hide_values_for.include?(key) ? 'PRESENT' : value.inspect}" }.join(', ') puts "{#{api_data_as_string}}" end begin result = HTTParty.post("#{BASE_URLS[site]}/#{url}", {:body => data}) rescue SocketError, TimeoutError, OpenSSL::SSL::SSLError raise Osm::ConnectionError, 'A problem occured on the internet.' end raise Osm::ConnectionError, "HTTP Status code was #{result.response.code}" if !result.response.code.eql?('200') if @@debug puts "Result from :#{site} request to #{url}" puts "#{result.response.content_type}" puts result.response.body end return nil if result.response.body.empty? case result.response.content_type when 'application/json', 'text/html' begin decoded = ActiveSupport::JSON.decode(result.response.body) if osm_error = get_osm_error(decoded) fail Osm::Error, osm_error if osm_error end return decoded rescue ActiveModel::VERSION::MAJOR.eql?(4) ? JSON::ParserError : MultiJson::ParseError fail Osm::Error, result.response.body end when 'image/jpeg' return result.response.body else fail Osm::Error, "Unhandled content-type: #{result.response.content_type}" end end |
Instance Method Details
#api_id ⇒ String
Get the API ID
94 95 96 |
# File 'lib/osm/api.rb', line 94 def api_id @@api_details[@site][:id] end |
#api_name ⇒ String
Get the API name
88 89 90 |
# File 'lib/osm/api.rb', line 88 def api_name @@api_details[@site][:name] end |
#base_url(site = @site) ⇒ String
Get the base URL for requests to OSM.OGM
157 158 159 |
# File 'lib/osm/api.rb', line 157 def base_url(site=@site) self.class.base_url(site) end |
#get_user_permissions(options = {}) ⇒ Object
Get API user’s permissions
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/osm/api.rb', line 214 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 = Hash.new get_user_roles().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 |
#get_user_roles(*args) ⇒ Array<Hash>
Get API user’s roles in OSM
175 176 177 178 179 180 181 |
# File 'lib/osm/api.rb', line 175 def get_user_roles(*args) begin get_user_roles!(*args) rescue Osm::NoActiveRoles return [] end end |
#get_user_roles!(options = {}) ⇒ Array<Hash>
Get API user’s roles in OSM
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/osm/api.rb', line 188 def get_user_roles!(={}) cache_key = ['user_roles', @user_id] if ![:no_cache] && Osm::Model.cache_exist?(self, cache_key) return Osm::Model.cache_read(self, cache_key) end begin data = perform_query('api.php?action=getUserRoles') Osm::Model.cache_write(self, cache_key, data) return data rescue Osm::Error => e if e..eql?('false') fail Osm::NoActiveRoles, "You do not have any active roles in OSM." else raise e end end end |
#perform_query(url, api_data = {}) ⇒ Hash, ...
Make a query to the OSM/OGM API
165 166 167 168 169 170 |
# File 'lib/osm/api.rb', line 165 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
140 141 142 143 144 |
# File 'lib/osm/api.rb', line 140 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
235 236 237 238 239 |
# File 'lib/osm/api.rb', line 235 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
104 105 106 |
# File 'lib/osm/api.rb', line 104 def site @site end |
#to_i ⇒ Object
97 98 99 |
# File 'lib/osm/api.rb', line 97 def to_i api_id end |
#user_id ⇒ String
Get the current user_id
111 112 113 |
# File 'lib/osm/api.rb', line 111 def user_id @user_id end |