hik_openapi 0.1.2 → 0.2

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
2
  SHA256:
3
- metadata.gz: b8dfc6814b2cc4c658722f6ff00dbb37eca887895ac9dad161204090908594ad
4
- data.tar.gz: d3d66653342ad868ea0c555298ba4c5b4fb03c5e25454b6ed8ca60b59855c52e
3
+ metadata.gz: 124810a1613f0b8e8ae9491dd535697611763308962dc61dd9dc07ddb62d4a9a
4
+ data.tar.gz: fe2cbb8f1f6f87b607bc5ce1c1886c9d28553b47593a50af7e0f5ba7e73b77bf
5
5
  SHA512:
6
- metadata.gz: d79392c09b986e6f85a86dc7c42558bbc5b01707c8d7d162d061bca66b7b3d95b757f29186c2a92ddd7f0e5e601ed1b8b44e4404c5e640e5784a9477cc3f8ebc
7
- data.tar.gz: 46aecdb8520b6580c99adaaff7e41a240270bf153cec2f3c170865537d71ac4e9b4627d4153c362c9e59c365cf3cbffb4520ca4caa12cd00340eed6eb477ccb9
6
+ metadata.gz: a7f1722cf24803942375ffc473416d40d2dee05e5e4918948b65ef635bf8d7475967c1831d77573338e607f30a5ec566f685bafd5cec9e010591987ea09cc2df
7
+ data.tar.gz: b2d14d7d2419a793c9b5d05ead14adf205a241afc81ef3427f35c6d382d40633ecde524341cdfa212a47a41fc87df79e6efd20cedb68575d921d3d5db06f346e
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+ hik_openapi-*.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,67 @@
1
+ AllCops:
2
+ NewCops: disable
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 2.4
5
+
6
+ Layout/AccessModifierIndentation:
7
+ EnforcedStyle: outdent
8
+
9
+ Layout/LineLength:
10
+ AllowURI: true
11
+ Enabled: false
12
+
13
+ Layout/SpaceInsideHashLiteralBraces:
14
+ EnforcedStyle: no_space
15
+
16
+ Lint/Void:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Max: 36
21
+ Exclude:
22
+ - spec/**/*.rb
23
+
24
+ Metrics/BlockNesting:
25
+ Max: 2
26
+
27
+ Metrics/MethodLength:
28
+ CountComments: false
29
+ Max: 10
30
+
31
+ Metrics/ModuleLength:
32
+ Max: 150
33
+
34
+ Metrics/ParameterLists:
35
+ Max: 5
36
+ MaxOptionalParameters: 5
37
+ CountKeywordArgs: true
38
+
39
+ Style/AsciiComments:
40
+ Enabled: false
41
+
42
+ Style/CollectionMethods:
43
+ Enabled: true
44
+ PreferredMethods:
45
+ map: 'collect'
46
+ map!: 'collect!'
47
+ reduce: 'inject'
48
+ find: 'detect'
49
+ find_all: 'select'
50
+
51
+ Style/Documentation:
52
+ Enabled: false
53
+
54
+ Style/DoubleNegation:
55
+ Enabled: false
56
+
57
+ Style/FrozenStringLiteralComment:
58
+ Enabled: false
59
+
60
+ Style/NumericPredicate:
61
+ Enabled: false
62
+
63
+ Style/TrailingCommaInArrayLiteral:
64
+ EnforcedStyleForMultiline: 'comma'
65
+
66
+ Style/TrailingCommaInHashLiteral:
67
+ EnforcedStyleForMultiline: 'comma'
data/Gemfile CHANGED
@@ -1,7 +1,12 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in hik_openapi.gemspec
4
- gemspec
3
+ gem 'rake', '~> 12.0'
4
+
5
+ group :development, :test do
6
+ gem 'pry'
7
+ gem 'guard-rspec', require: false
8
+ gem 'rspec', '~> 3.0'
9
+ gem 'rubocop'
10
+ end
5
11
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
12
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,13 +1,71 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hik_openapi (0.1.2)
4
+ hik_openapi (0.2)
5
+ http (~> 4.4.1)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ ast (2.4.2)
13
+ coderay (1.1.3)
9
14
  diff-lcs (1.4.4)
