cobot_client 4.0.0 → 6.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.
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CobotClient
4
+ class Response
5
+ def initialize(net_http_response)
6
+ @net_http_response = net_http_response
7
+ end
8
+
9
+ def body
10
+ @net_http_response.body
11
+ end
12
+
13
+ def headers
14
+ @net_http_response.to_hash
15
+ end
16
+
17
+ def code
18
+ Integer(@net_http_response.code)
19
+ end
20
+
21
+ def parsed_body
22
+ return if !success? || code == 204
23
+
24
+ JSON.parse(@net_http_response.body, symbolize_names: true)
25
+ end
26
+
27
+ def client_error?
28
+ (400..499).cover?(code)
29
+ end
30
+
31
+ def server_error?
32
+ (500..599).cover?(code)
33
+ end
34
+
35
+ def success?
36
+ (200..299).cover?(code)
37
+ end
38
+
39
+ def to_error
40
+ return if success?
41
+
42
+ ResponseError.build(@net_http_response.message, response: self)
43
+ end
44
+ end
45
+ end
@@ -1,28 +1,31 @@
1
- require 'uri'
1
+ # frozen_string_literal: true
2
2
 
3
- module CobotClient::UrlHelper
4
- # set this to override the site for accessing the cobot api
3
+ module CobotClient
4
+ module UrlHelper
5
+ DEFAULT_SITE = 'https://www.cobot.me'
5
6
 
6
- @@site = 'https://www.cobot.me'
7
- def self.site
8
- @@site
9
- end
7
+ # set this to override the site for accessing the cobot api
8
+ def self.site
9
+ @site || DEFAULT_SITE
10
+ end
10
11
 
11
- def self.site=(site)
12
- @@site = site
13
- end
12
+ def self.site=(site)
13
+ @site = site
14
+ end
14
15
 
15
- # generates a url to access the cobot api
16
- # see the spec for usage examples
17
- def cobot_url(subdomain = 'www', *path_options)
18
- path = path_options.first.is_a?(String) ? path_options.first : '/'
19
- options = path_options.find{|p| p.is_a?(Hash)} || {}
16
+ # generates a uri to access the cobot api
17
+ # see the spec for usage examples
18
+ def cobot_uri(subdomain = 'www', path = '/', params: {}, **)
19
+ uri = URI.parse(CobotClient::UrlHelper.site)
20
+ uri.host = uri.host.split('.').tap { |parts| parts[0] = subdomain }.join('.')
21
+ uri.path = path
22
+ uri.query = URI.encode_www_form(params) unless params.empty?
20
23
 
21
- url = URI.parse(CobotClient::UrlHelper.site)
22
- url.host = url.host.split('.').tap{|parts| parts[0] = subdomain}.join('.')
23
- url.path = path
24
- url.query = URI.encode_www_form(options[:params]) if options[:params] && options[:params].any?
24
+ uri
25
+ end
25
26
 
26
- url.to_s
27
+ def cobot_url(subdomain = 'www', path = '/', params: {}, **)
28
+ cobot_uri(subdomain, path, params: params, **).to_s
29
+ end
27
30
  end
28
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CobotClient
2
- VERSION = '4.0.0'
4
+ VERSION = '6.0.0'
3
5
  end
data/lib/cobot_client.rb CHANGED
@@ -1,11 +1,20 @@
1
- require "cobot_client/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ require 'active_model'
8
+
9
+ require 'cobot_client/version'
2
10
  require 'cobot_client/engine' if defined?(Rails)
3
- require 'cobot_client/exceptions'
11
+ require 'cobot_client/errors'
4
12
 
5
13
  module CobotClient
6
14
  autoload :ApiClient, 'cobot_client/api_client'
7
15
  autoload :NavigationLink, 'cobot_client/navigation_link'
8
16
  autoload :NavigationLinkService, 'cobot_client/navigation_link_service'
17
+ autoload :Response, 'cobot_client/response'
18
+ autoload :Request, 'cobot_client/request'
9
19
  autoload :UrlHelper, 'cobot_client/url_helper'
10
- autoload :XdmHelper, 'cobot_client/xdm_helper'
11
20
  end
@@ -0,0 +1,20 @@
1
+ # Download sources
2
+ sources:
3
+ - type: git
4
+ name: ruby/gem_rbs_collection
5
+ remote: https://github.com/ruby/gem_rbs_collection.git
6
+ revision: main
7
+ repo_dir: gems
8
+
9
+ # You can specify local directories as sources also.
10
+ # - type: local
11
+ # path: path/to/your/local/repository
12
+
13
+ # A directory to install the downloaded RBSs
14
+ path: .gem_rbs_collection
15
+
16
+ gems:
17
+ - name: auth-sanitizer
18
+ ignore: true # See: https://github.com/ruby-oauth/auth-sanitizer/pull/5
19
+ - name: snaky_hash
20
+ ignore: true # See: https://github.com/ruby-oauth/snaky_hash/pull/11
@@ -0,0 +1,51 @@
1
+ module CobotClient
2
+ class ApiClient
3
+ type headers = Hash[String, String]
4
+ type path = String
5
+ type request_params = parsed_json_object
6
+ type request_payload = (request_params | Array[request_params])
7
+ type parsed_json_object = Hash[Symbol, untyped]
8
+ type parsed_response_body = (parsed_json_object | Array[parsed_json_object])?
9
+ type subdomain = String
10
+ type url = String
11
+
12
+ @access_token: String
13
+
14
+ attr_accessor self.retry_time: Integer
15
+ attr_accessor self.user_agent: String
16
+
17
+ def initialize: (String) -> void
18
+
19
+ def delete:
20
+ (subdomain, path) -> Response |
21
+ (url) -> Response
22
+
23
+ def get:
24
+ (subdomain, path, ?request_payload) -> parsed_response_body |
25
+ (url, ?request_payload) -> parsed_response_body
26
+
27
+ def patch:
28
+ (subdomain, path, ?request_payload) -> parsed_response_body |
29
+ (url, ?request_payload) -> parsed_response_body
30
+
31
+ def post:
32
+ (subdomain, path, ?request_payload) -> parsed_response_body |
33
+ (url, ?request_payload) -> parsed_response_body
34
+
35
+ def put:
36
+ (subdomain, path, ?request_payload) -> parsed_response_body |
37
+ (url, ?request_payload) -> parsed_response_body
38
+
39
+ private
40
+
41
+ def headers: -> headers
42
+
43
+ def request:
44
+ (Request::verb, subdomain, path, ?request_payload) -> Response |
45
+ (Request::verb, url, ?request_payload) -> Response
46
+
47
+ def retry_errors: [R] () { () -> R } -> R
48
+
49
+ def rewrap_errors: () { () -> Response } -> Response
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module CobotClient
2
+ class Error < StandardError
3
+ end
4
+
5
+ class ConnectionError < StandardError
6
+ end
7
+
8
+ class ResponseError < Error
9
+ HTTP_CODE: Integer?
10
+
11
+ def self.build: (String?, response: Response) -> ResponseError
12
+
13
+ attr_reader response: Response?
14
+
15
+ def initialize: (String?, ?response: Response?) -> void
16
+
17
+ def http_body: -> String?
18
+
19
+ def http_code: -> Integer?
20
+ end
21
+
22
+ RESPONSE_CODE_TO_ERROR_CLASS: Hash[Integer, singleton(ResponseError)]
23
+ end
@@ -0,0 +1,11 @@
1
+ module CobotClient
2
+ class NavigationLink
3
+ attr_accessor section: String
4
+ attr_accessor label: String
5
+ attr_accessor iframe_url: String
6
+ attr_accessor user_url: String
7
+ attr_accessor user_editable: bool
8
+
9
+ def initialize: (Hash[Symbol, untyped]) -> void
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module CobotClient
2
+ class NavigationLinkService
3
+ @api_client: ApiClient
4
+
5
+ @subdomain: String
6
+
7
+ def initialize: (ApiClient, String) -> void
8
+
9
+ def install_links: (Array[NavigationLink]) -> Array[NavigationLink]
10
+
11
+ private
12
+
13
+ def create_link: (NavigationLink) -> NavigationLink
14
+
15
+ def fetch_links: -> Array[NavigationLink]
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ module CobotClient
2
+ class Request
3
+ type verb = Symbol
4
+
5
+ CONTENT_TYPE_HEADER: ApiClient::headers
6
+ VERBS: Array[verb]
7
+
8
+ @http: Net::HTTP
9
+
10
+ attr_reader body: String?
11
+ attr_reader headers: ApiClient::headers
12
+ attr_reader uri: URI::Generic
13
+ attr_reader verb: verb
14
+
15
+ def initialize:
16
+ (verb, ApiClient::subdomain, ApiClient::path, ?ApiClient::request_payload) -> void |
17
+ (verb, ApiClient::url, ?ApiClient::request_payload) -> void
18
+
19
+ def headers=: (ApiClient::headers) -> ApiClient::headers
20
+
21
+ def submit: -> Response
22
+
23
+ private
24
+
25
+ def build_uri: (ApiClient::subdomain | ApiClient::url, ApiClient::path?, **untyped) -> URI::Generic
26
+
27
+ def http: -> Net::HTTP
28
+
29
+ def net_http_request: -> Net::HTTPRequest
30
+
31
+ def parse_args:
32
+ (ApiClient::url) -> [ApiClient::url, nil, nil, ApiClient::request_payload] |
33
+ (ApiClient::url, ApiClient::request_payload) -> [ApiClient::url, nil, nil, ApiClient::request_payload] |
34
+ (ApiClient::subdomain, ApiClient::path) -> [nil, ApiClient::subdomain, ApiClient::path, ApiClient::request_payload] |
35
+ (ApiClient::subdomain, ApiClient::path, ApiClient::request_payload) -> [nil, ApiClient::subdomain, ApiClient::path, ApiClient::request_payload]
36
+
37
+ def request_class: -> singleton(Net::HTTPRequest)
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ module CobotClient
2
+ class Response
3
+ @net_http_response: Net::HTTPResponse
4
+
5
+ def initialize: (Net::HTTPResponse) -> void
6
+
7
+ def body: -> String
8
+
9
+ def client_error?: -> bool
10
+
11
+ def code: -> Integer
12
+
13
+ def headers: -> ApiClient::headers
14
+
15
+ def parsed_body: -> ApiClient::parsed_response_body
16
+
17
+ def server_error?: -> bool
18
+
19
+ def success?: -> bool
20
+
21
+ def to_error: -> ResponseError?
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module CobotClient
2
+ module UrlHelper
3
+ DEFAULT_SITE: String
4
+
5
+ self.@site: String
6
+
7
+ def self.site: -> String
8
+
9
+ def self.site=: (String) -> String
10
+
11
+ def cobot_uri:
12
+ () -> URI::Generic |
13
+ (ApiClient::subdomain) -> URI::Generic |
14
+ (ApiClient::subdomain, params: ApiClient::request_payload) -> URI::Generic |
15
+ (ApiClient::subdomain, ApiClient::path) -> URI::Generic |
16
+ (ApiClient::subdomain, ApiClient::path, params: ApiClient::request_payload) -> URI::Generic
17
+
18
+ def cobot_url:
19
+ () -> String |
20
+ (ApiClient::subdomain) -> String |
21
+ (ApiClient::subdomain, params: ApiClient::request_payload) -> String |
22
+ (ApiClient::subdomain, ApiClient::path) -> String |
23
+ (ApiClient::subdomain, ApiClient::path, params: ApiClient::request_payload) -> String
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module CobotClient
2
+ VERSION: String
3
+ end
@@ -0,0 +1,4 @@
1
+ dependencies:
2
+ - name: json
3
+ - name: net/http
4
+ - name: uri