kickbox-1.0.5 1.0.5

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: 95ed4a1d21cc152872d92dda532f3bc845fd1f03
4
+ data.tar.gz: 7c53e9f777202f680ab3c97defd9118a25a58632
5
+ SHA512:
6
+ metadata.gz: fff03a7c82b0e8af8386c82b957c44453d07fcf0936991d16ceaa1478c40bf492a070a016d2fbd38147acd5c11e085363f852c7a180cbee7e4db00ff042dd30e
7
+ data.tar.gz: b639f4b9289126fd969fa71b76e531d20f0fc9429cf0d8d16bf1505d6a8f886ac9ab11a71211d6f5ccf2e649e977fb9d643ea80897d36ab3d034a243db9722d3
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+
3
+ require "kickbox/client"
4
+ require "kickbox/error"
5
+ require "kickbox/http_client"
6
+
7
+ module Kickbox
8
+ end
@@ -0,0 +1,27 @@
1
+ module Kickbox
2
+
3
+ module Api
4
+
5
+ #
6
+ class Kickbox
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Email Verification
13
+ #
14
+ # '/verify?email=:email' GET
15
+ #
16
+ # email - Email address to verify
17
+ def verify(email, options = {})
18
+ body = options.fetch(:query, {})
19
+
20
+ @client.get("/verify?email=#{email}", body, options)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,21 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "kickbox/api/kickbox"
5
+
6
+ module Kickbox
7
+
8
+ class Client
9
+
10
+ def initialize(auth = {}, options = {})
11
+ @http_client = Kickbox::HttpClient::HttpClient.new(auth, options)
12
+ end
13
+
14
+ #
15
+ def kickbox()
16
+ Kickbox::Api::Kickbox.new(@http_client)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,9 @@
1
+ require "kickbox/error/client_error"
2
+
3
+ module Kickbox
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ module Kickbox
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,123 @@
1
+ require "kickbox/http_client/auth_handler"
2
+ require "kickbox/http_client/error_handler"
3
+ require "kickbox/http_client/request_handler"
4
+ require "kickbox/http_client/response"
5
+ require "kickbox/http_client/response_handler"
6
+
7
+ module Kickbox
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 = { :http_header => auth }
20
+ end
21
+
22
+ @options = {
23
+ :base => "https://api.kickbox.io",
24
+ :api_version => "v1",
25
+ :user_agent => "alpaca/0.2.1 (https://github.com/pksunkara/alpaca)"
26
+ }
27
+
28
+ @options.update(options)
29
+
30
+ @headers = {
31
+ "user-agent" => @options[:user_agent]
32
+ }
33
+
34
+ if @options.has_key?(:headers)
35
+ @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
36
+ @options.delete(:headers)
37
+ end
38
+
39
+ @client = Faraday.new(@options[:base]) do |conn|
40
+ conn.use(Kickbox::HttpClient::AuthHandler, auth)
41
+ conn.use(Kickbox::HttpClient::ErrorHandler)
42
+
43
+ conn.adapter(Faraday.default_adapter)
44
+ end
45
+ end
46
+
47
+ def get(path, params = {}, options = {})
48
+ request(path, nil, "get", options.merge({ :query => params }))
49
+ end
50
+
51
+ def post(path, body = {}, options = {})
52
+ request(path, body, "post", options)
53
+ end
54
+
55
+ def patch(path, body = {}, options = {})
56
+ request(path, body, "patch", options)
57
+ end
58
+
59
+ def delete(path, body = {}, options = {})
60
+ request(path, body, "delete", options)
61
+ end
62
+
63
+ def put(path, body = {}, options = {})
64
+ request(path, body, "put", options)
65
+ end
66
+
67
+ # Intermediate function which does three main things
68
+ #
69
+ # - Transforms the body of request into correct format
70
+ # - Creates the requests with give parameters
71
+ # - Returns response body after parsing it into correct format
72
+ def request(path, body, method, options)
73
+ options = @options.merge(options)
74
+
75
+ options[:headers] = options[:headers] || {}
76
+ options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])
77
+
78
+ options[:body] = body
79
+
80
+ if method != "get"
81
+ options[:body] = options[:body] || {}
82
+ options = set_body(options)
83
+ end
84
+
85
+ response = create_request(method, path, options)
86
+
87
+ body = get_body(response)
88
+
89
+ Kickbox::HttpClient::Response.new(body, response.status, response.headers)
90
+ end
91
+
92
+ # Creating a request with the given arguments
93
+ #
94
+ # If api_version is set, appends it immediately after host
95
+ def create_request(method, path, options)
96
+ version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
97
+
98
+ path = "#{version}#{path}"
99
+
100
+ instance_eval <<-RUBY, __FILE__, __LINE__ + 1
101
+ @client.#{method}(path) do |req|
102
+ req.body = options[:body]
103
+ req.headers.update(options[:headers])
104
+ req.params.update(options[:query]) if options[:query]
105
+ end
106
+ RUBY
107
+ end
108
+
109
+ # Get response body in correct format
110
+ def get_body(response)
111
+ Kickbox::HttpClient::ResponseHandler.get_body(response)
112
+ end
113
+
114
+ # Set request body in correct format
115
+ def set_body(options)
116
+ Kickbox::HttpClient::RequestHandler.set_body(options)
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,73 @@
1
+ require "base64"
2
+
3
+ module Kickbox
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_HEADER = 1
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_HEADER
23
+ env = http_header(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?(:http_header)
41
+ return HTTP_HEADER
42
+ end
43
+
44
+ return -1
45
+ end
46
+
47
+ # Authorization with HTTP header
48
+ def http_header(env)
49
+ env[:request_headers]["Authorization"] = "token #{@auth[:http_header]}"
50
+
51
+ return env
52
+ end
53
+
54
+ def query_params(url)
55
+ if url.query.nil? or url.query.empty?
56
+ {}
57
+ else
58
+ Faraday::Utils.parse_query(url.query)
59
+ end
60
+ end
61
+
62
+ def merge_query(env, query)
63
+ query = query.update query_params(env[:url])
64
+
65
+ env[:url].query = Faraday::Utils.build_query(query)
66
+
67
+ return env
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,51 @@
1
+ module Kickbox
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 Kickbox::Error::ClientError.new("Error #{code}", code)
20
+ when 400...499
21
+ body = Kickbox::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 Kickbox::Error::ClientError.new message, code
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,24 @@
1
+ module Kickbox
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, "raw")
10
+
11
+ # Raw body
12
+ if type == "raw"
13
+ options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
14
+ options[:headers].delete("content-type")
15
+ end
16
+
17
+ return options
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+ module Kickbox
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 Kickbox
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
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kickbox-1.0.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Chaitanya Surapaneni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-15 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.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.1
41
+ description: Official kickbox API library client for ruby
42
+ email: chaitanya.surapaneni@kickbox.io
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/kickbox.rb
48
+ - lib/kickbox/api/kickbox.rb
49
+ - lib/kickbox/client.rb
50
+ - lib/kickbox/error.rb
51
+ - lib/kickbox/error/client_error.rb
52
+ - lib/kickbox/http_client.rb
53
+ - lib/kickbox/http_client/auth_handler.rb
54
+ - lib/kickbox/http_client/error_handler.rb
55
+ - lib/kickbox/http_client/request_handler.rb
56
+ - lib/kickbox/http_client/response.rb
57
+ - lib/kickbox/http_client/response_handler.rb
58
+ homepage: http://kickbox.io
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Official kickbox API library client for ruby
82
+ test_files: []