help-scout-docs 0.1

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +56 -0
  5. data/README.rdoc +62 -0
  6. data/help_scout_docs.gemspec +33 -0
  7. data/lib/help_scout_docs.rb +14 -0
  8. data/lib/help_scout_docs/client.rb +95 -0
  9. data/lib/help_scout_docs/configurable.rb +78 -0
  10. data/lib/help_scout_docs/default.rb +55 -0
  11. data/lib/help_scout_docs/error.rb +5 -0
  12. data/lib/help_scout_docs/error/authentication_error.rb +6 -0
  13. data/lib/help_scout_docs/error/configuration_error.rb +6 -0
  14. data/lib/help_scout_docs/error/errors.rb +6 -0
  15. data/lib/help_scout_docs/error/options_error.rb +6 -0
  16. data/lib/help_scout_docs/error/parser_error.rb +6 -0
  17. data/lib/help_scout_docs/error/resource_not_found_error.rb +6 -0
  18. data/lib/help_scout_docs/method.rb +54 -0
  19. data/lib/help_scout_docs/methods/article.rb +45 -0
  20. data/lib/help_scout_docs/methods/category.rb +16 -0
  21. data/lib/help_scout_docs/methods/collection.rb +15 -0
  22. data/lib/help_scout_docs/methods/methods.rb +6 -0
  23. data/lib/help_scout_docs/methods/site.rb +15 -0
  24. data/lib/help_scout_docs/response/parse_json.rb +33 -0
  25. data/lib/help_scout_docs/result.rb +31 -0
  26. data/lib/help_scout_docs/version.rb +3 -0
  27. data/spec/cassettes/HelpScoutDocs_Article/_get/should_return_the_article.yml +46 -0
  28. data/spec/cassettes/HelpScoutDocs_Article/_get/when_using_an_id/should_return_the_article.yml +46 -0
  29. data/spec/cassettes/HelpScoutDocs_Article/_list_by_category/should_return_the_list_of_articles_by_category.yml +46 -0
  30. data/spec/cassettes/HelpScoutDocs_Article/_list_by_collection/should_return_the_list_of_articles_by_collection.yml +47 -0
  31. data/spec/cassettes/HelpScoutDocs_Article/_related/should_return_related_articles.yml +46 -0
  32. data/spec/cassettes/HelpScoutDocs_Article/_revisions/should_return_article_revisions.yml +46 -0
  33. data/spec/cassettes/HelpScoutDocs_Category/_get/should_return_the_category.yml +46 -0
  34. data/spec/cassettes/HelpScoutDocs_Category/_list/should_return_the_list_of_categories.yml +46 -0
  35. data/spec/cassettes/HelpScoutDocs_Client/_get/for_a_missing_request/should_raise_an_exception.yml +46 -0
  36. data/spec/cassettes/HelpScoutDocs_Client/_get/should_return_back_a_result.yml +65 -0
  37. data/spec/cassettes/HelpScoutDocs_Client/_get/with_invalid_authentication_params/should_raise_an_exception.yml +38 -0
  38. data/spec/cassettes/HelpScoutDocs_Client/_get/with_valid_params/should_call_the_requested_method.yml +65 -0
  39. data/spec/cassettes/HelpScoutDocs_Collection/_get/should_return_the_collection.yml +46 -0
  40. data/spec/cassettes/HelpScoutDocs_Collection/_list/should_return_the_list_of_collections.yml +48 -0
  41. data/spec/cassettes/HelpScoutDocs_Site/_get/should_return_the_site.yml +46 -0
  42. data/spec/cassettes/HelpScoutDocs_Site/_list/should_return_the_list_of_sites.yml +65 -0
  43. data/spec/client_spec.rb +78 -0
  44. data/spec/method_spec.rb +26 -0
  45. data/spec/methods/article_spec.rb +71 -0
  46. data/spec/methods/category_spec.rb +24 -0
  47. data/spec/methods/collection_spec.rb +23 -0
  48. data/spec/methods/site_spec.rb +23 -0
  49. data/spec/result_spec.rb +21 -0
  50. data/spec/spec_helper.rb +18 -0
  51. metadata +245 -0
@@ -0,0 +1,6 @@
1
+ module HelpScoutDocs
2
+ class Error
3
+ class OptionsError < Error
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module HelpScoutDocs
2
+ class Error
3
+ class ParserError < Error
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module HelpScoutDocs
2
+ class Error
3
+ class ResourceNotFoundError < Error
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,54 @@
1
+ module HelpScoutDocs
2
+ class Method
3
+ extend Forwardable
4
+ attr_accessor :method, :client, :options
5
+
6
+ def initialize(client=nil, options={})
7
+ @options ||= options
8
+ @client = client || initialize_client
9
+ end
10
+
11
+ def_delegator :@client, :get, :get_request
12
+ def_delegator :@client, :post, :post_request
13
+ def_delegator :@client, :path
14
+
15
+ # Delegate to a HelpScoutDocs::Client
16
+ #
17
+ # @return [HelpScoutDocs::Client]
18
+ #
19
+ def initialize_client
20
+ @client = HelpScoutDocs::Client.new(@options) unless defined?(@client) && @client.hash == @options.hash
21
+ @client
22
+ end
23
+
24
+ # Has a client been initialized?
25
+ #
26
+ # @return [Boolean]
27
+ #
28
+ def client?
29
+ !!@client
30
+ end
31
+
32
+ private
33
+
34
+ # If we're providing the client, the endpoint may need to be reset set
35
+ # @param [Client]
36
+ # @return [Client]
37
+ #
38
+ def set_endpoint(endpoint)
39
+ @client.instance_variable_set("@endpoint", endpoint)
40
+ end
41
+
42
+ # Are all the required params here?
43
+ # @param [Hash]
44
+ # @param [Array<Symbol>]
45
+ # @return [Boolean]
46
+ #
47
+ def validate_options(options, attrs)
48
+ matched_attrs = options.keys & attrs
49
+ if matched_attrs.to_set != attrs.to_set
50
+ raise HelpScoutDocs::Error::OptionsError.new("#{(attrs - matched_attrs).join(", ")} required options are missing")
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,45 @@
1
+ module HelpScoutDocs
2
+ class Article < HelpScoutDocs::Method
3
+
4
+ # List all articles
5
+ def list(id, type=:category, options={})
6
+ public_send(:"list_by_#{type}", id, options)
7
+ end
8
+
9
+ # List all articles
10
+ #
11
+ def list_by_category(category_id, options={})
12
+ get_request("categories/#{category_id}/articles", options)
13
+ end
14
+
15
+ # List all articles
16
+ #
17
+ def list_by_collection(collection_id, options={})
18
+ get_request("collections/#{collection_id}/articles", options)
19
+ end
20
+
21
+ # Get an article
22
+ #
23
+ def get(id, options={})
24
+ get_request("articles/#{id}", options)
25
+ end
26
+
27
+ # Get a related articles
28
+ #
29
+ def related(id, options={})
30
+ get_request("articles/#{id}/related", options)
31
+ end
32
+
33
+ # Get a article's revisions
34
+ #
35
+ def revisions(id, options={})
36
+ get_request("articles/#{id}/revisions", options)
37
+ end
38
+
39
+ # Get a article's revision
40
+ #
41
+ def get_revision(id, options={})
42
+ get_request("revisions/#{id}", options)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ module HelpScoutDocs
2
+ class Category < HelpScoutDocs::Method
3
+ # List categories
4
+ #
5
+ def list(collection_id, options={})
6
+ get_request("collections/#{collection_id}/categories", options)
7
+ end
8
+ alias :by_collection :list
9
+
10
+ # Get a category
11
+ #
12
+ def get(id, options={})
13
+ get_request("categories/#{id}", options)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module HelpScoutDocs
2
+ class Collection < HelpScoutDocs::Method
3
+ # List collections
4
+ #
5
+ def list(options={})
6
+ get_request("collections", options)
7
+ end
8
+
9
+ # Get a collection
10
+ #
11
+ def get(id, options={})
12
+ get_request("collections/#{id}", options)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ require 'help_scout_docs/method'
2
+ require 'help_scout_docs/methods/article'
3
+ require 'help_scout_docs/methods/category'
4
+ require 'help_scout_docs/methods/collection'
5
+ require 'help_scout_docs/methods/site'
6
+
@@ -0,0 +1,15 @@
1
+ module HelpScoutDocs
2
+ class Site < HelpScoutDocs::Method
3
+ # List sites
4
+ #
5
+ def list(options={})
6
+ get_request("sites", options)
7
+ end
8
+
9
+ # Get a site
10
+ #
11
+ def get(id, options={})
12
+ get_request("sites/#{id}", options)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'faraday_middleware'
2
+ require 'json/ext'
3
+
4
+ module HelpScoutDocs
5
+ module Response
6
+ class ParseJson < ::FaradayMiddleware::ParseJson
7
+ def on_complete(env)
8
+ status = env[:status]
9
+ if status == 403 || status == 401
10
+ error = env[:body].fetch(:error, "Unknown Help Scout Docs API error")
11
+ raise Error::AuthenticationError.new(error)
12
+ elsif status == 404
13
+ raise Error::ResourceNotFoundError.new("Help Scout Docs Not found")
14
+ elsif status =~ /^5/
15
+ raise Error::ApiException.new("Help Scout Docs call unsuccessful. Try again later.")
16
+ end
17
+ end
18
+
19
+ def call(env)
20
+ @app.call(env).on_complete do |environment|
21
+ environment[:body] = parse(environment[:body])
22
+ on_complete(environment)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def parse(body)
29
+ JSON.parse(body, symbolize_names: true)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ module HelpScoutDocs
2
+ class Result
3
+ attr_reader :response
4
+
5
+ # Initialize a new Response, throw exception for fatal errors
6
+ # @param [Hash] result
7
+ #
8
+ def initialize(response)
9
+ @response = response
10
+ end
11
+
12
+ # Was it successful?
13
+ # @return [Boolean]
14
+ #
15
+ def success?
16
+ !error?
17
+ end
18
+
19
+ # Was there an error?
20
+ # @return [Boolean]
21
+ #
22
+ def error?
23
+ response.is_a?(Hash) ? response.has_key?(:status) : false
24
+ end
25
+
26
+ def message
27
+ return unless error?
28
+ response.fetch(:message)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module HelpScoutDocs
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://<api-key>:<api-password>@docsapi.helpscout.net/v1/articles/54027853e4b019254d1ec268
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Accept:
13
+ - application/json
14
+ Accept-Charset:
15
+ - utf-8
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Sun, 31 Aug 2014 19:02:48 GMT
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Access-Control-Allow-Headers:
28
+ - Authorization
29
+ Access-Control-Allow-Methods:
30
+ - GET, POST, PUT, DELETE
31
+ Access-Control-Allow-Credentials:
32
+ - 'true'
33
+ Content-Type:
34
+ - application/json; charset=utf-8
35
+ Vary:
36
+ - Accept-Encoding,User-Agent
37
+ Content-Length:
38
+ - '331'
39
+ "<api-password>-Frame-Options":
40
+ - SAMEORIGIN
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"article":{"id":"54027853e4b019254d1ec268","number":149,"collectionId":"540221e9e4b019254d1ec25b","slug":"article","status":"notpublished","hasDraft":true,"name":"Article","text":"<p>Revision</p>","categories":["540227d7e4b003be53bdcd28"],"related":null,"publicUrl":"http://rubydocsapi.helpscoutdocs.com/article/149-article","popularity":0.0,"keywords":null,"viewCount":0,"createdBy":20055,"updatedBy":20055,"createdAt":"2014-08-31T01:20:19Z","updatedAt":"2014-08-31T02:48:48Z","lastPublishedAt":null}}'
44
+ http_version:
45
+ recorded_at: Sun, 31 Aug 2014 19:02:48 GMT
46
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://<api-key>:<api-password>@docsapi.helpscout.net/v1/articles/54027853e4b019254d1ec268
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Accept:
13
+ - application/json
14
+ Accept-Charset:
15
+ - utf-8
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Sun, 31 Aug 2014 19:02:48 GMT
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Access-Control-Allow-Headers:
28
+ - Authorization
29
+ Access-Control-Allow-Methods:
30
+ - GET, POST, PUT, DELETE
31
+ Access-Control-Allow-Credentials:
32
+ - 'true'
33
+ Content-Type:
34
+ - application/json; charset=utf-8
35
+ Vary:
36
+ - Accept-Encoding,User-Agent
37
+ Content-Length:
38
+ - '331'
39
+ "<api-password>-Frame-Options":
40
+ - SAMEORIGIN
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"article":{"id":"54027853e4b019254d1ec268","number":149,"collectionId":"540221e9e4b019254d1ec25b","slug":"article","status":"notpublished","hasDraft":true,"name":"Article","text":"<p>Revision</p>","categories":["540227d7e4b003be53bdcd28"],"related":null,"publicUrl":"http://rubydocsapi.helpscoutdocs.com/article/149-article","popularity":0.0,"keywords":null,"viewCount":0,"createdBy":20055,"updatedBy":20055,"createdAt":"2014-08-31T01:20:19Z","updatedAt":"2014-08-31T02:48:48Z","lastPublishedAt":null}}'
44
+ http_version:
45
+ recorded_at: Sun, 31 Aug 2014 19:02:48 GMT
46
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://<api-key>:<api-password>@docsapi.helpscout.net/v1/categories/540227d7e4b003be53bdcd28/articles
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Accept:
13
+ - application/json
14
+ Accept-Charset:
15
+ - utf-8
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Sun, 31 Aug 2014 19:02:47 GMT
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Access-Control-Allow-Headers:
28
+ - Authorization
29
+ Access-Control-Allow-Methods:
30
+ - GET, POST, PUT, DELETE
31
+ Access-Control-Allow-Credentials:
32
+ - 'true'
33
+ Content-Type:
34
+ - application/json; charset=utf-8
35
+ Vary:
36
+ - Accept-Encoding,User-Agent
37
+ Content-Length:
38
+ - '292'
39
+ "<api-password>-Frame-Options":
40
+ - SAMEORIGIN
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"articles":{"page":1,"pages":1,"count":1,"items":[{"id":"54027853e4b019254d1ec268","number":149,"collectionId":"540221e9e4b019254d1ec25b","slug":"article","status":"notpublished","hasDraft":true,"name":"Article","publicUrl":"http://rubydocsapi.helpscoutdocs.com/article/149-article","popularity":0.0,"viewCount":0,"createdBy":20055,"updatedBy":20055,"createdAt":"2014-08-31T01:20:19Z","updatedAt":"2014-08-31T02:48:48Z","lastPublishedAt":null}]}}'
44
+ http_version:
45
+ recorded_at: Sun, 31 Aug 2014 19:02:47 GMT
46
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,47 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://<api-key>:<api-password>@docsapi.helpscout.net/v1/collections/540221e9e4b019254d1ec25b/articles
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Accept:
13
+ - application/json
14
+ Accept-Charset:
15
+ - utf-8
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Sun, 31 Aug 2014 19:02:47 GMT
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Access-Control-Allow-Headers:
28
+ - Authorization
29
+ Access-Control-Allow-Methods:
30
+ - GET, POST, PUT, DELETE
31
+ Access-Control-Allow-Credentials:
32
+ - 'true'
33
+ Content-Type:
34
+ - application/json; charset=utf-8
35
+ Vary:
36
+ - Accept-Encoding,User-Agent
37
+ Content-Length:
38
+ - '360'
39
+ "<api-password>-Frame-Options":
40
+ - SAMEORIGIN
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"articles":{"page":1,"pages":1,"count":2,"items":[{"id":"54027853e4b019254d1ec268","number":149,"collectionId":"540221e9e4b019254d1ec25b","slug":"article","status":"notpublished","hasDraft":true,"name":"Article","publicUrl":"http://rubydocsapi.helpscoutdocs.com/article/149-article","popularity":0.0,"viewCount":0,"createdBy":20055,"updatedBy":20055,"createdAt":"2014-08-31T01:20:19Z","updatedAt":"2014-08-31T02:48:48Z","lastPublishedAt":null},{"id":"54028cf8e4b003be53bdcd2e","number":150,"collectionId":"540221e9e4b019254d1ec25b","slug":"another-article","status":"notpublished","hasDraft":false,"name":"Another
44
+ article","publicUrl":"http://rubydocsapi.helpscoutdocs.com/article/150-another-article","popularity":0.0,"viewCount":0,"createdBy":20055,"updatedBy":20055,"createdAt":"2014-08-31T02:48:24Z","updatedAt":"2014-08-31T02:48:31Z","lastPublishedAt":"2014-08-31T02:48:31Z"}]}}'
45
+ http_version:
46
+ recorded_at: Sun, 31 Aug 2014 19:02:47 GMT
47
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://<api-key>:<api-password>@docsapi.helpscout.net/v1/articles/54027853e4b019254d1ec268/related
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Accept:
13
+ - application/json
14
+ Accept-Charset:
15
+ - utf-8
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Sun, 31 Aug 2014 19:02:48 GMT
25
+ Access-Control-Allow-Origin:
26
+ - "*"
27
+ Access-Control-Allow-Headers:
28
+ - Authorization
29
+ Access-Control-Allow-Methods:
30
+ - GET, POST, PUT, DELETE
31
+ Access-Control-Allow-Credentials:
32
+ - 'true'
33
+ Content-Type:
34
+ - application/json; charset=utf-8
35
+ Vary:
36
+ - Accept-Encoding,User-Agent
37
+ Content-Length:
38
+ - '66'
39
+ "<api-password>-Frame-Options":
40
+ - SAMEORIGIN
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"articles":{"page":1,"pages":1,"count":0,"items":[]}}'
44
+ http_version:
45
+ recorded_at: Sun, 31 Aug 2014 19:02:49 GMT
46
+ recorded_with: VCR 2.9.0