Skip to content

Instantly share code, notes, and snippets.

@drewbaumann
Last active December 16, 2015 17:59
Show Gist options
  • Save drewbaumann/5474193 to your computer and use it in GitHub Desktop.
Save drewbaumann/5474193 to your computer and use it in GitHub Desktop.
require_relative "string"
module Mirror
module Api
module HashKeyConversion
def convert_hash_keys(value, case_type="")
case value
when Array
value.map { |v| convert_hash_keys(v) }
# or `value.map(&method(:convert_hash_keys))`
when Hash
Hash[value.map { |k, v| [k.to_s.__send__(case_type), convert_hash_keys(v)] }]
else
value
end
end
def convert_hash_keys_to_camel_case(value)
convert_hash_keys(value,"camel_case_lower")
end
def convert_hash_keys_to_underscore(value)
convert_hash_keys(value,"underscore")
end
end
end
end
require "mirror-api/version"
require "mirror-api/base"
require "mirror-api/client"
require "mirror-api/oauth"
require "mirror-api/string"
require_relative "base"
require_relative "hash_key_conversion"
module Mirror
module Api
HOST = "https://www.googleapis.com"
class Request < Mirror::Api::Base
extend HashKeyConversion
TIMELINE = "timeline"
SUBSCRIPTIONS = "subscriptions"
LOCATIONS = "locations"
CONTACTS = "contacts"
def initialize(creds, options={})
@resource = options[:resource] || TIMELINE
@id = options[:id]
@params = options[:params]
@expected_response = options[:expected_response]
host = options[:host] || HOST
super(creds, options[:raise_errors], host, options[:logger])
end
def invoke_url
return @invoke_url unless @invoke_url.nil?
@invoke_url ="#{self.host}/mirror/v1/#{@resource}/#{@id ? @id : ''}"
@invoke_url += "/attachments/#{attachment_id ? attachment_id : ''}" if attachments
@invoke_url
end
def attachment_id
attachments[:id] if attachments
end
def attachments
params[:attachments] if params
end
def params
return @params unless @params.nil?
@params = Request.convert_hash_keys_to_camel_case(@params)
end
def ret_val
@data = Request.convert_hash_keys_to_underscore(@data)
Hashie::Mash.new(@data)
end
def expected_response
@expected_response
end
end
end
end
String.class_eval do
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
def camel_case_lower
self.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment