sba 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gitignore +41 -0
  2. data/.rspec +3 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.md +10 -0
  5. data/LICENSE.mkd +20 -0
  6. data/README.mkd +72 -0
  7. data/Rakefile +16 -0
  8. data/autotest/discover.rb +1 -0
  9. data/lib/faraday/multipart.rb +29 -0
  10. data/lib/faraday/raise_error.rb +31 -0
  11. data/lib/faraday/response/raise_error.rb +32 -0
  12. data/lib/sba.rb +51 -0
  13. data/lib/sba/client.rb +44 -0
  14. data/lib/sba/client/connection.rb +37 -0
  15. data/lib/sba/client/data.rb +73 -0
  16. data/lib/sba/client/licenses.rb +111 -0
  17. data/lib/sba/client/loans.rb +125 -0
  18. data/lib/sba/client/request.rb +37 -0
  19. data/lib/sba/client/sites.rb +71 -0
  20. data/lib/sba/client/urls.rb +134 -0
  21. data/lib/sba/configuration.rb +44 -0
  22. data/lib/sba/version.rb +3 -0
  23. data/sba.gemspec +37 -0
  24. data/spec/data_spec.rb +103 -0
  25. data/spec/fixtures/all_sites_keywords.json +1393 -0
  26. data/spec/fixtures/business_type_state.json +81 -0
  27. data/spec/fixtures/business_type_state_city.json +131 -0
  28. data/spec/fixtures/business_type_state_county.json +101 -0
  29. data/spec/fixtures/business_type_zip.json +111 -0
  30. data/spec/fixtures/by_business_type.json +601 -0
  31. data/spec/fixtures/by_state.json +71 -0
  32. data/spec/fixtures/city_links_for_state.json +30 -0
  33. data/spec/fixtures/data_city.json +87543 -0
  34. data/spec/fixtures/data_city_county.json +88355 -0
  35. data/spec/fixtures/data_county.json +813 -0
  36. data/spec/fixtures/data_specific_city.json +15 -0
  37. data/spec/fixtures/data_specific_county.json +2493 -0
  38. data/spec/fixtures/doing_business_as.json +591 -0
  39. data/spec/fixtures/employer_requirements.json +2211 -0
  40. data/spec/fixtures/entity_filing.json +551 -0
  41. data/spec/fixtures/federal_and_state_financing_for.json +973 -0
  42. data/spec/fixtures/loan_grants.json +901 -0
  43. data/spec/fixtures/loan_grants_by_industry_specialty.json +533 -0
  44. data/spec/fixtures/loan_grants_by_specialty.json +210 -0
  45. data/spec/fixtures/loan_grants_industry.json +324 -0
  46. data/spec/fixtures/loan_grants_state_ind_spec.json +381 -0
  47. data/spec/fixtures/loan_grants_state_industry.json +381 -0
  48. data/spec/fixtures/loan_grants_state_specialty.json +362 -0
  49. data/spec/fixtures/primary_url_for_athens.json +15 -0
  50. data/spec/fixtures/primary_url_for_clarke_county.json +29 -0
  51. data/spec/fixtures/rec_sites_categories.json +249 -0
  52. data/spec/fixtures/rec_sites_categories.xml +1 -0
  53. data/spec/fixtures/rec_sites_domain.json +9 -0
  54. data/spec/fixtures/rec_sites_domain.xml +1 -0
  55. data/spec/fixtures/rec_sites_keyword.json +17 -0
  56. data/spec/fixtures/rec_sites_keywords.xml +1 -0
  57. data/spec/fixtures/rec_sites_master_term.json +9 -0
  58. data/spec/fixtures/rec_sites_master_term.xml +1 -0
  59. data/spec/fixtures/state_financing_for.json +73 -0
  60. data/spec/fixtures/state_licenses.json +11191 -0
  61. data/spec/fixtures/tax_registration.json +1101 -0
  62. data/spec/fixtures/url_city.json +6063 -0
  63. data/spec/fixtures/url_city_county.json +6875 -0
  64. data/spec/fixtures/url_county.json +813 -0
  65. data/spec/fixtures/url_specific_city.json +141 -0
  66. data/spec/fixtures/url_specific_county.json +477 -0
  67. data/spec/helper.rb +27 -0
  68. data/spec/licenses_spec.rb +148 -0
  69. data/spec/loans_spec.rb +193 -0
  70. data/spec/sba_ruby/client_spec.rb +8 -0
  71. data/spec/sba_spec.rb +9 -0
  72. data/spec/sites_spec.rb +101 -0
  73. data/spec/urls_spec.rb +188 -0
  74. metadata +345 -0
