Module: SurveyGizmo::Resource::ClassMethods
- Defined in:
- lib/survey_gizmo/resource.rb
Overview
These are methods that every API resource can use to access resources in SurveyGizmo
Instance Attribute Summary collapse
-
#route ⇒ Object
Returns the value of attribute route.
Instance Method Summary collapse
-
#all(conditions = {}) ⇒ Object
Get an enumerator of resources.
-
#create(attributes = {}) ⇒ Object
Create a new resource object locally and save to SurveyGizmo.
-
#create_route(method, params) ⇒ Object
Replaces the :page_id, :survey_id, etc strings defined in each model’s routes with the values in the params hash.
-
#destroy(conditions) ⇒ Object
Delete resources.
-
#first(conditions = {}) ⇒ Object
Retrieve a single resource.
- #routes ⇒ Object
Instance Attribute Details
#route ⇒ Object
Returns the value of attribute route.
22 23 24 |
# File 'lib/survey_gizmo/resource.rb', line 22 def route @route end |
Instance Method Details
#all(conditions = {}) ⇒ Object
Get an enumerator of resources. 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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(attributes = {}) ⇒ Object
Create a new resource object locally and save to SurveyGizmo. Returns the newly created Resource instance.
72 73 74 |
# File 'lib/survey_gizmo/resource.rb', line 72 def create(attributes = {}) new(attributes).save end |
#create_route(method, params) ⇒ Object
Replaces the :page_id, :survey_id, etc strings defined in each model’s routes with the values in the params hash
93 94 95 96 97 98 99 100 101 102 103 |
# 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 |
#destroy(conditions) ⇒ Object
Delete resources
77 78 79 |
# File 'lib/survey_gizmo/resource.rb', line 77 def destroy(conditions) Connection.delete(create_route(:delete, conditions)) end |
#first(conditions = {}) ⇒ Object
Retrieve a single resource. See usage comment on .all
67 68 69 |
# File 'lib/survey_gizmo/resource.rb', line 67 def first(conditions = {}) new(conditions.merge(Connection.get(create_route(:get, conditions)).body['data'])) end |
#routes ⇒ Object
82 83 84 85 86 87 88 89 |
# 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 |