class Citywrapper::ApiBase
Constants
- JSON_HEADERS
Attributes
config[RW]
response[RW]
Public Class Methods
new()
click to toggle source
# File lib/citywrapper/api_base.rb, line 12 def initialize @config = Citywrapper.configuration warn "NOTE: #{self.class} is deprecated and will likely not work in the future" end
Public Instance Methods
request(params: {}, method: :get)
click to toggle source
# File lib/citywrapper/api_base.rb, line 17 def request(params: {}, method: :get) raise 'Please set your API key' unless config.api_key @method = method @params = params @query_uri = build_query run_request process_response end
Private Instance Methods
base_api_uri()
click to toggle source
# File lib/citywrapper/api_base.rb, line 64 def base_api_uri URI(base_api_url) end
base_api_url()
click to toggle source
# File lib/citywrapper/api_base.rb, line 68 def base_api_url if @resource "#{BASE_URL}/#{API_VERSION}/#{@resource}/?key=#{config.api_key}" else "#{BASE_URL}/#{API_VERSION}/?key=#{config.api_key}" end end
build_query()
click to toggle source
# File lib/citywrapper/api_base.rb, line 30 def build_query base_api_uri.tap do |uri| uri.query = [parameters_for_query, uri.query].compact.join('&') end end
parameters_for_query()
click to toggle source
# File lib/citywrapper/api_base.rb, line 36 def parameters_for_query URI.encode_www_form(@params) unless @method == :post end
process_response()
click to toggle source
# File lib/citywrapper/api_base.rb, line 40 def process_response case @response when Net::HTTPNotFound raise "Please check the resource type - #{@resource}" when Net::HTTPForbidden raise 'Please check your API key' end JSON.parse(@response.body, object_class: OpenStruct) end
run_request()
click to toggle source
# File lib/citywrapper/api_base.rb, line 51 def run_request case @method when :get @response = Net::HTTP.get_response(@query_uri) when :post request = Net::HTTP.new(@query_uri.host, @query_uri.port) request.use_ssl = true @response = request.post("#{@query_uri.path}?#{@query_uri.query}", @params.to_json, JSON_HEADERS) else raise 'Only GET or POST' end end