Class: Opendata::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, options = {}) ⇒ Object

Constructor for Opendata::Client instances

Parameters:

  • url (String) (defaults to: nil)

    the base url to the open data API

  • options (Hash) (defaults to: {})

    assorted options for instantiating the Opendata::Client instance



13
14
15
16
# File 'lib/client.rb', line 13

def initialize(url = nil, options = {})
  opts = options.merge(DEFAULT_HEADERS)
  @connection = Faraday.new(url, opts)
end

Instance Method Details

#dataset_list(params = {}) ⇒ Object

Makes requests for ‘logical collections’ of Dataset resources (zero-to-many potential dataset resources)

Examples:

Search for census datasets, 25 per page and include their related organizations and sites

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_list(q: 'census', page: { size: 25 }, include: 'organizations,sites')

Search for parcel datasets, second page, and include their related organizations and filter the dataset fields to title and url

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_list(q: 'parcels', page: { number: 2 }, include: 'organizations', fields: { datasets: 'title,url'})

Parameters:

  • params (Hash) (defaults to: {})

    query parameters for Dataset resources

Options Hash (params):

  • :q (String)

    The query string for searching against datasets

  • :include (String)

    Comma-separate list of related resources to include. valid options are: ‘organizations’, ‘groups’, ‘sites’, ‘items’

  • :sort (String)

    Sort criteria for the request. prepend a ‘-’ to make the sort descending.

  • :page (Hash)

    Paging on the request. Use page for the number of results per page and page for the page number

  • :fields (Hash)

    The attribute subset you want returned for each object.

Returns:

  • (Object)

    Faraday::Response object



34
35
36
# File 'lib/client.rb', line 34

def dataset_list(params = {})
  connection.get(dataset_list_url(params))
end

#dataset_list_url(params = {}) ⇒ String

Makes requests for ‘logical collections’ of Dataset resources (zero-to-many potential dataset resources)

Examples:

Get the request url for a search for census datasets, 25 per page and include their related organizations and sites

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_list_url(q: 'census', page: { size: 25 }, include: 'organizations,sites')
#=> "/api/v2/datasets?q=census&page%5Bsize%5D=25&include=organizations%2Csites"

Parameters:

  • params (Hash) (defaults to: {})

    query parameters for Dataset resources

Returns:

  • (String)

    request url based on the parameters specfied



46
47
48
# File 'lib/client.rb', line 46

def dataset_list_url(params = {})
  DATASETS_API_PATH + param_to_query_string(params)
end

#dataset_show(id, params = {}) ⇒ Object

Makes requests for a ‘logical object’ for a specific Dataset resource (zero-to-one potential dataset resources)

Examples:

Retrive a dataset and related organizations and sites

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_show('c92b122901ad40ee897d36b1a21f3187_11', include: 'organizations,sites')

Retrive a dataset and specify a subset of fields (title,url,description,thumbnail)

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_show('c92b122901ad40ee897d36b1a21f3187_11', fields: { datasets: 'title,url,description,thumbnail'})

Parameters:

  • id (String)

    The dataset id

  • params (Hash) (defaults to: {})

    Additional request parameters

Returns:

  • (Object)

    Faraday::Response object



62
63
64
65
# File 'lib/client.rb', line 62

def dataset_show(id, params = {})
  raise '#dataset_show must receive a dataset id' if id.nil?
  connection.get(dataset_show_url(id, params))
end

#dataset_show_url(id, params = {}) ⇒ String

Returns the url based on the id and url parameters specified

Examples:

Get the requets url for retrieving a single dataset and its related organizations and sites

client = Opendata.new('https://opendata.arcgis.com')
client.dataset_show_url('c92b122901ad40ee897d36b1a21f3187_11', include: 'organizations,sites')
#=> "/api/v2/datasets/c92b122901ad40ee897d36b1a21f3187_11?include=organizations%2Csites"

Parameters:

  • id (String)

    The dataset id

  • params (Hash) (defaults to: {})

    Additional request parameters

Returns:

  • (String)

    request url based on id and parameters specified



75
76
77
# File 'lib/client.rb', line 75

def dataset_show_url(id, params = {})
  DATASETS_API_PATH + "/#{id}" + param_to_query_string(params)
end