Class: Easy311::ApiWrapper
- Inherits:
-
Object
- Object
- Easy311::ApiWrapper
- Defined in:
- lib/easy311/api_wrapper.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #all_services ⇒ Object
- #attrs_from_code(code) ⇒ Object
- #build_params ⇒ Object
- #group_names ⇒ Object
- #groups ⇒ Object
-
#initialize(resource, api_key, jurisdiction_id = nil) ⇒ ApiWrapper
constructor
A new instance of ApiWrapper.
- #log ⇒ Object
- #parse_error_response(body) ⇒ Object
- #parse_post_response(body) ⇒ Object
- #query_service(path) ⇒ Object
- #send_request(request) ⇒ Object
- #services_with_attrs ⇒ Object
Constructor Details
#initialize(resource, api_key, jurisdiction_id = nil) ⇒ ApiWrapper
Returns a new instance of ApiWrapper.
18 19 20 21 22 |
# File 'lib/easy311/api_wrapper.rb', line 18 def initialize(resource, api_key, jurisdiction_id=nil) @resource = resource @api_key = api_key @jurisdiction_id = jurisdiction_id end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/easy311/api_wrapper.rb', line 11 def logger @logger end |
Class Method Details
.from_url(url, api_key, jurisdiction_id = nil) ⇒ Object
13 14 15 16 |
# File 'lib/easy311/api_wrapper.rb', line 13 def self.from_url(url, api_key, jurisdiction_id=nil) resource = RestClient::Resource.new(url) return self.new(resource, api_key, jurisdiction_id) end |
Instance Method Details
#all_services ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/easy311/api_wrapper.rb', line 28 def all_services log.info "querying list of services" response = query_service('/services.json') return [] if response.empty? raw_services = JSON.parse(response) services = raw_services.map { |r| Service.new(r) } return services end |
#attrs_from_code(code) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/easy311/api_wrapper.rb', line 47 def attrs_from_code(code) log.info "querying attributes for service #{code}" url = "/services/#{code}.json" response = query_service(url) return [] if response.empty? json_data = JSON.parse(response) raw_attrs = json_data['attributes'] return [] if raw_attrs.empty? attrs = raw_attrs.map { |a| Attribute.new(a) } return attrs end |
#build_params ⇒ Object
79 80 81 82 83 |
# File 'lib/easy311/api_wrapper.rb', line 79 def build_params params = {} params[:jurisdiction_id] = @jurisdiction_id unless @jurisdiction_id.nil? return params end |
#group_names ⇒ Object
39 40 41 |
# File 'lib/easy311/api_wrapper.rb', line 39 def group_names all_services.map { |s| s.group }.uniq end |
#groups ⇒ Object
43 44 45 |
# File 'lib/easy311/api_wrapper.rb', line 43 def groups all_services.group_by { |s| s.group } end |
#log ⇒ Object
24 25 26 |
# File 'lib/easy311/api_wrapper.rb', line 24 def log @logger ||= Easy311.logger end |
#parse_error_response(body) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/easy311/api_wrapper.rb', line 110 def parse_error_response(body) json_response = JSON.parse(body) response = Response.new response. = json_response.map { |r| r['description'] }.join(",") return response end |
#parse_post_response(body) ⇒ Object
105 106 107 108 |
# File 'lib/easy311/api_wrapper.rb', line 105 def parse_post_response(body) json_response = JSON.parse(body) return Response.new(json_response.first) end |
#query_service(path) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/easy311/api_wrapper.rb', line 71 def query_service(path) params = build_params log.debug "GET #{path}" response = @resource[path].get(params) log.debug "RESPONSE #{response}" return response.strip end |
#send_request(request) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/easy311/api_wrapper.rb', line 85 def send_request(request) log.info "sending request #{request}" params = request.to_post_params params['api_key'] = @api_key log.debug "params #{params}" response = @resource['/requests.json'].post(params) do |http_response| log.debug "RESPONSE #{http_response.code} #{http_response.body}" if http_response.code.to_i >= 400 parse_error_response(http_response.body) else parse_post_response(http_response.body) end end log.info "response #{response}" return response end |
#services_with_attrs ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/easy311/api_wrapper.rb', line 62 def services_with_attrs log.info "querying all services and attributes" services = all_services.map do |service| service.attrs = attrs_from_code(service.code) service end return services end |