unp_smart 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 955f7942bdb4460b51b18d9f0a3700ac15ad07f6
4
+ data.tar.gz: 8fb515c5fa39660767cfadcb36085c4f312762de
5
+ SHA512:
6
+ metadata.gz: 0ef31862c4c24f90f7b57edb8b543dcee70476cd563851fea5b27ea686ec1ad33266215cb38ace7eba86a24536ac22a59e14524672c5097888403d6d9fa52afc
7
+ data.tar.gz: 342dfc040c2fe95dd8b04e27b480d5b0ebc739d5d823f4b75f66f2c56812fe1ef6abeb63c4d88c0d665c6288c2d51227ed6c9325845939416fdad69573a593ea
data/.gitignore ADDED
@@ -0,0 +1,31 @@
1
+ *.rbc
2
+ capybara-*.html
3
+ .rspec
4
+ /log
5
+ /tmp
6
+ /db/*.sqlite3
7
+ /public/system
8
+ /coverage/
9
+ /spec/tmp
10
+ **.orig
11
+ rerun.txt
12
+ pickle-email-*.html
13
+
14
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
15
+ config/initializers/secret_token.rb
16
+ config/secrets.yml
17
+
18
+ ## Environment normalisation:
19
+ /.bundle
20
+ /vendor/bundle
21
+
22
+ # these should all be checked in to normalise the environment:
23
+ # Gemfile.lock, .ruby-version, .ruby-gemset
24
+
25
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
26
+ .rvmrc
27
+
28
+ # if using bower-rails ignore default bower_components path bower.json files
29
+ /vendor/assets/bower_components
30
+ *.bowerrc
31
+ bower.json
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unp_smart.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ unp_smart (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (4.1.6)
10
+ i18n (~> 0.6, >= 0.6.9)
11
+ json (~> 1.7, >= 1.7.7)
12
+ minitest (~> 5.1)
13
+ thread_safe (~> 0.1)
14
+ tzinfo (~> 1.1)
15
+ diff-lcs (1.2.5)
16
+ i18n (0.6.11)
17
+ json (1.8.1)
18
+ mime-types (2.4.3)
19
+ minitest (5.4.3)
20
+ netrc (0.7.7)
21
+ rake (10.4.2)
22
+ rest-client (1.7.2)
23
+ mime-types (>= 1.16, < 3.0)
24
+ netrc (~> 0.7)
25
+ rspec (3.1.0)
26
+ rspec-core (~> 3.1.0)
27
+ rspec-expectations (~> 3.1.0)
28
+ rspec-mocks (~> 3.1.0)
29
+ rspec-core (3.1.7)
30
+ rspec-support (~> 3.1.0)
31
+ rspec-expectations (3.1.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.1.0)
34
+ rspec-mocks (3.1.3)
35
+ rspec-support (~> 3.1.0)
36
+ rspec-support (3.1.2)
37
+ thread_safe (0.3.4)
38
+ tzinfo (1.2.2)
39
+ thread_safe (~> 0.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activesupport (~> 4.1)
46
+ bundler (~> 1.6)
47
+ rake (~> 10.4)
48
+ rest-client (~> 1.7)
49
+ rspec (~> 3.1)
50
+ unp_smart!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Martin Xu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # UnpSmart
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'unp_smart'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install unp_smart
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/unp_smart/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,7 @@
1
+ development:
2
+ account: '000000'
3
+ secret_key: ''
4
+ server: 'http://192.168.88.105:9999'
5
+ debug: true
6
+ verbose: false
7
+ raw_response: true
@@ -0,0 +1,144 @@
1
+ #encoding: utf-8
2
+
3
+ module UnpSmart
4
+ module UnpSmartMethod
5
+ attr_accessor :api,:params,:http_method,:config,:debug,:verbose
6
+
7
+ def self.extended(base)
8
+ base.send :load_config
9
+ end
10
+
11
+ def get api,params={}
12
+ _invoke_api :get,api,params
13
+ end
14
+
15
+ def post api,params={}
16
+ _invoke_api :post,api,params
17
+ end
18
+
19
+ def _invoke_api http_method,api,params={}
20
+ log "request api:#{api},params:#{params}"
21
+
22
+ @api = api
23
+ @params = params
24
+ @http_method = http_method
25
+ @debug = get_variable :debug
26
+ @config = {} if @config.nil?
27
+ @verbose = get_variable :verbose
28
+
29
+ raise 'api is not allowed blank!' if api.blank?
30
+ account = get_variable :account
31
+ log "account:", account if @verbose
32
+ raise 'account is not allowed blank!' if account.blank?
33
+
34
+ secret_key = get_variable :secret_key
35
+ log "secret_key:",secret_key if @verbose
36
+
37
+ raise 'invalid secret_key!' if secret_key.blank?
38
+
39
+ extracted_params = request_params(params)
40
+ extracted_params[:account] = account
41
+ extracted_params[:source] = 'ruby-sdk'
42
+ extracted_params[:timestamp] =Time.now.to_f
43
+ extracted_params[:version] = '1.0'
44
+
45
+ extracted_params[:sign] = sign_params extracted_params,secret_key
46
+
47
+ request_url = "#{get_variable :server}#{api}?#{extracted_params.map{|key,value| "#{key}=#{CGI.escape value.nil? ? "" : value.to_s}"}.join("&")}"
48
+
49
+ log "request:",request_url if @debug
50
+
51
+ return_response = JSON.parse RestClient::Request.execute(url: request_url,
52
+ method: http_method,
53
+ verify_ssl: false,
54
+ timeout: get_variable(:timeout))
55
+
56
+ log "response:",return_response if @debug
57
+
58
+ if block_given?
59
+ log "invoke block" if @debug
60
+ yield return_response
61
+ else
62
+ if get_variable(:raw_response)
63
+ log "return raw response" if @debug
64
+ return_response
65
+ else
66
+ log "parse result"
67
+ if return_response['resCode'] == '0000'
68
+ return true,return_response
69
+ else
70
+ return false,"##{return_response['resCode']}:#{return_response['resMsg']}"
71
+ end
72
+ end
73
+ end
74
+ # rescue => e
75
+ # log "!!!invoke http service error",e.inspect
76
+ # return false,e.message
77
+ end
78
+
79
+ def sign_params params,secret_key
80
+ sign_str = "#{params.sort_by { |k, v| k.to_s }.flatten.join}#{secret_key}"
81
+ log "sign str",sign_str if @verbose
82
+ Digest::MD5.hexdigest(sign_str).upcase.tap do |str|
83
+ log "sign",str if @verbose
84
+ end
85
+ end
86
+
87
+ def request_params params={}
88
+ params.except(:action, :controller,:api_id,:id,:api_list,:format,:sign,:user,:source,:timestamp,:version)
89
+ end
90
+
91
+ def load_config
92
+ log "loading config"
93
+ @config ||= YAML.load_file("#{app_root}/config/unp_smart.yml")[current_env]
94
+ log "config:#{@config}" if @verbose
95
+ rescue => e
96
+ @config = {}
97
+ log "load config error:#{e.inspect}"
98
+ end
99
+
100
+ private
101
+ def get_variable key
102
+ get_variable_from_params(key).presence ||
103
+ get_variable_from_env(key).presence ||
104
+ get_variable_from_config(key)
105
+ end
106
+
107
+ def get_variable_from_params key
108
+ @params[key.to_sym]
109
+ end
110
+
111
+ def get_variable_from_env key
112
+ ENV[key.to_s]||ENV["#{key.to_s}_#{current_env}"]
113
+ end
114
+
115
+ def get_variable_from_config key
116
+ @config
117
+ @config[key.to_s]
118
+ end
119
+
120
+ def log *msg
121
+ file, line, _ = caller.first.split(":")
122
+ messages = "#{file.split("/").last}:#{line}:#{msg.join(" ")}"
123
+
124
+ @logger ||= rails_app? ? Logger.new("#{app_root}/log/api.log") : Logger.new(STDOUT)
125
+ @logger.debug messages
126
+ end
127
+
128
+ def app_root
129
+ root = Rails.root if rails_app?
130
+ root.present? ? root : '.'
131
+ rescue => e
132
+ log "access rails root got error:#{e.message}"
133
+ '.'
134
+ end
135
+
136
+ def current_env
137
+ rails_app? ? Rails.env : 'development'
138
+ end
139
+
140
+ def rails_app?
141
+ defined?(Rails)
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ module UnpSmart
2
+ VERSION = "0.0.1"
3
+ end
data/lib/unp_smart.rb ADDED
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'yaml'
3
+ require 'rest-client'
4
+
5
+ require 'active_support/logger'
6
+ require 'active_support/core_ext/object'
7
+ require 'active_support/core_ext/hash'
8
+
9
+ require 'unp_smart/version'
10
+ require 'unp_smart/unp_smart_method'
11
+
12
+ module UnpSmart
13
+ extend UnpSmart::UnpSmartMethod
14
+ end
@@ -0,0 +1,76 @@
1
+ require 'unp_smart'
2
+
3
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
+ RSpec.configure do |config|
5
+ # rspec-expectations config goes here. You can use an alternate
6
+ # assertion/expectation library such as wrong or the stdlib/minitest
7
+ # assertions if you prefer.
8
+ config.expect_with :rspec do |expectations|
9
+ # This option will default to `true` in RSpec 4. It makes the `description`
10
+ # and `failure_message` of custom matchers include text for helper methods
11
+ # defined using `chain`, e.g.:
12
+ # be_bigger_than(2).and_smaller_than(4).description
13
+ # # => "be bigger than 2 and smaller than 4"
14
+ # ...rather than:
15
+ # # => "be bigger than 2"
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ # rspec-mocks config goes here. You can use an alternate test double
20
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
21
+ config.mock_with :rspec do |mocks|
22
+ # Prevents you from mocking or stubbing a method that does not exist on
23
+ # a real object. This is generally recommended, and will default to
24
+ # `true` in RSpec 4.
25
+ mocks.verify_partial_doubles = true
26
+ end
27
+
28
+ # The settings below are suggested to provide a good initial experience
29
+ # with RSpec, but feel free to customize to your heart's content.
30
+ =begin
31
+ # These two settings work together to allow you to limit a spec run
32
+ # to individual examples or groups you care about by tagging them with
33
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
34
+ # get run.
35
+ config.filter_run :focus
36
+ config.run_all_when_everything_filtered = true
37
+
38
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
39
+ # For more details, see:
40
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
41
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
42
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
43
+ config.disable_monkey_patching!
44
+
45
+ # This setting enables warnings. It's recommended, but in some cases may
46
+ # be too noisy due to issues in dependencies.
47
+ config.warnings = true
48
+
49
+ # Many RSpec users commonly either run the entire suite or an individual
50
+ # file, and it's useful to allow more verbose output when running an
51
+ # individual spec file.
52
+ if config.files_to_run.one?
53
+ # Use the documentation formatter for detailed output,
54
+ # unless a formatter has already been configured
55
+ # (e.g. via a command-line flag).
56
+ config.default_formatter = 'doc'
57
+ end
58
+
59
+ # Print the 10 slowest examples and example groups at the
60
+ # end of the spec run, to help surface which specs are running
61
+ # particularly slow.
62
+ config.profile_examples = 10
63
+
64
+ # Run specs in random order to surface order dependencies. If you find an
65
+ # order dependency and want to debug it, you can fix the order by providing
66
+ # the seed, which is printed after each run.
67
+ # --seed 1234
68
+ config.order = :random
69
+
70
+ # Seed global randomization in this process using the `--seed` CLI option.
71
+ # Setting this allows you to use `--seed` to deterministically reproduce
72
+ # test failures related to randomization by passing the same `--seed` value
73
+ # as the one that triggered the failure.
74
+ Kernel.srand config.seed
75
+ =end
76
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ class UnpSmartSpec
4
+ describe UnpSmart do
5
+ it "should load config first" do
6
+ config = UnpSmart.config
7
+ expect(config).not_to be_nil
8
+ expect(config['account']).to eq('000000')
9
+ end
10
+
11
+ it "should invoke api" do
12
+ result = UnpSmart.get '/merchant/queryName',{mName: '巴黎春天'}
13
+ expect(result['resCode']).to eq('0000')
14
+ expect(result['data']).to be_a_kind_of(Array)
15
+ expect(result['data']).to include('巴黎春天')
16
+ end
17
+ end
18
+ end
data/unp_smart.gemspec ADDED
@@ -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 'unp_smart/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unp_smart"
8
+ spec.version = UnpSmart::VERSION
9
+ spec.authors = ["Martin Xu"]
10
+ spec.email = ["martin.xus@gmail.com"]
11
+ spec.summary = %q{SDK for Ruby}
12
+ spec.description = %q{UnionpaySmart SDK for Ruby}
13
+ spec.homepage = "https://github.com/unpsmart/ruby-sdk"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rspec", '~> 3.1'
23
+ spec.add_development_dependency "rest-client", '~> 1.7'
24
+ spec.add_development_dependency "activesupport", '~> 4.1'
25
+ spec.add_development_dependency "rake", '~> 10.4'
26
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unp_smart
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Xu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.4'
83
+ description: UnionpaySmart SDK for Ruby
84
+ email:
85
+ - martin.xus@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - config/unp_smart.yml
97
+ - lib/unp_smart.rb
98
+ - lib/unp_smart/unp_smart_method.rb
99
+ - lib/unp_smart/version.rb
100
+ - spec/spec_helper.rb
101
+ - spec/unp_smart_spec.rb
102
+ - unp_smart.gemspec
103
+ homepage: https://github.com/unpsmart/ruby-sdk
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: SDK for Ruby
127
+ test_files:
128
+ - spec/spec_helper.rb
129
+ - spec/unp_smart_spec.rb
130
+ has_rdoc: