helpful 0.0.1.pre → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae8b4de74ad5a4761ce73061ae9ea988e790754b
4
- data.tar.gz: 7584493dc2dc4aace5498e3112248c0615b65f3e
3
+ metadata.gz: 472d84181e1393ba4152cee0c2307c56c5cc85c5
4
+ data.tar.gz: 974104771ae3ffe8e888bac99242040c254cbbba
5
5
  SHA512:
6
- metadata.gz: 64c3c53b8ed342f348f167acc503c7038e30b2bf46ddf3e35111ba4776e1b3af728ff73864cc4fb67b1a09b506091a0b89f7429c7aee2ad12cc44ba0959b67a6
7
- data.tar.gz: 94c4baace34cc708e6873dbbcd5b0c45e1797a2c2115baa6ebdcea8ec6571460a823c339004758e8856b4706876cf0571a4f108d749dfd491c4ebb4d18423f93
6
+ metadata.gz: e6198b4f5838633ecd0abc95ab357efed9e8b6fc08cfcf58d90eb79035ab54e6631177624336c72576d4f13a63f6044caee12c263349dde426f991db4f6c1cc6
7
+ data.tar.gz: b91b230ce161d23a688f0e74a37d1066a77bb70dee3f8cc45fc77fae2f84474553d5666cbd80a761e69fd7d96f5a5ba06c977e8e026d923ee52cf6dcdeabc14e
@@ -1 +1,8 @@
1
- require 'helpful/version'
1
+ require "rubygems"
2
+
3
+ require "helpful/client"
4
+ require "helpful/error"
5
+ require "helpful/http_client"
6
+
7
+ module Helpful
8
+ end
@@ -0,0 +1,47 @@
1
+ module Helpful
2
+
3
+ module Api
4
+
5
+ # These are like organizations which use Helpful.
6
+ class Accounts
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # All the accounts the user has access to
13
+ #
14
+ # '/accounts' GET
15
+ def all(options = {})
16
+ body = options.fetch(:query, {})
17
+
18
+ @client.get("/accounts", body, options)
19
+ end
20
+
21
+ # Get an account the user has access to
22
+ #
23
+ # '/accounts/:account_id' GET
24
+ #
25
+ # account_id - Identifier of the account
26
+ def get(account_id, options = {})
27
+ body = options.fetch(:query, {})
28
+
29
+ @client.get("/accounts/#{account_id}", body, options)
30
+ end
31
+
32
+ # Update an account the user has access to
33
+ #
34
+ # '/accounts/:account_id' PATCH
35
+ #
36
+ # account_id - Identifier of the account
37
+ def update(account_id, options = {})
38
+ body = options.fetch(:body, {})
39
+
40
+ @client.patch("/accounts/#{account_id}", body, options)
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,49 @@
1
+ module Helpful
2
+
3
+ module Api
4
+
5
+ # Conversations in an account
6
+ class Conversations
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # List all conversations in an account the user has access to
13
+ #
14
+ # '/accounts/:account_id/conversations' GET
15
+ #
16
+ # account_id - Identifier of the account
17
+ def all(account_id, options = {})
18
+ body = options.fetch(:query, {})
19
+
20
+ @client.get("/accounts/#{account_id}/conversations", body, options)
21
+ end
22
+
23
+ # Create an empty conversation in account the user has access to
24
+ #
25
+ # '/accounts/:account_id/conversations' POST
26
+ #
27
+ # account_id - Identifier of the account
28
+ def create(account_id, options = {})
29
+ body = options.fetch(:body, {})
30
+
31
+ @client.post("/accounts/#{account_id}/conversations", body, options)
32
+ end
33
+
34
+ # Get a conversation the user has access to
35
+ #
36
+ # '/conversations/:conversation_id' GET
37
+ #
38
+ # conversation_id - Identifier of the conversation
39
+ def get(conversation_id, options = {})
40
+ body = options.fetch(:query, {})
41
+
42
+ @client.get("/conversations/#{conversation_id}", body, options)
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,27 @@
1
+ module Helpful
2
+
3
+ module Api
4
+
5
+ # Messages in a conversation
6
+ class Messages
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Get a message the user has access to
13
+ #
14
+ # '/messages/:message_id' GET
15
+ #
16
+ # message_id - Identifier of the message
17
+ def get(message_id, options = {})
18
+ body = options.fetch(:query, {})
19
+
20
+ @client.get("/messages/#{message_id}", body, options)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,27 @@
1
+ module Helpful
2
+
3
+ module Api
4
+
5
+ # People who operate or interacted with the account
6
+ class People
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # List all people in the account the user has access to
13
+ #
14
+ # '/accounts/:account_id/people' GET
15
+ #
16
+ # account_id - Identifier of the account
17
+ def all(account_id, options = {})
18
+ body = options.fetch(:query, {})
19
+
20
+ @client.get("/accounts/#{account_id}/people", body, options)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,39 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "helpful/api/accounts"
5
+ require "helpful/api/people"
6
+ require "helpful/api/conversations"
7
+ require "helpful/api/messages"
8
+
9
+ module Helpful
10
+
11
+ class Client
12
+
13
+ def initialize(auth = {}, options = {})
14
+ @http_client = Helpful::HttpClient::HttpClient.new(auth, options)
15
+ end
16
+
17
+ # These are like organizations which use Helpful.
18
+ def accounts()
19
+ Helpful::Api::Accounts.new(@http_client)
20
+ end
21
+
22
+ # People who operate or interacted with the account
23
+ def people()
24
+ Helpful::Api::People.new(@http_client)
25
+ end
26
+
27
+ # Conversations in an account
28
+ def conversations()
29
+ Helpful::Api::Conversations.new(@http_client)
30
+ end
31
+
32
+ # Messages in a conversation
33
+ def messages()
34
+ Helpful::Api::Messages.new(@http_client)
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,9 @@
1
+ require "helpful/error/client_error"
2
+
3
+ module Helpful
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ module Helpful
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 "helpful/http_client/auth_handler"
2
+ require "helpful/http_client/error_handler"
3
+ require "helpful/http_client/request_handler"
4
+ require "helpful/http_client/response"
5
+ require "helpful/http_client/response_handler"
6
+
7
+ module Helpful
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://helpful.io",
24
+ :api_version => "api",
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(Helpful::HttpClient::AuthHandler, auth)
41
+ conn.use(Helpful::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
+ Helpful::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
+ Helpful::HttpClient::ResponseHandler.get_body(response)
112
+ end
113
+
114
+ # Set request body in correct format
115
+ def set_body(options)
116
+ Helpful::HttpClient::RequestHandler.set_body(options)
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,113 @@
1
+ require "base64"
2
+
3
+ module Helpful
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
+ URL_SECRET = 2
13
+ URL_TOKEN = 3
14
+
15
+ def initialize(app, auth = {}, options = {})
16
+ @auth = auth
17
+ super(app)
18
+ end
19
+
20
+ def call(env)
21
+ if !@auth.empty?
22
+ auth = get_auth_type
23
+ flag = false
24
+
25
+ if auth == HTTP_PASSWORD
26
+ env = http_password(env)
27
+ flag = true
28
+ end
29
+
30
+ if auth == URL_SECRET
31
+ env = url_secret(env)
32
+ flag = true
33
+ end
34
+
35
+ if auth == URL_TOKEN
36
+ env = url_token(env)
37
+ flag = true
38
+ end
39
+
40
+ if !flag
41
+ raise StandardError.new "Unable to calculate authorization method. Please check"
42
+ end
43
+ else
44
+ raise StandardError.new "Server requires authentication to proceed further. Please check"
45
+ end
46
+
47
+ @app.call(env)
48
+ end
49
+
50
+ # Calculating the Authentication Type
51
+ def get_auth_type()
52
+
53
+ if @auth.has_key?(:username) and @auth.has_key?(:password)
54
+ return HTTP_PASSWORD
55
+ end
56
+
57
+ if @auth.has_key?(:client_id) and @auth.has_key?(:client_secret)
58
+ return URL_SECRET
59
+ end
60
+
61
+ if @auth.has_key?(:access_token)
62
+ return URL_TOKEN
63
+ end
64
+
65
+ return -1
66
+ end
67
+
68
+ # Basic Authorization with username and password
69
+ def http_password(env)
70
+ code = Base64.encode64 "#{@auth[:username]}:#{@auth[:password]}"
71
+
72
+ env[:request_headers]["Authorization"] = "Basic #{code}"
73
+
74
+ return env
75
+ end
76
+
77
+ # OAUTH2 Authorization with client secret
78
+ def url_secret(env)
79
+ query = {
80
+ :client_id => @auth[:client_id],
81
+ :client_secret => @auth[:client_secret]
82
+ }
83
+
84
+ merge_query(env, query)
85
+ end
86
+
87
+ # OAUTH2 Authorization with access token
88
+ def url_token(env)
89
+ query = { :access_token => @auth[:access_token] }
90
+
91
+ merge_query(env, query)
92
+ end
93
+
94
+ def query_params(url)
95
+ if url.query.nil? or url.query.empty?
96
+ {}
97
+ else
98
+ Faraday::Utils.parse_query(url.query)
99
+ end
100
+ end
101
+
102
+ def merge_query(env, query)
103
+ query = query.update query_params(env[:url])
104
+
105
+ env[:url].query = Faraday::Utils.build_query(query)
106
+
107
+ return env
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -0,0 +1,51 @@
1
+ module Helpful
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 Helpful::Error::ClientError.new("Error #{code}", code)
20
+ when 400...499
21
+ body = Helpful::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?("error")
32
+ message = body["error"]
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 Helpful::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 Helpful
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 Helpful
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 Helpful
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 CHANGED
@@ -1,27 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helpful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Assembly
7
+ - Pavan Kumar Sunkara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2012-02-18 00:00:00.000000000 Z
12
- dependencies: []
13
- description:
14
- email:
15
- - christopher.lloyd@gmail.com
16
- - helpful@assemblymade.com
11
+ date: 2014-09-12 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.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
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 Helpful API library client for ruby
42
+ email: pavan.sss1991@gmail.com
17
43
  executables: []
18
44
  extensions: []
19
45
  extra_rdoc_files: []
20
46
  files:
21
47
  - lib/helpful.rb
22
- - lib/helpful/version.rb
23
- homepage: http://assemblymade.com/helpful
24
- licenses: []
48
+ - lib/helpful/api/accounts.rb
49
+ - lib/helpful/api/conversations.rb
50
+ - lib/helpful/api/messages.rb
51
+ - lib/helpful/api/people.rb
52
+ - lib/helpful/client.rb
53
+ - lib/helpful/error.rb
54
+ - lib/helpful/error/client_error.rb
55
+ - lib/helpful/http_client.rb
56
+ - lib/helpful/http_client/auth_handler.rb
57
+ - lib/helpful/http_client/error_handler.rb
58
+ - lib/helpful/http_client/request_handler.rb
59
+ - lib/helpful/http_client/response.rb
60
+ - lib/helpful/http_client/response_handler.rb
61
+ homepage: https://helpful.io
62
+ licenses:
63
+ - MIT
25
64
  metadata: {}
26
65
  post_install_message:
27
66
  rdoc_options: []
@@ -34,13 +73,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
73
  version: '0'
35
74
  required_rubygems_version: !ruby/object:Gem::Requirement
36
75
  requirements:
37
- - - ">"
76
+ - - ">="
38
77
  - !ruby/object:Gem::Version
39
- version: 1.3.1
78
+ version: '0'
40
79
  requirements: []
41
80
  rubyforge_project:
42
- rubygems_version: 2.2.0
81
+ rubygems_version: 2.2.2
43
82
  signing_key:
44
83
  specification_version: 4
45
- summary: A Ruby client library for Helpful.io
84
+ summary: Official Helpful API library client for ruby
46
85
  test_files: []
@@ -1,3 +0,0 @@
1
- module Helpful
2
- VERSION = '0.0.1.pre'
3
- end