@@ -0,0 +1,41 @@
1
+ !.gitignore
2
+ *.gem
3
+ *.rbc
4
+ *.sw[a-p]
5
+ *.tmproj
6
+ *.tmproject
7
+ *.un~
8
+ *~
9
+ .DS_Store
10
+ .Spotlight-V100
11
+ .Trashes
12
+ ._*
13
+ .bundle
14
+ .config
15
+ .directory
16
+ .elc
17
+ .redcar
18
+ .yardoc
19
+ /.emacs.desktop
20
+ /.emacs.desktop.lock
21
+ Desktop.ini
22
+ Gemfile.lock
23
+ Icon?
24
+ InstalledFiles
25
+ Session.vim
26
+ Thumbs.db
27
+ \#*\#
28
+ _yardoc
29
+ auto-save-list
30
+ coverage
31
+ doc/
32
+ lib/bundler/man
33
+ pkg
34
+ pkg/*
35
+ rdoc
36
+ spec/reports
37
+ test/tmp
38
+ test/version_tmp
39
+ tmp
40
+ tmtags
41
+ tramp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=nested
3
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gem_template.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2011, Code for America
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of Code for America nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Code for America Laboratories
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ SBA API
2
+ =======
3
+ A Ruby wrapper for the SBA APIs at http://www.sba.gov/api.
4
+
5
+ Does your project or organization use this gem?
6
+ ------------------------------------------
7
+ Add it to the [apps](http://github.com/codeforamerica/sba/wiki/apps) wiki!
8
+
9
+ Installation
10
+ ------------
11
+ $ [sudo] gem install sba
12
+
13
+ Documentation
14
+ -------------
15
+ [http://rdoc.info/gems/sba_ruby](http://rdoc.info/gems/sba_ruby)
16
+
17
+ Continuous Integration
18
+ ----------------------
19
+ [![Build Status](http://travis-ci.org/codeforamerica/sba_ruby.png)](http://travis-ci.org/codeforamerica/sba_ruby)
20
+
21
+ Usage Examples
22
+ --------------
23
+ require 'sba'
24
+
25
+ @client = SBA::Client::New
26
+ a = @client.all_primary_urls_in_state("ca")
27
+ a[0]["name"] # => "Adelanto"
28
+
29
+
30
+ Contributing
31
+ ------------
32
+ In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
33
+
34
+ Here are some ways *you* can contribute:
35
+
36
+ * by using alpha, beta, and prerelease versions
37
+ * by reporting bugs
38
+ * by suggesting new features
39
+ * by writing or editing documentation
40
+ * by writing specifications
41
+ * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
42
+ * by refactoring code
43
+ * by resolving [issues](http://github.com/cfalabs/sba/issues)
44
+ * by reviewing patches
45
+
46
+ Submitting an Issue
47
+ -------------------
48
+ We use the [GitHub issue tracker](http://github.com/codeforamerica/sba/issues) to track bugs and
49
+ features. Before submitting a bug report or feature request, check to make sure it hasn't already
50
+ been submitted. You can indicate support for an existing issuse by voting it up. When submitting a
51
+ bug report, please include a [Gist](http://gist.github.com/) that includes a stack trace and any
52
+ details that may be necessary to reproduce the bug, including your gem version, Ruby version, and
53
+ operating system. Ideally, a bug report should include a pull request with failing specs.
54
+
55
+ Submitting a Pull Request
56
+ -------------------------
57
+ 1. Fork the project.
58
+ 2. Create a topic branch.
59
+ 3. Implement your feature or bug fix.
60
+ 4. Add documentation for your feature or bug fix.
61
+ 5. Run <tt>bundle exec rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
62
+ 6. Add specs for your feature or bug fix.
63
+ 7. Run <tt>bundle exec rake spec</tt>. If your changes are not 100% covered, go back to step 6.
64
+ 8. Commit and push your changes.
65
+ 9. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)
66
+
67
+ Copyright
68
+ ---------
69
+ Copyright (c) 2010 Code for America Laboratories
70
+ See [LICENSE](https://github.com/cfalabs/sba/blob/master/LICENSE.mkd) for details.
71
+
72
+ [![Code for America Tracker](http://stats.codeforamerica.org/codeforamerica/sba_ruby.png)](http://stats.codeforamerica.org/projects/sba_ruby)
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+ task :test => :spec
9
+
10
+ require 'yard'
11
+ namespace :doc do
12
+ YARD::Rake::YardocTask.new do |task|
13
+ task.files = ['LICENSE.md', 'lib/**/*.rb']
14
+ task.options = ['--markup', 'markdown']
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,29 @@
1
+ require 'faraday'
2
+
3
+ # @api private
4
+ module Faraday
5
+ class Request::Multipart < Faraday::Middleware
6
+ def call(env)
7
+ if env[:body].is_a?(Hash)
8
+ env[:body].each do |key, value|
9
+ if value.is_a?(File)
10
+ env[:body][key] = Faraday::UploadIO.new(value, mime_type(value), value.path)
11
+ end
12
+ end
13
+ end
14
+
15
+ @app.call(env)
16
+ end
17
+
18
+ private
19
+
20
+ def mime_type(file)
21
+ case file.path
22
+ when /\.jpe?g/i then 'image/jpeg'
23
+ when /\.gif$/i then 'image/gif'
24
+ when /\.png$/i then 'image/png'
25
+ else 'application/octet-stream'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+
3
+ # @api private
4
+ module Faraday
5
+ class Response::RaiseError < Response::Middleware
6
+ def on_complete(response)
7
+ case response[:status].to_i
8
+ when 400
9
+ raise SBA::BadRequest, error_message(response)
10
+ when 401
11
+ raise SBA::Unauthorized, error_message(response)
12
+ when 403
13
+ raise SBA::Forbidden, error_message(response)
14
+ when 404
15
+ raise SBA::NotFound, error_message(response)
16
+ when 406
17
+ raise SBA::NotAcceptable, error_message(response)
18
+ when 500
19
+ raise SBA::InternalServerError, error_message(response)
20
+ when 502
21
+ raise SBA::BadGateway, error_message(response)
22
+ when 503
23
+ raise SBA::ServiceUnavailable, error_message(response)
24
+ end
25
+ end
26
+
27
+ def error_message(response)
28
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ require 'faraday'
2
+
3
+ # @api private
4
+ module Faraday
5
+ class Response::RaiseError < Response::Middleware
6
+ def on_complete(response)
7
+ case response[:status].to_i
8
+ when 400
9
+ raise SBA::BadRequest, error_message(response)
10
+ when 401
11
+ raise SBA::Unauthorized, error_message(response)
12
+ when 403
13
+ raise SBA::Forbidden, error_message(response)
14
+ when 404
15
+ raise SBA::NotFound, error_message(response)
16
+ when 406
17
+ raise SBA::NotAcceptable, error_message(response)
18
+ when 500
19
+ raise SBA::InternalServerError, error_message(response)
20
+ when 502
21
+ raise SBA::BadGateway, error_message(response)
22
+ when 503
23
+ raise SBA::ServiceUnavailable, error_message(response)
24
+ end
25
+ end
26
+
27
+ def error_message(response)
28
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,51 @@
1
+ require ::File.expand_path('../sba/version', __FILE__)
2
+ require ::File.expand_path('../sba/configuration.rb', __FILE__)
3
+ require ::File.expand_path('../sba/client', __FILE__)
4
+
5
+ module SBA
6
+ extend Configuration
7
+
8
+ # Alias for SBA::Client.new
9
+ #
10
+ # @return [SBA::Client]
11
+ def self.client(options={})
12
+ SBA::Client.new(options)
13
+ end
14
+
15
+ # Delegate to SBA::Client
16
+ def self.method_missing(method, *args, &block)
17
+ return super unless client.respond_to?(method)
18
+ client.send(method, *args, &block)
19
+ end
20
+
21
+ def self.respond_to?(method, include_private=false)
22
+ client.respond_to?(method, include_private) || super(method, include_private)
23
+ end
24
+
25
+ # Custom error class for rescuing from all SBA errors
26
+ class Error < StandardError; end
27
+
28
+ # Raised when SBA returns a 400 HTTP status code
29
+ class BadRequest < Error; end
30
+
31
+ # Raised when SBA returns a 401 HTTP status code
32
+ class Unauthorized < Error; end
33
+
34
+ # Raised when SBA returns a 403 HTTP status code
35
+ class Forbidden < Error; end
36
+
37
+ # Raised when SBA returns a 404 HTTP status code
38
+ class NotFound < Error; end
39
+
40
+ # Raised when SBA returns a 406 HTTP status code
41
+ class NotAcceptable < Error; end
42
+
43
+ # Raised when SBA returns a 500 HTTP status code
44
+ class InternalServerError < Error; end
45
+
46
+ # Raised when SBA returns a 502 HTTP status code
47
+ class BadGateway < Error; end
48
+
49
+ # Raised when SBA returns a 503 HTTP status code
50
+ class ServiceUnavailable < Error; end
51
+ end
@@ -0,0 +1,44 @@
1
+ require ::File.expand_path('../client/connection', __FILE__)
2
+ require ::File.expand_path('../client/request', __FILE__)
3
+ require ::File.expand_path('../client/data.rb', __FILE__)
4
+ require ::File.expand_path('../client/loans.rb', __FILE__)
5
+ require ::File.expand_path('../client/licenses.rb', __FILE__)
6
+ require ::File.expand_path('../client/sites.rb', __FILE__)
7
+ require ::File.expand_path('../client/urls.rb', __FILE__)
8
+
9
+ module SBA
10
+ class Client
11
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
12
+
13
+ def initialize(options={})
14
+ options = SBA.options.merge(options)
15
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
16
+ send("#{key}=", options[key])
17
+ end
18
+ end
19
+
20
+ include SBA::Client::Connection
21
+ include SBA::Client::Request
22
+ include SBA::Client::Data
23
+ include SBA::Client::Urls
24
+ include SBA::Client::Loans
25
+ include SBA::Client::Licenses
26
+ include SBA::Client::Sites
27
+
28
+ private
29
+ #some JSON replies we get back are in an ugly format. simplify them
30
+ def simplify_response(response)
31
+ #the returned 'key' is pretty useless. the key=>value thing here is basically an array
32
+ #within that, we're given an array of key=>value pairs. we want this to be one big hash.
33
+ Hash[*response.group_by {|key,val| key.match(/(.*)_item/)[1]}.map do |section,items|
34
+ [section,
35
+ items.inject([]) do |array,(key,value)|
36
+ i = key.match(/\d+$/)[0].to_i
37
+ array[i] = value.inject({}) {|all,current| all.merge! current}
38
+ array
39
+ end]
40
+ end.flatten(1)]
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,37 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../../faraday/*.rb', __FILE__)].each{|file| require file}
3
+
4
+ module SBA
5
+ class Client
6
+ # @private
7
+ module Connection
8
+ private
9
+
10
+ def connection(raw=false)
11
+ options = {
12
+ #:headers => {'Accept' => "*/#{format}", 'User-Agent' => user_agent},
13
+ :headers => {'Accept' => 'application/json'},
14
+ :proxy => proxy,
15
+ :ssl => {:verify => false},
16
+ :url => 'http://api.sba.gov/' + endpoint.to_s
17
+ }
18
+
19
+ Faraday::Connection.new(options) do |connection|
20
+ connection.use Faraday::Request::Multipart
21
+ unless raw
22
+ connection.use Faraday::Response::Mashify
23
+ case format.to_s.downcase
24
+ when 'json'
25
+ connection.use Faraday::Response::ParseJson
26
+ when 'xml'
27
+ connection.use Faraday::Response::ParseXml
28
+ end
29
+ end
30
+ connection.use Faraday::Response::RaiseError
31
+ connection.adapter(adapter)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,73 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'json'
3
+
4
+ module SBA
5
+ class Client
6
+ module Data
7
+ include SBA::Client::Connection
8
+
9
+ # Returns Data for all Cities and Counties in a State
10
+ #
11
+ # @param state [String] Input the two leter postal code for state abbreviation.
12
+ # @param options [Hash] A customizable set of options.
13
+ # @return {Array}
14
+ # @see http://www.sba.gov/content/us-city-and-county-web-data-api-city-county-data-all-data-methods
15
+ # @example
16
+ # SBA.data_city_county("ca")
17
+ def data_city_county(state,options={})
18
+ response = get("geodata/city_county_data_for_state_of/#{state}.json", options)
19
+ end
20
+
21
+ # Returns Data for all Cities in a State
22
+ #
23
+ # @param state [String] Input the two leter postal code for state abbreviation.
24
+ # @param options [Hash] A customizable set of options.
25
+ # @return {Array}
26
+ # @see http://www.sba.gov/content/us-city-and-county-web-data-api-city-county-data-all-data-methods
27
+ # @example
28
+ # SBA.data_city("ca")
29
+ def data_city(state, options={})
30
+ response = get("geodata/city_data_for_state_of/#{state}.json", options)
31
+ end
32
+
33
+ # Returns Data for all Counties in a State
34
+ #
35
+ # @param state [String] Input the two leter postal code for state abbreviation.
36
+ # @param options [Hash] A customizable set of options.
37
+ # @return {Array}
38
+ # @see http://www.sba.gov/content/us-city-and-county-web-data-api-city-county-data-all-data-methods
39
+ # @example
40
+ # SBA.data_county("ca")
41
+ def data_county(state, options={})
42
+ response = get("geodata/county_data_for_state_of/#{state}.json", options)
43
+ end
44
+
45
+ # Returns Data for a specific City
46
+ #
47
+ # @param city [String] Input the name of the city
48
+ # @param state [String] Input the two leter postal code for state abbreviation.
49
+ # @param options [Hash] A customizable set of options.
50
+ # @return {Array}
51
+ # @see http://www.sba.gov/content/us-city-and-county-web-data-api-city-county-data-all-data-methods
52
+ # @example
53
+ # SBA.data_specific_city("ca")
54
+ def data_specific_city(city, state, options={})
55
+ response = get("geodata/all_data_for_city_of/#{city}/#{state}.json", options)
56
+ end
57
+
58
+ # Returns Data for a specific County
59
+ #
60
+ # @param county [String] Input the name of the County
61
+ # @param state [String] Input the two leter postal code for state abbreviation.
62
+ # @param options [Hash] A customizable set of options.
63
+ # @return {Array}
64
+ # @see http://www.sba.gov/content/us-city-and-county-web-data-api-city-county-data-all-data-methods
65
+ # @example
66
+ # SBA.data_specific_county("orange county", "ca")
67
+ def data_specific_county(county, state, options={})
68
+ response = get("geodata/all_data_for_county_of/#{county}/#{state}.json", options)
69
+ end
70
+
71
+ end
72
+ end
73
+ end