Module: PYR

Defined in:
lib/pyr.rb,
lib/pyr/param.rb,
lib/pyr/parser.rb,
lib/pyr/request.rb,
lib/pyr/version.rb,
lib/pyr/resource.rb,
lib/pyr/response.rb,
lib/pyr/geo_findable.rb,
lib/pyr/resources/reps.rb,
lib/pyr/resources/zctas.rb,
lib/pyr/response_object.rb,
lib/pyr/resources/states.rb,
lib/pyr/resources/districts.rb,
lib/pyr/response_objects/rep.rb,
lib/pyr/response_objects/zcta.rb,
lib/pyr/response_objects/state.rb,
lib/pyr/response_objects/district.rb,
lib/pyr/resources/office_locations.rb,
lib/pyr/response_objects/office_location.rb

Overview

PYR wraps the Phone Your Rep API in an easy interface, converting the data payloads into Ruby objects.

Examples

response = PYR.call(:reps) { |r| r.address = 'vermont' }

response.body # => { ... }
response.path # => 'reps?address=vermont'
reps = response.objects
reps.count # => 3
reps.independent.senators.first.official_full # => "Bernard Sanders"

response = PYR.call :reps

response.objects.count # => 536
independent_senators = response.objects.independent.senators
vt_fav_son = independent_senators.where { |r| r.state.abbr == 'VT' }.first
vt_fav_son.official_full # => "Bernard Sanders"
vt_fav_son.bioguide_id # => "S000033"

response = PYR.call :reps, 'S000033'

response.objects.first.official_full # => "Bernard Sanders"

PYR.reps('S000033').objects.first.official_full # => "Bernard Sanders"

Any of the following resources can be passed as the first argument to ‘PYR.call`, or called as a class method directly to the PYR module, as in the above example. Both options yield a resource to the block for setting params.

:reps, :office_locations, :states, :districts, :zctas

So

PYR.call(:reps) { |r| r.address = 'vermont' }

Is equivalent to

PYR.reps { |r| r.address = 'vermont' }

Defined Under Namespace

Modules: GeoFindable, Parser, Request Classes: District, OfficeLocation, Param, Rep, Resource, Response, ResponseObject, State, Zcta

Constant Summary collapse

API_BASE_URI =

The base URI for all requests

'https://phone-your-rep.herokuapp.com/api/beta/'
API_CONN =

Faraday API Connection

Faraday.new url: API_BASE_URI do |conn|
  conn.response :json, content_type: /\bjson$/
  conn.response :yaml, content_type: /\byaml$/
  conn.adapter Faraday.default_adapter
end
PYR_RESOURCES =

The API resources available to query

i[
  reps
  office_locations
  states
  districts
  zctas
].freeze
VERSION =

:nodoc:

'0.3.0'

Class Method Summary collapse

Class Method Details

.call(resource, id = nil, &config_block) ⇒ Object

The main API interface. All the objects you will need to work with are returned by this method.

Arguments can be:

a particular PYR::ResponseObject itself to query that object’s ‘self’ url;

a valid Phone Your Rep URI beginning with the API_BASE_URI string value;

or, perhaps most commonly, a resource name (String or Symbol) with optional ID(String).



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pyr.rb', line 124

def self.call(resource, id = nil, &config_block)
  if resource.is_a?(ResponseObject)
    request_object = { response_object: resource }
  elsif resource.to_s.include? API_BASE_URI
    request_object = { uri: resource }
  else
    resource = Request.build(resource, id)
    config_block.call resource if block_given?
    request_object = { resource: resource }
  end
  Response.new request_object
end

.districts(id = nil, &config_block) ⇒ Object

Call the :districts resource.

PYR.districts '5000'


168
169
170
# File 'lib/pyr.rb', line 168

def self.districts(id = nil, &config_block)
  call(:districts, id, &config_block)
end

.office_locations(id = nil, &config_block) ⇒ Object

Call the :office_locations resource.

PYR.office_locations { |r| r.address = '123 Main St, USA 12345' }


147
148
149
# File 'lib/pyr.rb', line 147

def self.office_locations(id=nil, &config_block)
  call(:office_locations, id, &config_block)
end

.reps(id = nil, &config_block) ⇒ Object

Call the :reps resource.

PYR.reps { |r| r.address = '123 Main St, USA 12345' }


140
141
142
# File 'lib/pyr.rb', line 140

def self.reps(id = nil, &config_block)
  call(:reps, id, &config_block)
end

.states(id = nil, &config_block) ⇒ Object

Call the :states resource.

PYR.states '50'


161
162
163
# File 'lib/pyr.rb', line 161

def self.states(id = nil, &config_block)
  call(:states, id, &config_block)
end

.zctas(id = nil, &config_block) ⇒ Object

Call the :zctas resource.

PYR.zctas('90026') { |r| r.reps = true }


154
155
156
# File 'lib/pyr.rb', line 154

def self.zctas(id = nil, &config_block)
  call(:zctas, id, &config_block)
end