15
+ domain_name (0.5.20190701)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ ffi (1.15.0)
18
+ ffi-compiler (1.0.1)
19
+ ffi (>= 1.0.0)
20
+ rake
21
+ formatador (0.2.5)
22
+ guard (2.16.2)
23
+ formatador (>= 0.2.4)
24
+ listen (>= 2.7, < 4.0)
25
+ lumberjack (>= 1.0.12, < 2.0)
26
+ nenv (~> 0.1)
27
+ notiffany (~> 0.0)
28
+ pry (>= 0.9.12)
29
+ shellany (~> 0.0)
30
+ thor (>= 0.18.1)
31
+ guard-compat (1.2.1)
32
+ guard-rspec (4.7.3)
33
+ guard (~> 2.1)
34
+ guard-compat (~> 1.1)
35
+ rspec (>= 2.99.0, < 4.0)
36
+ http (4.4.1)
37
+ addressable (~> 2.3)
38
+ http-cookie (~> 1.0)
39
+ http-form_data (~> 2.2)
40
+ http-parser (~> 1.2.0)
41
+ http-cookie (1.0.3)
42
+ domain_name (~> 0.5)
43
+ http-form_data (2.3.0)
44
+ http-parser (1.2.3)
45
+ ffi-compiler (>= 1.0, < 2.0)
46
+ listen (3.5.0)
47
+ rb-fsevent (~> 0.10, >= 0.10.3)
48
+ rb-inotify (~> 0.9, >= 0.9.10)
49
+ lumberjack (1.2.8)
50
+ method_source (1.0.0)
51
+ nenv (0.3.0)
52
+ notiffany (0.1.3)
53
+ nenv (~> 0.1)
54
+ shellany (~> 0.0)
55
+ parallel (1.20.1)
56
+ parser (3.0.0.0)
57
+ ast (~> 2.4.1)
58
+ pry (0.14.0)
59
+ coderay (~> 1.1)
60
+ method_source (~> 1.0)
61
+ public_suffix (4.0.6)
62
+ rainbow (3.0.0)
10
63
  rake (12.3.3)
64
+ rb-fsevent (0.10.4)
65
+ rb-inotify (0.10.1)
66
+ ffi (~> 1.0)
67
+ regexp_parser (2.1.1)
68
+ rexml (3.2.4)
11
69
  rspec (3.9.0)
12
70
  rspec-core (~> 3.9.0)
13
71
  rspec-expectations (~> 3.9.0)
@@ -21,14 +79,35 @@ GEM
21
79
  diff-lcs (>= 1.2.0, < 2.0)
22
80
  rspec-support (~> 3.9.0)
23
81
  rspec-support (3.9.3)
82
+ rubocop (1.11.0)
83
+ parallel (~> 1.10)
84
+ parser (>= 3.0.0.0)
85
+ rainbow (>= 2.2.2, < 4.0)
86
+ regexp_parser (>= 1.8, < 3.0)
87
+ rexml
88
+ rubocop-ast (>= 1.2.0, < 2.0)
89
+ ruby-progressbar (~> 1.7)
90
+ unicode-display_width (>= 1.4.0, < 3.0)
91
+ rubocop-ast (1.4.1)
92
+ parser (>= 2.7.1.5)
93
+ ruby-progressbar (1.11.0)
94
+ shellany (0.0.1)
95
+ thor (1.1.0)
96
+ unf (0.1.4)
97
+ unf_ext
98
+ unf_ext (0.0.7.7)
99
+ unicode-display_width (2.0.0)
24
100
 
25
101
  PLATFORMS
26
102
  ruby
27
103
 
28
104
  DEPENDENCIES
105
+ guard-rspec
29
106
  hik_openapi!
107
+ pry
30
108
  rake (~> 12.0)
31
109
  rspec (~> 3.0)
110
+ rubocop
32
111
 
33
112
  BUNDLED WITH
34
113
  2.1.4
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch(%r{^lib/rest/(.+)\.rb$}) { |m| "spec/lib/rest/#{m[1]}_spec.rb" }
5
+ watch('spec/spec_helper.rb') { 'spec' }
6
+ end
data/README.md CHANGED
@@ -1,37 +1,22 @@
1
- # HikOpenapi
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hik_openapi`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'hik_openapi'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install hik_openapi
22
-
23
1
  ## Usage
24
2
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hik_openapi.
3
+ ```ruby
4
+ @client = HikOpenapi::Client.new do |config|
5
+ config.host = "https://192.168.3.230"
6
+ config.prefix = "/artemis"
7
+ config.app_key = "app_key"
8
+ config.app_secret = "app_secret"
9
+ config.proxy = { host: "127.0.0.1", port: 8123 } if Rails.env.development?
10
+ end
11
+
12
+ params = {
13
+ 'cameraIndexCode': [url],
14
+ 'streamType': 0,
15
+ 'protocol': [video_type],
16
+ 'transmode': 0,
17
+ 'expand': "transcode=0",
18
+ }
19
+
20
+ res = @client.post("/api/video/v1/cameras/previewURLs", params)
36
21
 
