module Geoservice::Base

Attributes

metadata[RW]
token[RW]
url[RW]

Public Instance Methods

get(path, options = {}) click to toggle source
# File lib/geoservices/base.rb, line 7
def get(path, options = {})
  begin
    path.gsub!(/%username%/, @username || '')
    uri = URI.parse(path)
    params = { f: 'json' }.merge(options)
    params.merge(token: @token) unless @token.nil? || @token.empty?

    uri.query = URI.encode_www_form(params)

    res = Net::HTTP.get_response(uri)
    return JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)
    throw res.status
  rescue => e
    throw "Error in GeoServices: #{e.message}"
  end
end
post(path, options = {}) click to toggle source
# File lib/geoservices/base.rb, line 24
def post(path, options = {})
  secure = options.delete(:secure) || false
  path.gsub!(/%username%/, @username || '')
  uri = URI.parse(path)
  http = Net::HTTP.new(uri.host, secure ? 443 : uri.port)
  if secure || uri.scheme == 'https'
    http.use_ssl = true
    http.ssl_version = :TLSv1
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  params = { f: 'json' }.merge(options)
  params.merge(token: @token) unless @token.nil? || @token.empty?
  request.body = URI.encode_www_form(params)

  res = http.request(request)
  return JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)
  throw res.status
end