xlogin-apiclient 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 83943d818f1f52aed3bb0d97f36d52473e7a1f42
4
- data.tar.gz: 399db00e2c8f0dc6f9a74ad3132a6a37433dfec8
2
+ SHA256:
3
+ metadata.gz: 18a35f6ba98b58373950e71f3c3a87683087981acc457099952fe85beb474957
4
+ data.tar.gz: 6501f3d09a773b2925e779d5c72a7905e9871a0e002bb99560bea537b8747cdd
5
5
  SHA512:
6
- metadata.gz: 44ee7f3f4a7ddbb964d0fa58a3161f83c81ab90cd704f8734fd0c4444821cfaa8b97bfad0b22e3142318f4332827bc03814909a3e13d95c1eba25fe6db19f375
7
- data.tar.gz: 25603c911d6daf0f972bd658e071129ed84a890c666f3026f657d3c1e2e421a802b92895bc20fe32ca4a0419f9dd8194df36c4675b331ed02a863f6a74fe0a4d
6
+ metadata.gz: 84722296fb0c543e96fe23629845694c76a96dbac5f6d7812b3daf3022374163f4e389110b772931517e43fa895fe3baac23e03dbd8908fadaf020986d72d41e
7
+ data.tar.gz: 0ac161f727446451c8981e66e80c4c69be3821eab9cf7ad6998b95738cfba11cde028f9fa75e8d51e08f2fff16b73e5ae9c2327c762918ec7e4f66427530a800
@@ -1,78 +1,44 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
- require 'ostruct'
3
+ require 'addressable/uri'
4
4
  require "xlogin/apiclient/version"
5
5
 
6
6
  module Xlogin
7
- module APIClient
8
- class APIError < StandardError; end
7
+ class APIClient
8
+ class Error < StandardError; end
9
9
 
10
- class Client
11
- def initialize(base_uri, type:, **args)
12
- @uri = base_uri
13
- @type = type
14
- @args = args
15
- end
16
-
17
- def cmd(req = Request.new, &block)
18
- if req.kind_of?(String)
19
- req, _req = Request.new, req
20
- req.command = _req
21
- end
22
-
23
- req.xlogin = @args
24
- block.call(req) if block
25
-
26
- uri = @uri.dup
27
- uri.path += "/vendors/#{@type}/actions"
28
-
29
- http(:post, uri, req.to_h)
30
- rescue => e
31
- raise APIError.new(e.message)
32
- end
33
-
34
- private
35
- def http(method, uri, **args)
36
- http = Net::HTTP.new(uri.host, uri.port)
37
-
38
- klass = Net::HTTP.const_get(method.to_s.capitalize)
39
- req = klass.new(uri.path)
40
- req["Accept"] = "application/json"
41
-
42
- unless args.empty?
43
- req["Content-Type"] = "application/json"
44
- req.body = JSON.dump(args)
45
- end
10
+ class << self
11
+ attr_accessor :base_url
12
+ end
46
13
 
47
- resp = http.request(req)
48
- body = resp.body
14
+ def initialize(base_url: self.class.base_url, **args)
15
+ raise Error.new('base_url not defined') unless base_url
49
16
 
50
- Response.new(body.nil? ? {} : JSON.parse(body))
51
- end
17
+ @uri = Addressable::URI.parse(base_url)
18
+ @type = args.delete(:type)
19
+ @args = args
52
20
  end
53
21
 
54
- class Factory
55
- def initialize(uri)
56
- @base_uri = URI(uri)
57
- end
58
-
59
- def create(**args)
60
- Client.new(@base_uri, args)
61
- end
22
+ def cmd(args)
23
+ params = {driver: @type, target: @args, command: args}
24
+ resp = request(**params.transform_keys(&:to_sym))
25
+ resp['payload'].join
62
26
  end
63
27
 
64
- class Request < OpenStruct
65
- def initialize(**args)
66
- super(*args)
67
- self.captures ||= []
68
- end
28
+ private
29
+ def request(**params)
30
+ uri = @uri.dup
31
+ uri.query = "q=#{URI.encode_www_form_component(JSON.generate(params))}"
69
32
 
70
- def to_h
71
- super.delete_if { |k, v| k == :captures && v.empty? }
72
- end
73
- end
33
+ req = Net::HTTP::Get.new(uri.request_uri)
34
+ req["Accept"] = "application/json"
35
+
36
+ http = Net::HTTP.new(uri.host, uri.port)
37
+ body = http.request(req).body
38
+ resp = body ? JSON.parse(body) : {}
39
+ return resp if resp['success']
74
40
 
75
- class Response < OpenStruct
41
+ raise Error.new(resp['error'])
76
42
  end
77
43
  end
78
44
  end
@@ -1,5 +1,5 @@
1
1
  module Xlogin
2
2
  module Apiclient
3
- VERSION = "0.1.6"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_development_dependency "bundler", "~> 1.17"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
+
29
+ spec.add_dependency 'addressable'
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlogin-apiclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-05 00:00:00.000000000 Z
11
+ date: 2021-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: addressable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: API client for xlogin-api.
42
56
  email:
43
57
  - haccht@users.noreply.github.com
@@ -75,8 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
89
  - !ruby/object:Gem::Version
76
90
  version: '0'
77
91
  requirements: []
78
- rubyforge_project:
79
- rubygems_version: 2.6.14.1
92
+ rubygems_version: 3.0.3
80
93
  signing_key:
81
94
  specification_version: 4
82
95
  summary: API client for xlogin-api.