lib/osm/api.rb in osm-0.0.13 vs lib/osm/api.rb in osm-0.0.14

- old
+ new

@@ -18,10 +18,11 @@ # Most items however will be cached for this time @@user_access = Hash.new @@cache_prepend_to_key = 'OSMAPI' @@cache = nil + @@debug = false # Initialize a new API connection # If passing user details then both must be passed # @param [String] userid osm userid of the user to act as # @param [String] secret osm secret of the user to act as @@ -45,10 +46,11 @@ # @option options [String] :api_name the name displayed in the External Access tab of OSM # @option options [Symbol] :api_sate wether to use OSM (if :scout) or OGM (if :guide) # @option options [Class] :cache (optional) An instance of a cache class, must provide the methods (exist?, delete, write, read), for details see Rails.cache. Whilst this is optional you should remember that caching is required to use the OSM API. # @option options [Fixnum] :default_cache_ttl (optional, default = 30.minutes) The default TTL value for the cache, note that some items are cached for twice this time and others are cached for half this time (in seconds) # @option options [String] :cache_prepend_to_key (optional, default = 'OSMAPI') Text to prepend to the key used to store data in the cache + # @option options [Boolean] :debug if true debugging info is output (options, default = false) # @return nil def self.configure(options) raise ArgumentError, ':api_id does not exist in options hash' if options[:api_id].nil? raise ArgumentError, ':api_token does not exist in options hash' if options[:api_token].nil? raise ArgumentError, ':api_name does not exist in options hash' if options[:api_name].nil? @@ -65,10 +67,11 @@ @@api_name = options[:api_name].to_s @@api_site = options[:api_site] @@default_cache_ttl = options[:default_cache_ttl].to_i unless options[:default_cache_ttl].nil? @@cache_prepend_to_key = options[:cache_prepend_to_key].to_s unless options[:cache_prepend_to_key].nil? @@cache = options[:cache] + @@debug = !!options[:debug] nil end # Get the API ID used in this class # @return [String] the API ID @@ -487,16 +490,17 @@ end data = perform_query("users.php?action=register&sectionid=#{section_id}&termid=#{term_id}", api_data) data = data['items'] + to_return = [] data.each do |item| - item = Osm::RegisterData.from_api(item) + to_return.push Osm::RegisterData.from_api(item) end self.user_can_access :register, section_id, api_data cache_write("register-#{section_id}-#{term_id}", data, :expires_in => @@default_cache_ttl/2) - return data + return to_return end # Create an evening in OSM # @param [Fixnum] section_id the id of the section to add the term to # @param [Date] meeting_date the date of the meeting @@ -593,15 +597,25 @@ api_data['userid'] = @userid api_data['secret'] = @secret end end + if @@debug + puts "Making OSM API request to #{url}" + puts api_data.to_s + end + begin result = HTTParty.post("#{@base_url}/#{url}", {:body => api_data}) rescue SocketError, TimeoutError, OpenSSL::SSL::SSLError raise ConnectionError, 'A problem occured on the internet.' end raise ConnectionError, "HTTP Status code was #{result.response.code}" if !result.response.code.eql?('200') + + if @@debug + puts "Result from OSM request to #{url}" + puts result.response.body + end raise Error, result.response.body unless looks_like_json?(result.response.body) decoded = ActiveSupport::JSON.decode(result.response.body) osm_error = get_osm_error(decoded) raise Error, osm_error if osm_error