cola_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cola_client.rb +83 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 27170afc63b8c6eb81ffb71b8c8815e230c15266ff0d425385f709b0e7f9820b
4
+ data.tar.gz: e06888111a16d76f36faacfe3c5764d231337aaa8e48e48110d37e298604d0eb
5
+ SHA512:
6
+ metadata.gz: 718c9bd20a172f9a7da3897cdbc554cbe996b4b6372aef2c0131678d267462d30380a9b6992e0aa894155e35af5d58b723a9e3f95400d2ebc603b8705b0ac1d0
7
+ data.tar.gz: 2fa8dbee0936ef53cb669478a25518e6ce0fb3f4c281d23278dd70f131eabb6fbe3d29c0837ccec0da3874cd106b1fb99c6df9461e4d435d12688d31686a3def
@@ -0,0 +1,83 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+
6
+ class ColaClient
7
+ MEDIA_TYPES = [:form, :json]
8
+ METHODS = [:get, :post, :patch, :put, :delete]
9
+
10
+ CONTENT_TYPE_HEADER = 'Content-Type'
11
+
12
+
13
+ def self.go(method, url, query: nil, body: nil, headers: {}, options: {})
14
+ uri = URI(url)
15
+ uri.query = URI.encode_www_form(query) if query
16
+
17
+ http = Net::HTTP.new(uri.host, uri.port)
18
+ http.use_ssl = true if uri.scheme.match(/^https/)
19
+
20
+ payload = nil
21
+
22
+ if body
23
+ case options[:content_type]
24
+ when :form
25
+ payload = URI.encode_www_form body
26
+ headers[CONTENT_TYPE_HEADER] = 'application/x-www-form-urlencoded'
27
+ when :json
28
+ payload = body.to_json
29
+ headers[CONTENT_TYPE_HEADER] = 'application/json'
30
+ else
31
+ headers[CONTENT_TYPE_HEADER] = 'text/plain'
32
+ payload = body.to_s
33
+ end
34
+ end
35
+
36
+ if options[:basic_auth]
37
+ headers.merge! basic_auth options[:basic_auth]
38
+ end
39
+
40
+ response = http.send_request(method.to_s.upcase, uri.request_uri, payload, headers)
41
+
42
+ if options[:accept] == :json
43
+ response.body = JSON.parse(response.body, symbolize_names: true) unless response.body.empty?
44
+ end
45
+
46
+ response
47
+ end
48
+
49
+ def self.method_missing method, *args
50
+ if METHODS.include? method.to_sym
51
+ args.unshift method.to_sym
52
+ self.send :go, *args
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def self.basic_auth(user_pass)
59
+ credentials = "#{user_pass[:user]}:#{user_pass[:pass]}"
60
+ encoded = Base64.strict_encode64(credentials)
61
+
62
+ {
63
+ authorization: "Basic #{encoded}"
64
+ }
65
+ end
66
+ end
67
+
68
+
69
+
70
+ # params = {
71
+ # headers: {:haha => 'haha2'},
72
+ # query: {b: 'c'},
73
+ # body: {a: :b},
74
+ # options: {
75
+ # content_type: :json,
76
+ # accept: :json,
77
+ # basic_auth: {
78
+ # user: 'bla',
79
+ # pass: 'bla2'
80
+ # }
81
+ # }
82
+ # }
83
+ # p ColaClient.get('http://localhost:4567/some/path', params)
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cola_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jozef Vasek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple http/s client
14
+ email: jozef.vasek@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cola_client.rb
20
+ homepage: https://rubygems.org/gems/cola_client
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.1.6
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Simple http/s client
43
+ test_files: []