module SurveyGizmo::Resource::ClassMethods
These are methods that every API
resource can use to access resources in SurveyGizmo
Attributes
Public Instance Methods
Get an enumerator of resources. @param [Hash] conditions - URL and pagination params with SurveyGizmo
“filters” at the :filters key
Set all_pages: true if you want the gem to page through all the available responses
example: { page: 2, filters: { field: “istestdata”, operator: “<>”, value: 1 } }
The top level keys (e.g. :page, :resultsperpage) get encoded in the url, while the contents of the array of hashes passed at the :filters key get turned into the format SurveyGizmo
expects for its internal filtering.
Properties from the conditions hash (e.g. survey_id) will be added to the returned objects
# File lib/survey_gizmo/resource.rb, line 36 def all(conditions = {}) fail ':all_pages and :page are mutually exclusive' if conditions[:page] && conditions[:all_pages] logger.warn('Only retrieving first page of results!') unless conditions[:page] || conditions[:all_pages] all_pages = conditions.delete(:all_pages) conditions[:resultsperpage] ||= SurveyGizmo.configuration.results_per_page Enumerator.new do |yielder| response = nil while !response || (all_pages && response['page'] < response['total_pages']) conditions[:page] = response ? response['page'] + 1 : conditions.fetch(:page, 1) start_fetch_time = Time.now logger.debug("Fetching #{name} page #{conditions} - #{conditions[:page]}#{response ? "/#{response['total_pages']}" : ''}...") response = Connection.get(create_route(:create, conditions)).body collection = response['data'].map { |datum| datum.is_a?(Hash) ? new(conditions.merge(datum)) : datum } # Sub questions are not pulled by default so we have to retrieve them manually. SurveyGizmo # claims they will fix this bug and eventually all questions will be returned in one request. if self == SurveyGizmo::API::Question collection += collection.flat_map { |question| question.sub_questions } end logger.debug(" Fetched #{conditions[:resultsperpage]} of #{name} in #{(Time.now - start_fetch_time).to_i}s...") collection.each { |e| yielder.yield(e) } end end end
Create a new resource object locally and save to SurveyGizmo
. Returns the newly created Resource
instance.
# File lib/survey_gizmo/resource.rb, line 72 def create(attributes = {}) new(attributes).save end
Replaces the :page_id, :survey_id, etc strings defined in each model's routes with the values in the params hash
# File lib/survey_gizmo/resource.rb, line 93 def create_route(method, params) fail "No route defined for #{method} on #{name}" unless routes[method] url_params = params.dup rest_path = routes[method].gsub(/:(\w+)/) do |m| fail SurveyGizmo::URLError, "Missing RESTful parameters in request: `#{m}`" unless url_params[$1.to_sym] url_params.delete($1.to_sym) end SurveyGizmo.configuration.api_version + rest_path + filters_to_query_string(url_params) end
Delete resources
# File lib/survey_gizmo/resource.rb, line 77 def destroy(conditions) Connection.delete(create_route(:delete, conditions)) end
Retrieve a single resource. See usage comment on .all
# File lib/survey_gizmo/resource.rb, line 67 def first(conditions = {}) new(conditions.merge(Connection.get(create_route(:get, conditions)).body['data'])) end
@route is either a hash to be used directly or a string from which standard routes will be built
# File lib/survey_gizmo/resource.rb, line 82 def routes fail "route not set in #{name}" unless @route return @route if @route.is_a?(Hash) routes = { create: @route } [:get, :update, :delete].each { |k| routes[k] = @route + '/:id' } routes end
Private Instance Methods
Convert a [Hash] of params and internal surveygizmo style filters into a query string
The hashes at the :filters key get turned into URL params like: # filter[0]=istestdata&filter[0]=<>&filter[0]=1
# File lib/survey_gizmo/resource.rb, line 111 def filters_to_query_string(params = {}) return '' unless params && params.size > 0 params = params.dup url_params = {} Array.wrap(params.delete(:filters)).each_with_index do |filter, i| fail "Bad filter params: #{filter}" unless filter.is_a?(Hash) && [:field, :operator, :value].all? { |k| filter[k] } url_params["filter[field][#{i}]".to_sym] = "#{filter[:field]}" url_params["filter[operator][#{i}]".to_sym] = "#{filter[:operator]}" url_params["filter[value][#{i}]".to_sym] = "#{filter[:value]}" end uri = Addressable::URI.new(query_values: url_params.merge(params)) "?#{uri.query}" end
# File lib/survey_gizmo/resource.rb, line 129 def logger SurveyGizmo.configuration.logger end