ocx_client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bdc18ad4b8b258c90142abc89553524c9b26fb65
4
+ data.tar.gz: 403e7a5e1767c28e8fa2a6a4d4c1f7508f83c655
5
+ SHA512:
6
+ metadata.gz: f066bc098082f6084646e03a9000ce68d364de77f0e2324bedb08a320718adecc3f8948c9bf711c6cbaa40d7095ed5c22589273a55d8a07cef5ba0b7ead2659e
7
+ data.tar.gz: bc15e18f289e9017d8e68b93376edd689f146dd98596dce6797927c50b79708c470cbf8b099de31855c2f1599c12005ba951a1f2a26351f2876dac742e156043
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yalong1976@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ocx_client.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 aaron
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # OcxClient
2
+
3
+ OcxClient是用Ruby语言编写的OCX API客户端
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ocx_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ocx_client
20
+
21
+ ## Usage
22
+
23
+ ### REST API client ###
24
+
25
+ Use `#get` or `#post` to access API after you created a `OcxClient::RestApi`:
26
+
27
+ ```ruby
28
+ require 'ocx_client'
29
+
30
+ # Client can be initialized not providing key and sercet, but this client can only access public APIs
31
+ client_public = OcxClient::RestApi.new endpoint: 'https://openapi.ocx.com'
32
+
33
+ # GET public api /api/v2/markets
34
+ client_public.get_public '/api/v2/markets'
35
+
36
+ # To build a full functional client which can access both public/private api, access_key/secret_key
37
+ # are required.
38
+ #
39
+ # `endpoint` can be ignored or set.
40
+ #
41
+ # If there's no data received in `timeout` seconds, Net::OpenTimeout will be raised. Default to 60.
42
+ #
43
+ client = OcxClient::RestApi.new access_key: 'your_access_key', secret_key: 'your_secret_key', endpoint: 'https://openapi.ocx.com', timeout: 60
44
+
45
+ # GET private api /api/v2/orders with 'market=btcusdt'
46
+ client.get '/api/v2/orders', market: 'btcusdt'
47
+
48
+ # POST to create an order
49
+ client.post '/api/v2/orders', market: 'btcusdt', side: 'sell', volume: '0.11', price: '8000.0'
50
+
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ocx_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ocx_client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require "ocx_client/version"
2
+ require "ocx_client/core_ext"
3
+ require "ocx_client/auth"
4
+ require "ocx_client/rest_api"
@@ -0,0 +1,38 @@
1
+ require 'openssl'
2
+
3
+ module OcxClient
4
+ class Auth
5
+
6
+ def initialize(access_key, secret_key)
7
+ @access_key = access_key
8
+ @secret_key = secret_key
9
+ end
10
+
11
+ def signed_challenge(challenge)
12
+ signature = OpenSSL::HMAC.hexdigest 'SHA256', @secret_key, "#{@access_key}#{challenge}"
13
+ {auth: {access_key: @access_key, answer: signature}}
14
+ end
15
+
16
+ def signed_params(verb, path, params={})
17
+ params = format_params params
18
+ signature = sign verb, path, URI.unescape(params.to_query)
19
+ params.merge(signature: signature)
20
+ end
21
+
22
+ def sign(verb, path, params)
23
+ OpenSSL::HMAC.hexdigest 'SHA256', @secret_key, payload(verb, path, params)
24
+ end
25
+
26
+ def payload(verb, path, params)
27
+ "#{verb.upcase}|#{path}|#{params}"
28
+ end
29
+
30
+ def format_params(params)
31
+ params = params.symbolize_keys
32
+ params[:access_key] ||= @access_key
33
+ params[:tonce] ||= (Time.now.to_f*1000).to_i
34
+ params
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require 'ocx_client/core_ext/keys'
2
+ require 'ocx_client/core_ext/to_query'
@@ -0,0 +1,16 @@
1
+ unless {}.respond_to? :symbolize_keys
2
+ class Hash
3
+ def transform_keys
4
+ return enum_for(:transform_keys) unless block_given?
5
+ result = self.class.new
6
+ each_key do |key|
7
+ result[yield(key)] = self[key]
8
+ end
9
+ result
10
+ end
11
+
12
+ def symbolize_keys
13
+ transform_keys{ |key| key.to_sym rescue key }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,60 @@
1
+ unless Object.new.respond_to? :to_query and Object.new.respond_to? :to_param
2
+
3
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
4
+
5
+ class Object
6
+ def to_param
7
+ to_s
8
+ end
9
+
10
+ def to_query(key)
11
+ "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
12
+ end
13
+ end
14
+
15
+ class NilClass
16
+ def to_param
17
+ self
18
+ end
19
+ end
20
+
21
+ class TrueClass
22
+ def to_param
23
+ self
24
+ end
25
+ end
26
+
27
+ class FalseClass
28
+ def to_param
29
+ self
30
+ end
31
+ end
32
+
33
+ class Array
34
+ def to_param
35
+ collect(&:to_param).join '/'
36
+ end
37
+
38
+ def to_query(key)
39
+ prefix = "#{key}[]"
40
+
41
+ if empty?
42
+ nil.to_query(prefix)
43
+ else
44
+ collect { |value| value.to_query(prefix) }.join '&'
45
+ end
46
+ end
47
+ end
48
+
49
+ class Hash
50
+ def to_query(namespace = nil)
51
+ collect do |key, value|
52
+ unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
53
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
54
+ end
55
+ end.compact.sort! * '&'
56
+ end
57
+
58
+ alias_method :to_param, :to_query
59
+ end
60
+ end
@@ -0,0 +1,79 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module OcxClient
5
+ class RestApi
6
+
7
+ attr :auth
8
+
9
+ def initialize(options={})
10
+ options = options.symbolize_keys
11
+ setup_auth_keys options
12
+ @endpoint = options[:endpoint] || 'https://openapi.ocx.com'
13
+ @timeout = options[:timeout] || 60
14
+ end
15
+
16
+ def get_public(path, params={})
17
+ uri = URI("#{@endpoint}#{path}")
18
+ uri.query = URI.encode_www_form params
19
+
20
+ request(:get, path, nil, params) do |http, _|
21
+ http.request_get(uri.request_uri)
22
+ end
23
+ end
24
+
25
+ def get(path, params={})
26
+ check_auth!
27
+
28
+ uri = URI("#{@endpoint}#{path}")
29
+
30
+ request(:get, path, @auth, params) do |http, signed_params|
31
+ uri.query = URI.encode_www_form signed_params
32
+ http.request_get(uri.request_uri)
33
+ end
34
+ end
35
+
36
+ def post(path, params={})
37
+ check_auth!
38
+
39
+ request(:post, path, @auth, params) do |http, signed_params|
40
+ http.request_post(path, signed_params.to_query)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def request(action, path, auth, params={})
47
+ uri = URI("#{@endpoint}#{path}")
48
+ params = auth.signed_params action.to_s.upcase, path, params if auth
49
+
50
+ http = Net::HTTP.new(uri.hostname, uri.port)
51
+ http.open_timeout = @timeout
52
+ http.use_ssl = true if @endpoint.start_with?('https://')
53
+
54
+ http.start do |http|
55
+ parse yield(http, params)
56
+ end
57
+ end
58
+
59
+ def parse(response)
60
+ JSON.parse response.body
61
+ rescue JSON::ParserError
62
+ {http_error: {code: response.code, body: response.body}}
63
+ end
64
+
65
+ def setup_auth_keys(options)
66
+ if options[:access_key] && options[:secret_key]
67
+ @access_key = options[:access_key]
68
+ @secret_key = options[:secret_key]
69
+ @auth = Auth.new @access_key, @secret_key
70
+ else
71
+ #raise ArgumentError, 'Missing access key and/or secret key'
72
+ end
73
+ end
74
+
75
+ def check_auth!
76
+ raise ArgumentError, 'Missing access key and/or secret key' if @auth.nil?
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module OcxClient
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ocx_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ocx_client"
8
+ spec.version = OcxClient::VERSION
9
+ spec.authors = ["aaron"]
10
+ spec.email = ["aaron@hash.tech"]
11
+
12
+ spec.summary = %q{ocx client}
13
+ spec.description = %q{ocx client}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ocx_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - aaron
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ocx client
42
+ email:
43
+ - aaron@hash.tech
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/ocx_client.rb
57
+ - lib/ocx_client/auth.rb
58
+ - lib/ocx_client/core_ext.rb
59
+ - lib/ocx_client/core_ext/keys.rb
60
+ - lib/ocx_client/core_ext/to_query.rb
61
+ - lib/ocx_client/rest_api.rb
62
+ - lib/ocx_client/version.rb
63
+ - ocx_client.gemspec
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: ocx client
88
+ test_files: []