Sha256: 5b885330558866429be6f5ec7c2f9bdabe28cdbfb1da64de467eec0a0aedb42b

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

module TheCity
  require 'cgi'
  require 'json'


  def self.admin_request(method, path, params = {})
    headers = self._build_admin_headers(method, path, params)
    url = THE_CITY_ADMIN_PATH+path

    response =
    case method
    when :post
      Typhoeus::Request.post(url, {:headers => headers, :params => params})
    when :get
      Typhoeus::Request.get(url, {:headers => headers, :params => params})
    when :put
      Typhoeus::Request.put(url, {:headers => headers, :params => params})
    when :delete
      Typhoeus::Request.delete(url, {:headers => headers, :params => params})
    end    

    unless response.success?
      if response.curl_error_message != 'No error'
        raise TheCityExceptions::UnableToConnectToTheCity.new(response.curl_error_message)
      else
        begin
          error_messages = JSON.parse(response.body)['error_message']
        rescue
          raise TheCityExceptions::UnknownErrorConnectingToTheCity.new('Unknown error when connecting to The City')
        else
          raise TheCityExceptions::TheCityResponseError.new(error_messages)
        end
      end
    end    
    
    response
  end


  def self._build_admin_headers(method, path, params)
    get_vars = method == :post ? '' : '?'
    get_vars += params.to_a.sort.collect { |kv_pair| "#{kv_pair[0]}=#{kv_pair[1].to_s}" }.join('&')
    get_vars = '' if get_vars == '?'
    method_request = method.to_s.upcase
    url = THE_CITY_ADMIN_PATH + path + get_vars
    current_time = Time.now.to_i.to_s
    string_to_sign = current_time.to_s + method_request + url
    unencoded_hmac = OpenSSL::HMAC.digest('sha256', TheCity::AdminApi::API_KEY, string_to_sign)
    unescaped_hmac = Base64.encode64(unencoded_hmac).chomp
    hmac_signature = CGI.escape(unescaped_hmac)

    {'X-City-Sig' => hmac_signature,
     'X-City-User-Token' => TheCity::AdminApi::API_TOKEN,
     'X-City-Time' => current_time,
     'Accept' => 'application/vnd.thecity.admin.v1+json',
     'Content-Type' => 'application/json'}
  end 

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
the-city-admin-0.2.1 lib/common.rb
the-city-admin-0.2.0 lib/common.rb
the-city-admin-0.1.5 lib/common.rb
the-city-admin-0.1.4 lib/common.rb
the-city-admin-0.1.2 lib/common.rb