confyio 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea9e4413332fb7ad883b464c1a25cab29716df58
4
+ data.tar.gz: 391ffa974eba2de3d5245a46885918f0e06f6c41
5
+ SHA512:
6
+ metadata.gz: d187682791f3ebf9bb4456ae708865c28e0b97352c4da5aac25eac12ff8c4abc664712c8e1629b96e11f37d0bce7ce6e70d8f83ee1f8f12bd5807011e066fe8b
7
+ data.tar.gz: 54ff76bd53bfa3801383fedb436107e0e3d0463d0945d7a41109f435df3e2dc85aa3d0c2d065eba0a0dfe7511b0e6ddba8ddd2ecc5023f771cb39a47391f7974
@@ -0,0 +1,49 @@
1
+ module Confy
2
+
3
+ module Api
4
+
5
+ # Organizations are owned by users and only (s)he can add/remove teams and projects for that organization. A default organization will be created for every user.
6
+ class Orgs
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # List all organizations the authenticated user is a member of.
13
+ #
14
+ # '/orgs' GET
15
+ def list(options = {})
16
+ body = options.fetch(:query, {})
17
+
18
+ @client.get("/orgs", body, options)
19
+ end
20
+
21
+ # Get an organization the user has access to.
22
+ #
23
+ # '/orgs/:org' GET
24
+ #
25
+ # org - Name of the organization
26
+ def retrieve(org, options = {})
27
+ body = options.fetch(:query, {})
28
+
29
+ @client.get("/orgs/#{org}", body, options)
30
+ end
31
+
32
+ # Update an organization the user is owner of.
33
+ #
34
+ # '/orgs/:org' PATCH
35
+ #
36
+ # org - Name of the organization
37
+ # email - Billing email of the organization
38
+ def update(org, email, options = {})
39
+ body = options.fetch(:body, {})
40
+ body[:email] = email
41
+
42
+ @client.patch("/orgs/#{org}", body, options)
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,37 @@
1
+ module Confy
2
+
3
+ module Api
4
+
5
+ # User who is authenticated currently.
6
+ class User
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Get the authenticated user's info.
13
+ #
14
+ # '/user' GET
15
+ def retrieve(options = {})
16
+ body = options.fetch(:query, {})
17
+
18
+ @client.get("/user", body, options)
19
+ end
20
+
21
+ # Update the authenticated user's profile
22
+ #
23
+ # '/user' PATCH
24
+ #
25
+ # email - Profile email of the user
26
+ def update(email, options = {})
27
+ body = options.fetch(:body, {})
28
+ body[:email] = email
29
+
30
+ @client.patch("/user", body, options)
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,27 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "confy/api/user"
5
+ require "confy/api/orgs"
6
+
7
+ module Confy
8
+
9
+ class Client
10
+
11
+ def initialize(auth = {}, options = {})
12
+ @http_client = Confy::HttpClient::HttpClient.new(auth, options)
13
+ end
14
+
15
+ # User who is authenticated currently.
16
+ def user()
17
+ Confy::Api::User.new(@http_client)
18
+ end
19
+
20
+ # Organizations are owned by users and only (s)he can add/remove teams and projects for that organization. A default organization will be created for every user.
21
+ def orgs()
22
+ Confy::Api::Orgs.new(@http_client)
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,9 @@
1
+ require "confy/error/client_error"
2
+
3
+ module Confy
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ module Confy
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,118 @@
1
+ require "confy/http_client/auth_handler"
2
+ require "confy/http_client/error_handler"
3
+ require "confy/http_client/request_handler"
4
+ require "confy/http_client/response"
5
+ require "confy/http_client/response_handler"
6
+
7
+ module Confy
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
+ @options = {
19
+ :base => "https://api.confy.io",
20
+ :user_agent => "alpaca/0.2.0 (https://github.com/pksunkara/alpaca)"
21
+ }
22
+
23
+ @options.update(options)
24
+
25
+ @headers = {
26
+ "user-agent" => @options[:user_agent]
27
+ }
28
+
29
+ if @options.has_key?(:headers)
30
+ @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
31
+ @options.delete(:headers)
32
+ end
33
+
34
+ @client = Faraday.new(@options[:base]) do |conn|
35
+ conn.use(Confy::HttpClient::AuthHandler, auth)
36
+ conn.use(Confy::HttpClient::ErrorHandler)
37
+
38
+ conn.adapter(Faraday.default_adapter)
39
+ end
40
+ end
41
+
42
+ def get(path, params = {}, options = {})
43
+ request(path, nil, "get", options.merge({ :query => params }))
44
+ end
45
+
46
+ def post(path, body = {}, options = {})
47
+ request(path, body, "post", options)
48
+ end
49
+
50
+ def patch(path, body = {}, options = {})
51
+ request(path, body, "patch", options)
52
+ end
53
+
54
+ def delete(path, body = {}, options = {})
55
+ request(path, body, "delete", options)
56
+ end
57
+
58
+ def put(path, body = {}, options = {})
59
+ request(path, body, "put", options)
60
+ end
61
+
62
+ # Intermediate function which does three main things
63
+ #
64
+ # - Transforms the body of request into correct format
65
+ # - Creates the requests with give parameters
66
+ # - Returns response body after parsing it into correct format
67
+ def request(path, body, method, options)
68
+ options = @options.merge(options)
69
+
70
+ options[:headers] = options[:headers] || {}
71
+ options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])
72
+
73
+ options[:body] = body
74
+
75
+ if method != "get"
76
+ options[:body] = options[:body] || {}
77
+ options = set_body(options)
78
+ end
79
+
80
+ response = create_request(method, path, options)
81
+
82
+ body = get_body(response)
83
+
84
+ Confy::HttpClient::Response.new(body, response.status, response.headers)
85
+ end
86
+
87
+ # Creating a request with the given arguments
88
+ #
89
+ # If api_version is set, appends it immediately after host
90
+ def create_request(method, path, options)
91
+ version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
92
+
93
+ path = "#{version}#{path}"
94
+
95
+ instance_eval <<-RUBY, __FILE__, __LINE__ + 1
96
+ @client.#{method}(path) do |req|
97
+ req.body = options[:body]
98
+ req.headers.update(options[:headers])
99
+ req.params.update(options[:query]) if options[:query]
100
+ end
101
+ RUBY
102
+ end
103
+
104
+ # Get response body in correct format
105
+ def get_body(response)
106
+ Confy::HttpClient::ResponseHandler.get_body(response)
107
+ end
108
+
109
+ # Set request body in correct format
110
+ def set_body(options)
111
+ Confy::HttpClient::RequestHandler.set_body(options)
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,75 @@
1
+ require "base64"
2
+
3
+ module Confy
4
+
5
+ module HttpClient
6
+
7
+ # AuthHandler takes care of devising the auth type and using it
8
+ class AuthHandler < Faraday::Middleware
9
+
10
+ HTTP_PASSWORD = 0
11
+
12
+ def initialize(app, auth = {}, options = {})
13
+ @auth = auth
14
+ super(app)
15
+ end
16
+
17
+ def call(env)
18
+ if !@auth.empty?
19
+ auth = get_auth_type
20
+ flag = false
21
+
22
+ if auth == HTTP_PASSWORD
23
+ env = http_password(env)
24
+ flag = true
25
+ end
26
+
27
+ if !flag
28
+ raise StandardError.new "Unable to calculate authorization method. Please check"
29
+ end
30
+ else
31
+ raise StandardError.new "Server requires authentication to proceed further. Please check"
32
+ end
33
+
34
+ @app.call(env)
35
+ end
36
+
37
+ # Calculating the Authentication Type
38
+ def get_auth_type()
39
+
40
+ if @auth.has_key?(:username) and @auth.has_key?(:password)
41
+ return HTTP_PASSWORD
42
+ end
43
+
44
+ return -1
45
+ end
46
+
47
+ # Basic Authorization with username and password
48
+ def http_password(env)
49
+ code = Base64.encode64 "#{@auth[:username]}:#{@auth[:password]}"
50
+
51
+ env[:request_headers]["Authorization"] = "Basic #{code}"
52
+
53
+ return env
54
+ end
55
+
56
+ def query_params(url)
57
+ if url.query.nil? or url.query.empty?
58
+ {}
59
+ else
60
+ Faraday::Utils.parse_query(url.query)
61
+ end
62
+ end
63
+
64
+ def merge_query(env, query)
65
+ query = query.update query_params(env[:url])
66
+
67
+ env[:url].query = Faraday::Utils.build_query(query)
68
+
69
+ return env
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,51 @@
1
+ module Confy
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[:response].status
15
+ type = env[:response].headers["content-type"]
16
+
17
+ case code
18
+ when 500...599
19
+ raise Confy::Error::ClientError.new("Error #{code}", code)
20
+ when 400...499
21
+ body = Confy::HttpClient::ResponseHandler.get_body(env[:response])
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?("message")
32
+ message = body["message"]
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 Confy::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 Confy
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 == "json"
13
+ options[:body] = options[:body].to_json
14
+ options[:headers]["content-type"] = "application/json"
15
+ end
16
+
17
+ # Raw body
18
+ if 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 Confy
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 Confy
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.headers["content-type"]
10
+ body = response.body
11
+
12
+ # Response body is in JSON
13
+ if 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
data/lib/confyio.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+
3
+ require "confy/client"
4
+ require "confy/error"
5
+ require "confy/http_client"
6
+
7
+ module Confy
8
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confyio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavan Kumar Sunkara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-14 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.8.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.7
41
+ description: Official Confy API library client for ruby
42
+ email: pavan.sss1991@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/confy/http_client/response_handler.rb
48
+ - lib/confy/http_client/request_handler.rb
49
+ - lib/confy/http_client/response.rb
50
+ - lib/confy/http_client/error_handler.rb
51
+ - lib/confy/http_client/auth_handler.rb
52
+ - lib/confy/api/orgs.rb
53
+ - lib/confy/api/user.rb
54
+ - lib/confy/http_client.rb
55
+ - lib/confy/client.rb
56
+ - lib/confy/error/client_error.rb
57
+ - lib/confy/error.rb
58
+ - lib/confyio.rb
59
+ homepage: https://confy.io
60
+ licenses:
61
+ - BSD
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.1.11
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Official Confy API library client for ruby
83
+ test_files: []