Class: USA::Procurement

Inherits:
Base
  • Object
show all
Defined in:
lib/usaspending/procurement.rb

Constant Summary

Constants inherited from Base

Base::API_URL

Instance Method Summary collapse

Methods inherited from Base

#construct_url, #get_data, #hash2get, #set_instance_methods

Constructor Details

#initializeProcurement

Returns a new instance of Procurement.



5
6
7
8
9
# File 'lib/usaspending/procurement.rb', line 5

def initialize
  @database = 'fpds'
  @query = {}
  @errors = {}
end

Instance Method Details

#agency(code) ⇒ Object

The 4-digit code for a specific governmental agency issuing contracts. See here www.usaspending.gov/apidocsmore.html#FPDS_AGENCY.



90
91
92
93
94
95
96
97
98
# File 'lib/usaspending/procurement.rb', line 90

def agency(code)
  require '../lib/codes/fpds_agencies.rb'
  if agency_codes["#{code}"]
    @query[:mod_agency] = code  
  else
    @errors[:agency] = "Couldn't find that agency specific code."
  end
  self
end

#business_fund(fund_value) ⇒ Object

Determines the Business Fund Indicator: r = funds provided by Recovery Act



72
73
74
75
# File 'lib/usaspending/procurement.rb', line 72

def business_fund(fund_value)
  @query[:busn_indctr] = fund_value
  self
end

#city(city_value) ⇒ Object

city The city within a contractor’s address



21
22
23
24
# File 'lib/usaspending/procurement.rb', line 21

def city(city_value)
  @query[:city] = city_value unless city_value == ""
  self
end

#clearObject

Clears all the query filters to make a new search



254
255
256
257
258
# File 'lib/usaspending/procurement.rb', line 254

def clear
  @fetch = nil
  @query = {}
  self
end

#company_name(company_name_value = nil) ⇒ Object

Specify a company name



169
170
171
172
# File 'lib/usaspending/procurement.rb', line 169

def company_name(company_name_value = nil)
  @query[:company_name] = company_name_value unless company_name_value == ""
  self
end

#competition_status(cat_value) ⇒ Object

The competition status of a contract. Values are:

everyone or c = Available for everyone for competition
one offer or o = Everyone could compete, but only one bid or offer was recieved
pool  p = Competition within a limited pool
no  n = Not competed for an allowable reason
groups  a = Available only for groups such as disabled persons, prisoners, and regulated utilities
actions  f = Actions necessary to continue existing competitive contracts for continuity (until the next one could be competed)
unknown  u = Not identified, soon to be addressed

compete_cat



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/usaspending/procurement.rb', line 143

def competition_status(cat_value)
  @query[:complete_cat] = case cat_value
  when "everyone", "c" then "c"
  when "One offer", "o" then "o"
  when "pool", "p" then "p"
  when "no", "n" then "n"
  when "groups", "a" then "a"
  when "actions", "f" then "f"
  else "u"
  end
  self
end

#congress_district(congress_value, location_type = 'vendor') ⇒ Object

accepts abbreviated list of congressional districts and vendor or performance, defaults to vendor



38
39
40
41
42
43
44
45
46
# File 'lib/usaspending/procurement.rb', line 38

def congress_district(congress_value, location_type='vendor')
  require '../lib/codes/congress_districts.rb'
  if congress_districts["#{congress_value}"]
  location_type != 'vendor' ? @query[:pop_cd] = congress_value : @query[:vendor_cd] = congress_value  
  else
    @errors[:congress_district] = "Couldn't find that congressional district code."
  end
  self
end

#contract_requirement(description) ⇒ Object



53
54
55
56
# File 'lib/usaspending/procurement.rb', line 53

def contract_requirement(description)
  @query[:descriptionOfContractRequirement] = description
  self
end

#country(country_abbr_value, location_type = 'vendor') ⇒ Object

accepts abbreviate country letters and vendor or performance for place of performance, defaults to vendor



27
28
29
30
31
32
33
34
35
# File 'lib/usaspending/procurement.rb', line 27

def country(country_abbr_value, location_type='vendor')
  require "../lib/codes/countries.rb"
  if country_codes["#{country_abbr_value}"] 
  location_type != 'vendor' ? @query[:placeOfPerformanceCountryCode] = country_abbr_value : @query[:vendorCountryCode] = country_abbr_value  
  else
    @errors[:country] = "Couldn't find the country code."
  end
  self
end

#databaseObject

returns current database



12
13
14
# File 'lib/usaspending/procurement.rb', line 12

def database
  @database
end

#detail(detail_number) ⇒ Object

accepts values b or c, defaults to b



238
239
240
241
242
243
244
245
246
# File 'lib/usaspending/procurement.rb', line 238

def detail(detail_number)
  if ["b", "c"].include?(detail_number.downcase)
    detail_value = detail_number
  else
    detail_value = "b"
  end  
  @query[:detail] = detail_value
  self
end

#duns_number(duns_value) ⇒ Object

he contractor duns number.



59
60
61
62
# File 'lib/usaspending/procurement.rb', line 59

def duns_number(duns_value)
  @query[:duns_number] = duns_value
  self
end

#errorsObject



16
17
18
# File 'lib/usaspending/procurement.rb', line 16

def errors
  @errors
end

#fetchObject

Fetch the Procurement Search returning parsed xml



265
266
267
# File 'lib/usaspending/procurement.rb', line 265

def fetch
  get_data(url)
end

#fetch_contractsObject

Fetch the Procurement Search returning array of contract objects



270
271
272
273
274
275
276
# File 'lib/usaspending/procurement.rb', line 270

def fetch_contracts
  contracts = []
  self.fetch["result"].first.first[1].each do |contract|
    contracts << USA::Contract.new(contract)
  end
  return contracts
end

#major_agency(code) ⇒ Object

The 2-character code for a major governmental agency issuing contracts. See here www.usaspending.gov/apidocsmore.html#FPDS_MAJAGENCY for list.



101
102
103
104
105
106
107
108
109
# File 'lib/usaspending/procurement.rb', line 101

def major_agency(code)
  require '../lib/codes/fpds_agencies.rb'
  if major_codes["#{code}"]
    @query[:maj_agency_cat] = code  
  else
    @errors[:major_agency] = "Couldn't find that major agency category code."
  end
  self
end

#max_records(max_number) ⇒ Object

Allows you to set the maximum number of records retrieved to fewer than 1000 defaults to 1000 when over a 1000



225
226
227
228
# File 'lib/usaspending/procurement.rb', line 225

def max_records(max_number)
  @query[:max_records] = max_number.to_i < 1000 ? max_number : 1000
  self
end

#piid(piid_value) ⇒ Object



48
49
50
51
# File 'lib/usaspending/procurement.rb', line 48

def piid(piid_value)
  @query[:PIID] = piid_value
  self
end

#product(code) ⇒ Object

The 4-character code for a product or service. See here www.usaspending.gov/apidocsmore.html#PSC for list.



123
124
125
126
127
128
129
130
131
# File 'lib/usaspending/procurement.rb', line 123

def product(code)
  require '../lib/codes/products_services.rb'
  if product_codes["#{code}"]
    @query[:psc_sub] = code  
  else
    @errors[:product] = "Couldn't find that product code."
  end
  self
end

#product_category(code) ⇒ Object

The 2-character code for a major product or service category. See here www.usaspending.gov/apidocsmore.html#PSCCAT for list.



112
113
114
115
116
117
118
119
120
# File 'lib/usaspending/procurement.rb', line 112

def product_category(code)
  require '../lib/codes/products_services.rb'
  if product_category_codes["#{code}"]
    @query[:psc_cat] = code  
  else
    @errors[:product_category] = "Couldn't find that major product category code."
  end
  self
end

#program_account(account_value) ⇒ Object

The program source account code.



84
85
86
87
# File 'lib/usaspending/procurement.rb', line 84

def ()
  @query[:program_source_account_code] = 
  self
end

#program_code(code_value) ⇒ Object

The program source agency code.



78
79
80
81
# File 'lib/usaspending/procurement.rb', line 78

def program_code(code_value)
  @query[:program_source_agency_code] = code_value
  self
end

#program_description(description) ⇒ Object

Full text search of program source description.



65
66
67
68
# File 'lib/usaspending/procurement.rb', line 65

def program_description(description)
  @query[:program_source_desc] = description
  self
end

#queryObject

return current query



249
250
251
# File 'lib/usaspending/procurement.rb', line 249

def query
  @query
end

#record(record_number) ⇒ Object



230
231
232
233
234
235
# File 'lib/usaspending/procurement.rb', line 230

def record(record_number)
  #set the detail to c
  self.detail("c")
  @query[:record_id] = record_number
  self
end

#sort_by(sortby_number) ⇒ Object

Determines how records are sorted. Valid values are: name or r = by contractor or recipient name dollars or f = by dollars of awards (in descending order) major or g = by major contracting agency category or p = by Product or Service Category date or d = by date of award Defaults to sort by dollars if not provided.



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/usaspending/procurement.rb', line 181

def sort_by(sortby_number)
  @query[:sortby] = case sortby_number 
  when "name", "r" then "r"
  when "dollars", "f" then "f"
  when "major agency", "g" then "g"
  when "category", "p" then "p"
  when "date", "d" then "d"
  else "f"
  end
  self
end

#start_from(start_number) ⇒ Object

Allows you to set the starting position of the records to be retrieved, defaults to 1 if less than 0



218
219
220
221
# File 'lib/usaspending/procurement.rb', line 218

def start_from(start_number)
  @query[:records_from] = start_number > 0 ? start_number : 1
  self
end

#state(state_value, location_type = 'vendor') ⇒ Object

state The state abbreviation within a contractor’s address.



157
158
159
160
# File 'lib/usaspending/procurement.rb', line 157

def state(state_value, location_type='vendor')
  location_type != 'vendor' ? @query[:stateCode] = state_value : @query[:state] = state_value  
  self
end

#urlObject



260
261
262
# File 'lib/usaspending/procurement.rb', line 260

def url
  construct_url(database, @query)
end

#years(s_year, end_year = nil) ⇒ Object

specify time frame in years. Requires start value if specified, otherwise, leaving this function will default to all fiscal years entering a wrong year will default to the current year



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/usaspending/procurement.rb', line 195

def years(s_year, end_year = nil )
  start_year = s_year.to_i
  end_year = end_year.to_i unless end_year.nil?
  if end_year
    if start_year < 2000 || start_year > Time.now.year.to_i + 1
      @errors[:years] = "Problem with Start Year"
    else  
      if end_year > start_year && end_year < Time.now.year.to_i + 1
        @query[:first_year_range] = start_year
        @query[:last_year_range] = end_year
      else
        @errors[:years] = "Problem with End Year"
      end        
    end
  elsif start_year < 2000 or start_year > Time.now.year.to_i + 1
    @errors[:years] = "Problem with Start Year"
  else
    @query[:fiscal_year] = start_year  
  end
  self
end

#zip(zip_value, location_type = 'vendor') ⇒ Object

ZIPCode The ZIP code within a contractor’s address.



163
164
165
166
# File 'lib/usaspending/procurement.rb', line 163

def zip(zip_value, location_type='vendor')
  location_type != 'vendor' ? @query[:placeOfPerformanceZIPCode] = zip_value : @query[:ZIPCode] = zip_value  
  self
end