37
- # hik_openapi
22
+ ```
data/hik_openapi.gemspec CHANGED
@@ -1,6 +1,8 @@
1
1
  require_relative 'lib/hik_openapi/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
+ spec.add_dependency 'http', '~> 4.4.1'
5
+
4
6
  spec.name = 'hik_openapi'
5
7
  spec.version = HikOpenapi::VERSION
6
8
  spec.authors = ['palytoxin']
@@ -9,7 +11,7 @@ Gem::Specification.new do |spec|
9
11
  spec.summary = 'hikvision openapi for ruby.'
10
12
  spec.description = 'hikvision openapi for ruby.'
11
13
  spec.homepage = 'https://github.com/palytoxin/hik_openapi'
12
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
13
15
 
14
16
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
15
17
 
@@ -17,12 +19,8 @@ Gem::Specification.new do |spec|
17
19
  spec.metadata['source_code_uri'] = 'https://github.com/palytoxin/hik_openapi'
18
20
  spec.metadata['changelog_uri'] = 'https://github.com/palytoxin/hik_openapi'
19
21
 
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
22
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
23
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
24
  end
25
- spec.bindir = 'exe'
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
25
  spec.require_paths = ['lib']
28
26
  end
data/lib/hik_openapi.rb CHANGED
@@ -1,25 +1,96 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'hik_openapi/version'
4
- require 'hik_openapi/configuration'
5
- require 'hik_openapi/api'
6
- require 'hik_openapi/result'
4
+ require 'securerandom'
5
+ require 'openssl'
6
+ require 'http'
7
7
 
8
8
  module HikOpenapi
9
- class Error < StandardError; end
9
+ class Client
10
+ attr_accessor :host, :prefix, :app_key, :app_secret, :proxy, :timeouts
11
+ attr_writer :user_agent
10
12
 
11
- def self.config
12
- return @config if defined?(@config)
13
+ def initialize(options = {})
14
+ options.each do |key, value|
15
+ instance_variable_set("@#{key}", value)
16
+ end
17
+ yield(self) if block_given?
18
+ end
13
19
 
14
- @config = Configuration.new
15
- @config
16
- end
20
+ def get(path, params)
21
+ request(:get, path, params)
22
+ end
17
23
 
18
- def self.setup
19
- yield config
20
- end
24
+ def post(path, params)
25
+ request(:post, path, params)
26
+ end
27
+
28
+ private
29
+
30
+ def request(request_method, path, params)
31
+ @uri = host + prefix + path
32
+ @request_method = request_method
33
+ @path = path
34
+ @params = params
35
+ @options_key = {get: :params, post: :json}[request_method] || :form
36
+ @headers = {'Content-Type': 'application/json', 'Accept': '*/*', 'x-ca-timestamp': (Time.now.to_f * 1000).to_i.to_s, 'x-ca-nonce': SecureRandom.uuid, 'x-ca-key': app_key}
37
+ perform
38
+ end
39
+
40
+ def perform
41
+ ctx = OpenSSL::SSL::SSLContext.new
42
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+
44
+ response = http_client.headers(sign_headers(@headers)).public_send(@request_method, @uri.to_s, request_options.merge(ssl_context: ctx))
45
+ response_body = response.body.empty? ? '' : symbolize_keys!(response.parse)
46
+ response_headers = response.headers
47
+ {
48
+ headers: response_headers,
49
+ body: response_body,
50
+ }
51
+ end
52
+
53
+ def symbolize_keys!(object)
54
+ case object
55
+ when Array
56
+ object.each_with_index { |val, index| object[index] = symbolize_keys!(val) }
57
+ when Hash
58
+ object.dup.each_key { |key| object[key.to_sym] = symbolize_keys!(object.delete(key)) }
59
+ end
60
+ object
61
+ end
62
+
63
+ def timeout_keys_defined
64
+ (%i[write connect read] - (timeouts&.keys || [])).empty?
65
+ end
66
+
67
+ def http_client
68
+ client = proxy ? HTTP.via(*proxy.values_at(:host, :port, :username, :password).compact) : HTTP
69
+ client = client.timeout(connect: timeouts[:connect], read: timeouts[:read], write: timeouts[:write]) if timeout_keys_defined
70
+ client
71
+ end
72
+
73
+ def request_options
74
+ {@options_key => @params}
75
+ end
76
+
77
+ def sign_headers(headers)
78
+ headers.merge!({
79
+ "x-ca-signature": sign(@request_method, (prefix + @path), headers),
80
+ })
81
+ end
82
+
83
+ def sign(method, path, headers)
84
+ sign_str = headers.reject { |a| a.to_s.start_with?('x-ca') }.values.sort
85
+ sign_str = sign_str.unshift(method.to_s.upcase).push(path).join("\n")
21
86
 
22
- def self.api
23
- Api
87
+ Base64.encode64(
88
+ OpenSSL::HMAC.digest(
89
+ OpenSSL::Digest.new('sha256'),
90
+ app_secret,
91
+ sign_str
92
+ )
93
+ ).strip
94
+ end
24
95
  end
25
96
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HikOpenapi
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2'
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hik_openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - palytoxin
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-22 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.4.1
13
27
  description: hikvision openapi for ruby.
14
28
  email:
15
29
  - sxty32@gmail.com
@@ -19,16 +33,15 @@ extra_rdoc_files: []
19
33
  files:
20
34
  - ".gitignore"
21
35
  - ".rspec"
36
+ - ".rubocop.yml"
22
37
  - ".travis.yml"
23
38
  - Gemfile
24
39
  - Gemfile.lock
40
+ - Guardfile
25
41
  - README.md
26
42
  - Rakefile
27
43
  - hik_openapi.gemspec
28
44
  - lib/hik_openapi.rb
29
- - lib/hik_openapi/api.rb
30
- - lib/hik_openapi/configuration.rb
31
- - lib/hik_openapi/result.rb
32
45
  - lib/hik_openapi/version.rb
33
46
  homepage: https://github.com/palytoxin/hik_openapi
34
47
  licenses: []
@@ -45,14 +58,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
58
  requirements:
46
59
  - - ">="
47
60
  - !ruby/object:Gem::Version
48
- version: 2.3.0
61
+ version: 2.4.0
49
62
  required_rubygems_version: !ruby/object:Gem::Requirement
50
63
  requirements:
51
64
  - - ">="
52
65
  - !ruby/object:Gem::Version
53
66
  version: '0'
54
67
  requirements: []
55
- rubygems_version: 3.0.3
68
+ rubygems_version: 3.1.6
56
69
  signing_key:
57
70
  specification_version: 4
58
71
  summary: hikvision openapi for ruby.
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'net/http'
4
- require 'json'
5
- require 'base64'
6
- require 'uri'
7
- require 'securerandom'
8
- require 'openssl'
9
-
10
- module HikOpenapi
11
- module Api
12
- class << self
13
- def get
14
- # TODO
15
- puts 'WIP'
16
- end
17
-
18
- def post(path, body)
19
- uri = URI(HikOpenapi.config.host)
20
- uri.merge!(path)
21
-
22
- https = Net::HTTP.new(uri.host, uri.port)
23
- https.use_ssl = true
24
- https.open_timeout = 10
25
- https.read_timeout = 10
26
- https.verify_mode = OpenSSL::SSL::VERIFY_NONE
27
- headers = init_header('POST', uri.path)
28
- # headers.transform_values! { |v| v.force_encoding('iso-8859-1') }
29
-
30
- begin
31
- request = Net::HTTP::Post.new(uri.request_uri, headers)
32
- request.body = body.to_json
33
- res = https.request(request)
34
-
35
- result = HikOpenapi::Result.new
36
- result.code = res.code
37
- result.body = JSON.parse(res.body.force_encoding('utf-8'))
38
- result.origin = res
39
- result
40
- rescue StandardError => e
41
- result = HikOpenapi::Result.new
42
- result.error = HikOpenapi::Error.new(e)
43
- result
44
- end
45
- end
46
-
47
- private
48
-
49
- def init_header(method, path)
50
- headers = {
51
- 'Content-Type': 'application/json',
52
- "Accept": '*/*',
53
- 'x-ca-timestamp': (Time.now.to_f * 1000).to_i.to_s,
54
- 'x-ca-nonce': SecureRandom.uuid,
55
- 'x-ca-key': HikOpenapi.config.app_key
56
- }
57
-
58
- headers.merge({
59
- "x-ca-signature": sign(method, path, headers)
60
- })
61
- end
62
-
63
- def sign(method, path, headers)
64
- sign_str = headers.reject { |a| a.to_s.start_with?('x-ca') }.values.sort
65
- sign_str = sign_str.unshift(method).push(path).join("\n")
66
-
67
- enc = Base64.encode64(
68
- OpenSSL::HMAC.digest(
69
- OpenSSL::Digest.new('sha256'),
70
- HikOpenapi.config.app_secret,
71
- sign_str
72
- )
73
- ).strip
74
- enc
75
- end
76
- end
77
- end
78
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HikOpenapi
4
- class Configuration
5
- attr_accessor :host
6
- attr_accessor :app_key
7
- attr_accessor :app_secret
8
- end
9
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HikOpenapi
4
- class Result
5
- attr_accessor :code
6
- attr_accessor :body
7
- attr_accessor :origin
8
- attr_accessor :error
9
- end
10
- end