aqua-io 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48d31b07dfa68e868a37588213d209e5b12003ad
4
+ data.tar.gz: c498be93026a69d7afe2e0dd802dec9a682b4cd4
5
+ SHA512:
6
+ metadata.gz: 132d4336e133162824d69720dc65f29bfe4f5b74dbf55ff543849c56eb705733a58816cf40bd763b459edc44ee0ae6e2d439b58559fd037aa462708006b5429a
7
+ data.tar.gz: 0c595d99ad1866248dea7b656ceb433cb1a0b3101a28a55382fa9cf5dc4127bcf66b9656522a10cd35ed03c8f9799ad53bce5868ec286173501c5de73be7e25a
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+
3
+ require "aqua_io/client"
4
+ require "aqua_io/error"
5
+ require "aqua_io/http_client"
6
+
7
+ module AquaIo
8
+ end
@@ -0,0 +1,25 @@
1
+ module AquaIo
2
+
3
+ module Api
4
+
5
+ # Retrieve access token using API credentials.
6
+ class AccessToken
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Returns an access token (required for making all other API calls).
13
+ #
14
+ # 'oauth/token?grant_type=client_credentials' POST
15
+ def retrieve(options = {})
16
+ body = options.fetch(:body, {})
17
+
18
+ @client.post("oauth/token?grant_type=client_credentials", body, options)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,73 @@
1
+ module AquaIo
2
+
3
+ module Api
4
+
5
+ # Returns an ICD-10 code.
6
+ class Icd10
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Returns all top-level ICD-10 codes. Useful for helping a user navigate down the ICD-10 hierarchy to find a code.
13
+ #
14
+ # 'codes/v1/icd10' GET
15
+ def top_level_codes(options = {})
16
+ body = options.fetch(:query, {})
17
+
18
+ @client.get("codes/v1/icd10", body, options)
19
+ end
20
+
21
+ # Returns a single code matching the name, if any exists. Replace periods with hypens (e.g. 'R50-9' for 'R50.9')
22
+ #
23
+ # 'codes/v1/icd10/:code_name' GET
24
+ #
25
+ # code_name - name of code
26
+ def single_code(code_name, options = {})
27
+ body = options.fetch(:query, {})
28
+ body[:code_name] = code_name
29
+
30
+ @client.get("codes/v1/icd10/#{code_name}", body, options)
31
+ end
32
+
33
+ # Returns all codes whose name contains the search string.
34
+ #
35
+ # 'codes/v1/icd10?q[name_cont]=:query' GET
36
+ #
37
+ # query - the search query string
38
+ def search_by_name(query, options = {})
39
+ body = options.fetch(:query, {})
40
+ body[:query] = query
41
+
42
+ @client.get("codes/v1/icd10?q[name_cont]=#{query}", body, options)
43
+ end
44
+
45
+ # Returns all codes whose description contains the search string.
46
+ #
47
+ # 'codes/v1/icd10?q[description_cont]=:query' GET
48
+ #
49
+ # query - the search query string
50
+ def search_by_description(query, options = {})
51
+ body = options.fetch(:query, {})
52
+ body[:query] = query
53
+
54
+ @client.get("codes/v1/icd10?q[description_cont]=#{query}", body, options)
55
+ end
56
+
57
+ # Returns all codes whose name or description contains the search string.
58
+ #
59
+ # 'codes/v1/icd10?q[name_or_description_cont]=:query' GET
60
+ #
61
+ # query - the search query string
62
+ def search_by_name_or_description(query, options = {})
63
+ body = options.fetch(:query, {})
64
+ body[:query] = query
65
+
66
+ @client.get("codes/v1/icd10?q[name_or_description_cont]=#{query}", body, options)
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,73 @@
1
+ module AquaIo
2
+
3
+ module Api
4
+
5
+ # Returns an ICD-9 code.
6
+ class Icd9
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Returns all top-level ICD-9 codes. Useful for helping a user navigate down the ICD-9 hierarchy to find a code.
13
+ #
14
+ # 'codes/v1/icd9' GET
15
+ def top_level_codes(options = {})
16
+ body = options.fetch(:query, {})
17
+
18
+ @client.get("codes/v1/icd9", body, options)
19
+ end
20
+
21
+ # Returns a single code matching the name, if any exists. Replace periods with hypens (e.g. '066-4' for '066.4')
22
+ #
23
+ # 'codes/v1/icd9/:code_name' GET
24
+ #
25
+ # code_name - name of code
26
+ def single_code(code_name, options = {})
27
+ body = options.fetch(:query, {})
28
+ body[:code_name] = code_name
29
+
30
+ @client.get("codes/v1/icd9/#{code_name}", body, options)
31
+ end
32
+
33
+ # Returns all codes whose name contains the search string.
34
+ #
35
+ # 'codes/v1/icd9?q[name_cont]=:query' GET
36
+ #
37
+ # query - the search query string
38
+ def search_by_name(query, options = {})
39
+ body = options.fetch(:query, {})
40
+ body[:query] = query
41
+
42
+ @client.get("codes/v1/icd9?q[name_cont]=#{query}", body, options)
43
+ end
44
+
45
+ # Returns all codes whose description contains the search string.
46
+ #
47
+ # 'codes/v1/icd9?q[description_cont]=:query' GET
48
+ #
49
+ # query - the search query string
50
+ def search_by_description(query, options = {})
51
+ body = options.fetch(:query, {})
52
+ body[:query] = query
53
+
54
+ @client.get("codes/v1/icd9?q[description_cont]=#{query}", body, options)
55
+ end
56
+
57
+ # Returns all codes whose name or description contains the search string.
58
+ #
59
+ # 'codes/v1/icd9?q[name_or_description_cont]=:query' GET
60
+ #
61
+ # query - the search query string
62
+ def search_by_name_or_description(query, options = {})
63
+ body = options.fetch(:query, {})
64
+ body[:query] = query
65
+
66
+ @client.get("codes/v1/icd9?q[name_or_description_cont]=#{query}", body, options)
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,33 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "aqua_io/api/access_token"
5
+ require "aqua_io/api/icd9"
6
+ require "aqua_io/api/icd10"
7
+
8
+ module AquaIo
9
+
10
+ class Client
11
+
12
+ def initialize(auth = {}, options = {})
13
+ @http_client = AquaIo::HttpClient::HttpClient.new(auth, options)
14
+ end
15
+
16
+ # Retrieve access token using API credentials.
17
+ def access_token()
18
+ AquaIo::Api::AccessToken.new(@http_client)
19
+ end
20
+
21
+ # Returns an ICD-9 code.
22
+ def icd9()
23
+ AquaIo::Api::Icd9.new(@http_client)
24
+ end
25
+
26
+ # Returns an ICD-10 code.
27
+ def icd10()
28
+ AquaIo::Api::Icd10.new(@http_client)
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,9 @@
1
+ require "aqua_io/error/client_error"
2
+
3
+ module AquaIo
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ module AquaIo
2
+
3
+ module Error
4
+
5
+ class ClientError < ::StandardError
6
+
7
+ attr_reader :code
8
+
9
+ def initialize(message, code)
10
+ @code = code
11
+ super message
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,122 @@
1
+ require "aqua_io/http_client/auth_handler"
2
+ require "aqua_io/http_client/error_handler"
3
+ require "aqua_io/http_client/request_handler"
4
+ require "aqua_io/http_client/response"
5
+ require "aqua_io/http_client/response_handler"
6
+
7
+ module AquaIo
8
+
9
+ module HttpClient
10
+
11
+ # Main HttpClient which is used by Api classes
12
+ class HttpClient
13
+
14
+ attr_accessor :options, :headers
15
+
16
+ def initialize(auth = {}, options = {})
17
+
18
+ if auth.is_a?(String)
19
+ auth = { :access_token => auth }
20
+ end
21
+
22
+ @options = {
23
+ :base => "https://api.aqua.io",
24
+ :user_agent => "Aqua.io official Ruby client (0.1.0)"
25
+ }
26
+
27
+ @options.update(options)
28
+
29
+ @headers = {
30
+ "user-agent" => @options[:user_agent]
31
+ }
32
+
33
+ if @options.has_key?(:headers)
34
+ @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
35
+ @options.delete(:headers)
36
+ end
37
+
38
+ @client = Faraday.new(@options[:base]) do |conn|
39
+ conn.use(AquaIo::HttpClient::AuthHandler, auth)
40
+ conn.use(AquaIo::HttpClient::ErrorHandler)
41
+
42
+ conn.adapter(Faraday.default_adapter)
43
+ end
44
+ end
45
+
46
+ def get(path, params = {}, options = {})
47
+ request(path, nil, "get", options.merge({ :query => params }))
48
+ end
49
+
50
+ def post(path, body = {}, options = {})
51
+ request(path, body, "post", options)
52
+ end
53
+
54
+ def patch(path, body = {}, options = {})
55
+ request(path, body, "patch", options)
56
+ end
57
+
58
+ def delete(path, body = {}, options = {})
59
+ request(path, body, "delete", options)
60
+ end
61
+
62
+ def put(path, body = {}, options = {})
63
+ request(path, body, "put", options)
64
+ end
65
+
66
+ # Intermediate function which does three main things
67
+ #
68
+ # - Transforms the body of request into correct format
69
+ # - Creates the requests with give parameters
70
+ # - Returns response body after parsing it into correct format
71
+ def request(path, body, method, options)
72
+ options = @options.merge(options)
73
+
74
+ options[:headers] = options[:headers] || {}
75
+ options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])
76
+
77
+ options[:body] = body
78
+
79
+ if method != "get"
80
+ options[:body] = options[:body] || {}
81
+ options = set_body(options)
82
+ end
83
+
84
+ response = create_request(method, path, options)
85
+
86
+ body = get_body(response)
87
+
88
+ AquaIo::HttpClient::Response.new(body, response.status, response.headers)
89
+ end
90
+
91
+ # Creating a request with the given arguments
92
+ #
93
+ # If api_version is set, appends it immediately after host
94
+ def create_request(method, path, options)
95
+ version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
96
+
97
+ path = "#{version}#{path}"
98
+
99
+ instance_eval <<-RUBY, __FILE__, __LINE__ + 1
100
+ @client.#{method}(path) do |req|
101
+ req.body = options[:body]
102
+ req.headers.update(options[:headers])
103
+ req.params.update(options[:query]) if options[:query]
104
+ end
105
+ RUBY
106
+ end
107
+
108
+ # Get response body in correct format
109
+ def get_body(response)
110
+ AquaIo::HttpClient::ResponseHandler.get_body(response)
111
+ end
112
+
113
+ # Set request body in correct format
114
+ def set_body(options)
115
+ AquaIo::HttpClient::RequestHandler.set_body(options)
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -0,0 +1,91 @@
1
+ require "base64"
2
+
3
+ module AquaIo
4
+
5
+ module HttpClient
6
+
7
+ # AuthHandler takes care of devising the auth type and using it
8
+ class AuthHandler < Faraday::Middleware
9
+
10
+ URL_SECRET = 2
11
+ URL_TOKEN = 3
12
+
13
+ def initialize(app, auth = {}, options = {})
14
+ @auth = auth
15
+ super(app)
16
+ end
17
+
18
+ def call(env)
19
+ if !@auth.empty?
20
+ auth = get_auth_type
21
+ flag = false
22
+
23
+ if auth == URL_SECRET
24
+ env = url_secret(env)
25
+ flag = true
26
+ end
27
+
28
+ if auth == URL_TOKEN
29
+ env = url_token(env)
30
+ flag = true
31
+ end
32
+
33
+ if !flag
34
+ raise StandardError.new "Unable to calculate authorization method. Please check"
35
+ end
36
+ end
37
+
38
+ @app.call(env)
39
+ end
40
+
41
+ # Calculating the Authentication Type
42
+ def get_auth_type()
43
+
44
+ if @auth.has_key?(:client_id) and @auth.has_key?(:client_secret)
45
+ return URL_SECRET
46
+ end
47
+
48
+ if @auth.has_key?(:access_token)
49
+ return URL_TOKEN
50
+ end
51
+
52
+ return -1
53
+ end
54
+
55
+ # OAUTH2 Authorization with client secret
56
+ def url_secret(env)
57
+ query = {
58
+ :client_id => @auth[:client_id],
59
+ :client_secret => @auth[:client_secret]
60
+ }
61
+
62
+ merge_query(env, query)
63
+ end
64
+
65
+ # OAUTH2 Authorization with access token
66
+ def url_token(env)
67
+ query = { :access_token => @auth[:access_token] }
68
+
69
+ merge_query(env, query)
70
+ end
71
+
72
+ def query_params(url)
73
+ if url.query.nil? or url.query.empty?
74
+ {}
75
+ else
76
+ Faraday::Utils.parse_query(url.query)
77
+ end
78
+ end
79
+
80
+ def merge_query(env, query)
81
+ query = query.update query_params(env[:url])
82
+
83
+ env[:url].query = Faraday::Utils.build_query(query)
84
+
85
+ return env
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,51 @@
1
+ module AquaIo
2
+
3
+ module HttpClient
4
+
5
+ # ErrorHanlder takes care of selecting the error message from response body
6
+ class ErrorHandler < Faraday::Middleware
7
+
8
+ def initialize(app)
9
+ super(app)
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).on_complete do |env|
14
+ code = env.status
15
+ type = env.response_headers["content-type"]
16
+
17
+ case code
18
+ when 500...599
19
+ raise AquaIo::Error::ClientError.new("Error #{code}", code)
20
+ when 400...499
21
+ body = AquaIo::HttpClient::ResponseHandler.get_body(env)
22
+ message = ""
23
+
24
+ # If HTML, whole body is taken
25
+ if body.is_a?(String)
26
+ message = body
27
+ end
28
+
29
+ # If JSON, a particular field is taken and used
30
+ if type.include?("json") and body.is_a?(Hash)
31
+ if body.has_key?("")
32
+ message = body[""]
33
+ else
34
+ message = "Unable to select error message from json returned by request responsible for error"
35
+ end
36
+ end
37
+
38
+ if message == ""
39
+ message = "Unable to understand the content type of response returned by request responsible for error"
40
+ end
41
+
42
+ raise AquaIo::Error::ClientError.new message, code
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,30 @@
1
+ module AquaIo
2
+
3
+ module HttpClient
4
+
5
+ # RequestHandler takes care of encoding the request body into format given by options
6
+ class RequestHandler
7
+
8
+ def self.set_body(options)
9
+ type = options.fetch(:request_type, "json")
10
+
11
+ # Encoding request body into JSON format
12
+ if type && type == "json"
13
+ options[:body] = options[:body].to_json
14
+ options[:headers]["content-type"] = "application/json"
15
+ end
16
+
17
+ # Raw body
18
+ if type && type == "raw"
19
+ options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
20
+ options[:headers].delete("content-type")
21
+ end
22
+
23
+ return options
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,20 @@
1
+ module AquaIo
2
+
3
+ module HttpClient
4
+
5
+ # Response object contains the response returned by the client
6
+ class Response
7
+
8
+ attr_accessor :body, :code, :headers
9
+
10
+ def initialize(body, code, headers)
11
+ @body = body
12
+ @code = code
13
+ @headers = headers
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ module AquaIo
2
+
3
+ module HttpClient
4
+
5
+ # ResponseHandler takes care of decoding the response body into suitable type
6
+ class ResponseHandler
7
+
8
+ def self.get_body(response)
9
+ type = response.response_headers["content-type"]
10
+ body = response.body
11
+
12
+ # Response body is in JSON
13
+ if type && type.include?("json")
14
+ body = JSON.parse body
15
+ end
16
+
17
+ return body
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module AquaIo
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aqua-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Carroll / Aqua.io
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.9'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.7.7
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.7'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.7.7
53
+ description: Official Aqua.io API library client for ruby. Covers API authentication
54
+ and methods for ICD-9 and ICD-10 code endpoints.
55
+ email: michael@aqua.io
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - lib/aqua-io.rb
61
+ - lib/aqua_io/api/access_token.rb
62
+ - lib/aqua_io/api/icd10.rb
63
+ - lib/aqua_io/api/icd9.rb
64
+ - lib/aqua_io/client.rb
65
+ - lib/aqua_io/error.rb
66
+ - lib/aqua_io/error/client_error.rb
67
+ - lib/aqua_io/http_client.rb
68
+ - lib/aqua_io/http_client/auth_handler.rb
69
+ - lib/aqua_io/http_client/error_handler.rb
70
+ - lib/aqua_io/http_client/request_handler.rb
71
+ - lib/aqua_io/http_client/response.rb
72
+ - lib/aqua_io/http_client/response_handler.rb
73
+ - lib/aqua_io/version.rb
74
+ homepage: https://aqua.io
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Official Aqua.io API library client for ruby (ICD-9 and ICD-10).
98
+ test_files: []
99
+ has_rdoc: