class Arcgis::Connection

Attributes

username[R]

Public Class Methods

new(host:, username: nil, password: nil, token: nil) click to toggle source

Take in username/password upon initialization

# File lib/arcgis/connection.rb, line 9
def initialize(host:, username: nil, password: nil, token: nil)
  @host = host.sub(/\/$/,'')
  @token, @token_expires = token, nil
  @username, @password = username, password

  if username && password
    login(username, password)
  end
end

Public Instance Methods

default_params() click to toggle source
# File lib/arcgis/connection.rb, line 55
def default_params
  @token.nil? ? {f: :json} : {f: :json, token: @token}
end
handle_response(response) click to toggle source
# File lib/arcgis/connection.rb, line 69
def handle_response(response)
  if response.success?
    r = JSON.parse(response.body)
    raise ErrorResponse.new(r["error"]) if r["error"]
    r
  else
    raise response.inspect
  end
end
login(username, password) click to toggle source
# File lib/arcgis/connection.rb, line 20
def login(username, password)
  response = run(
    path: "/generateToken",
    method: "POST",
    body: {username: username, password: password, referer: "http://arcgis.com"}
  )

  @token = response["token"]
  @token_expires = Time.at(response["expires"]/1000)
end
run(path:, method: "GET", params: {}, headers: {}, body: {}) click to toggle source
# File lib/arcgis/connection.rb, line 40
def run(path:, method: "GET", params: {}, headers: {}, body: {})
  full_params = default_params.merge(params)

  # JSON encode array and hash fields
  params, body = sanitize_params(params), sanitize_params(body)

  request = Typhoeus::Request.new(
    @host + path,
    {method: method, params: full_params, headers: headers, body: body}
  )

  handle_response(request.run)
end
sanitize_params(params) click to toggle source

also used on body

# File lib/arcgis/connection.rb, line 61
def sanitize_params(params)
  params.reduce({}) do |agg,(k,v)|
    agg[k] = (v.is_a?(Hash) || v.is_a?(Array)) ? v.to_json : v
    agg
  end
end