module Rebrandly::Client
Constants
- API_VERSION
- BASE_URL
Public Class Methods
delete(end_point, options = {})
click to toggle source
# File lib/rebrandly_client.rb, line 23 def delete(end_point, options = {}) wrap_request(end_point, options) do |url, http_options, _formatted_options| HTTParty.delete(url, http_options) end end
get(end_point, options = {})
click to toggle source
# File lib/rebrandly_client.rb, line 11 def get(end_point, options = {}) wrap_request(end_point, options) do |url, http_options, formatted_options| HTTParty.get(url, http_options.merge(query: formatted_options)) end end
post(end_point, options = {})
click to toggle source
# File lib/rebrandly_client.rb, line 17 def post(end_point, options = {}) wrap_request(end_point, options) do |url, http_options, formatted_options| HTTParty.post(url, http_options.merge(body: formatted_options.to_json)) end end
Private Class Methods
handle_error(response)
click to toggle source
# File lib/rebrandly_client.rb, line 76 def handle_error(response) parsed_response = response.parsed_response case response.code when 429 raise RateLimitExceeded when 403 raise UsageLimitExceeded.new(parsed_response['source']) else raise Error.new(parsed_response['message']) end end
headers()
click to toggle source
# File lib/rebrandly_client.rb, line 101 def headers raise MissingApiKey unless Rebrandly.api_key h = Rebrandly.workspace ? {'workspace-type' => Rebrandly.workspace} : {} h.merge({ 'Content-type' => 'application/json', 'apikey' => Rebrandly.api_key }) end
lower_camelize(string)
click to toggle source
# File lib/rebrandly_client.rb, line 31 def lower_camelize(string) s = string.split('_').collect(&:capitalize).join s[0].downcase + s[1..-1] end
lower_camelize_keys(object)
click to toggle source
# File lib/rebrandly_client.rb, line 44 def lower_camelize_keys(object) case object when Hash object.inject({}) do |hash, (key, value)| hash[lower_camelize(key.to_s).to_sym] = lower_camelize_keys(value) hash end when Array object.map do |value| lower_camelize_keys(value) end else object end end
underscore(string)
click to toggle source
# File lib/rebrandly_client.rb, line 36 def underscore(string) string.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr('-', '_'). downcase end
underscore_keys(object)
click to toggle source
# File lib/rebrandly_client.rb, line 60 def underscore_keys(object) case object when Hash object.inject({}) do |hash, (key, value)| hash[underscore(key.to_s).to_sym] = underscore_keys(value) hash end when Array object.map do |value| underscore_keys(value) end else object end end
wrap_request(end_point, options = {}) { |url, http_options, formatted_options| ... }
click to toggle source
# File lib/rebrandly_client.rb, line 89 def wrap_request(end_point, options = {}) url = "#{BASE_URL}/#{end_point.sub(/^\//, '')}" http_options = {headers: headers} formatted_options = lower_camelize_keys(options) response = yield(url, http_options, formatted_options) return handle_error(response) unless response.code == 200 underscore_keys(JSON.parse(response.body)) end