Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created November 1, 2013 01:50
Show Gist options
  • Save warmwaffles/7259951 to your computer and use it in GitHub Desktop.
Save warmwaffles/7259951 to your computer and use it in GitHub Desktop.
module Analytic
class Query < OpenStruct
SPECIAL = {
'start-index' => 'start_index',
'max-results' => 'max_results',
'start_index' => 'start-index',
'max_results' => 'max-results'
}
def to_params
hash = to_h.delete_if { |k,v| v.nil? }
hash.deep_transform_keys! do |key|
SPECIAL.fetch(key.to_s, key.to_s.camelize(:lower))
end
hash
end
def to_json(*)
to_h.to_json
end
def checksum(digest = Digest::SHA256)
params = to_params.sort_by { |k, v| k }.map { |row| row.join('=') }
digest.new.hexdigest(params.join('&'))
end
end
end
# irb(main):001:0> q=Analytic::Query.new
# => #<Analytic::Query>
# irb(main):002:0> q.web_property_id = 1
# => 1
# irb(main):003:0> q.to_params
# => {"webPropertyId"=>1}
# irb(main):004:0> q.start_index = 1
# => 1
# irb(main):005:0> q.to_params
# => {"webPropertyId"=>1, "start-index"=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment