open_jd 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: c0a555f25e8092790f5c29b8a4b181f0206b1de9
4
+ data.tar.gz: 0e5a126d8c998fa25d825f4e40f3f69c74fdbccd
5
+ SHA512:
6
+ metadata.gz: 23deca3dcbd96f27e91ffd5c1d20f72765bde3ad8daa2507bc01dd661e6b9a86413c3cc939a8ed2f546f17819ad06b0c07953bd7ef315348357399750bcc52e2
7
+ data.tar.gz: 8207fd72f6926fdc5de5a0e84e12680ee53ac5fd40cc0876e23fcfdc53ad9061bb3cfe11d75a5cdaffc63b3fe816af57f33db1e5f45c9d7abe6e2031fcc0cb4a
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ .project
2
+ .classpath
3
+ .settings
4
+ *.swp
5
+ *.keep
6
+ *.tmproj
7
+ *.esproj
8
+ *.chproj
9
+ *.[oa]
10
+ *~
11
+ *.lock
12
+ *.sublime-project
13
+ *.sublime-workspace
14
+ *.class
15
+
16
+ # Xcode
17
+ .DS_Store
18
+ build/
19
+ *.pbxuser
20
+ !default.pbxuser
21
+ *.mode1v3
22
+ !default.mode1v3
23
+ *.mode2v3
24
+ !default.mode2v3
25
+ *.perspectivev3
26
+ !default.perspectivev3
27
+ *.xcworkspace
28
+ !default.xcworkspace
29
+ xcuserdata
30
+ profile
31
+ *.moved-aside
32
+ DerivedData
33
+ .idea/
34
+ *.rbc
35
+ *.sassc
36
+ .sass-cache
37
+ capybara-*.html
38
+ .rspec
39
+ /.bundle
40
+ /vendor/bundle
41
+ /log/*
42
+ /logs/*
43
+ /tmp/*
44
+ /db/*.sqlite3
45
+ /public/system/*
46
+ /coverage/
47
+ /spec/tmp/*
48
+ **.orig
49
+ rerun.txt
50
+ npm-debug.log
51
+ pickle-email-*.html
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_jd.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Justin Hao
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ OpenJd
2
+ ==========
3
+
4
+ JD Open Platform client for ruby. Rails3+ is supported.
5
+
6
+ ## NOTICE
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'open_jd'
13
+
14
+ If you want to use [patron][] as http client instead of Net::HTTP, add line below to your Gemfile
15
+
16
+ gem 'patron'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install open_jd
25
+
26
+ ## Usage
27
+
28
+ ### Rails with yaml file configured
29
+
30
+ Run generator to generate config file:
31
+
32
+ $ rails g open_jd:install
33
+
34
+ It will generate a `jd.yml` file under your Rails' config dir.
35
+ Open the file and configure it with your jd info.
36
+
37
+ Notice: `app_key`, `secret_key`, `endpoint` must be included in your YAML file, otherwise OpenJd.load will fail.
38
+
39
+ The `app_key` and `secret_key` are avaliable by `ENV['JD_API_KEY']`, `ENV['JD_SECRET_KEY']` out of the box if you use same keys for other jd related gems, e.g. omniauth-taobao.
40
+
41
+ call `OpenJd.get` or `OpenJd.post`,with jd parameters:
42
+
43
+ hash = OpenJd.post(
44
+ method: 'jingdong.ware.product.detail.search.list.get',
45
+ fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
46
+ )
47
+
48
+
49
+ The return data will be converted to a Hash automatically.
50
+
51
+ Also `OpenJd.get!` and `OpenJd.post!` are avaliable, which will raise an `OpenJd::Error` if a `error_response` receieved.
52
+
53
+ ### plain ruby
54
+
55
+ OpenJd.config = {
56
+ 'app_key' => 'test',
57
+ 'secret_key' => 'test',
58
+ 'endpoint' => 'http://gw.api.jd.com/routerjson'
59
+ }
60
+
61
+ OpenJd.initialize_session
62
+
63
+ hash = OpenJd.get(
64
+ method: 'jingdong.ware.product.detail.search.list.get',
65
+ fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
66
+ )
67
+
68
+ ### get query string
69
+
70
+ If you want the query string with some params just pass the params to OpenJd.url() the same as OpenJd.get().
71
+ The query string will change every time your executed because timestamps changed.
72
+
73
+ OpenJd.url(
74
+ method: 'jingdong.ware.product.detail.search.list.get',
75
+ fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
76
+ )
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
85
+
86
+ [patron]: https://github.com/toland/patron
@@ -0,0 +1,11 @@
1
+ module OpenJd
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ def copy_yml_file
7
+ copy_file 'jd.yml', 'config/jd.yml'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ development:
2
+ app_key: 'test'
3
+ secret_key: 'test'
4
+ endpoint: 'http://gw.api.jd.com/routerjson'
5
+
6
+ test:
7
+ app_key: 'test'
8
+ secret_key: 'test'
9
+ endpoint: 'http://gw.api.jd.com/routerjson'
10
+
11
+ production:
12
+ app_key: 'test'
13
+ secret_key: 'test'
14
+ endpoint: 'http://gw.api.jd.com/routerjson'
@@ -0,0 +1,7 @@
1
+ require 'cgi'
2
+
3
+ class Hash
4
+ def to_query
5
+ self.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
6
+ end
7
+ end
@@ -0,0 +1,165 @@
1
+ module OpenJd
2
+ REQUEST_TIMEOUT = 10
3
+ API_VERSION = '2.0'
4
+ USER_AGENT = "open_jd-v#{VERSION}"
5
+ PARAMS_KEY = '360buy_param_json'
6
+ PRODUCT_LINK = 'http://item.jd.com'
7
+
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ attr_accessor :config, :session
12
+
13
+ def jd_link(id)
14
+ "#{PRODUCT_LINK}/#{id}.html"
15
+ end
16
+
17
+ # Load a yml config, and initialize http session
18
+ # yml config file content should be:
19
+ #
20
+ # app_key: "YOUR APP KEY"
21
+ # secret_key: "YOUR SECRET KEY"
22
+ # endpoint: "TAOBAO GATEWAY API URL"
23
+ #
24
+ def load(config_file)
25
+ @config = YAML.load_file(config_file)
26
+ @config = config[Rails.env] if defined? Rails
27
+ check_config_and_export_to_env
28
+ initialize_session
29
+ end
30
+
31
+ # check config and export all setting to ENV
32
+ def check_config_and_export_to_env
33
+ check_config
34
+ export_config_to_env
35
+ end
36
+
37
+ # check config
38
+ #
39
+ # raise exception if config key missed in YAML file
40
+ def check_config
41
+ list = []
42
+ %w(app_key secret_key endpoint).map do |k|
43
+ list << k unless config.key? k
44
+ end
45
+
46
+ raise "[#{list.join(', ')}] not included in your yaml file." unless list.empty?
47
+ end
48
+
49
+ # setting ENV variables from config
50
+ #
51
+ # ENV variables:
52
+ #
53
+ # JD_API_KEY -> config['app_key']
54
+ # JD_SECRET_KEY -> config['secret_key']
55
+ # JD_ENDPOINT -> config['endpoint']
56
+ def export_config_to_env
57
+ ENV['JD_API_KEY'] = config['app_key']
58
+ ENV['JD_SECRET_KEY'] = config['secret_key']
59
+ ENV['JD_ENDPOINT'] = config['endpoint']
60
+ end
61
+
62
+ # Initialize http sesison
63
+ def initialize_session
64
+ @session = Faraday.new url: config['endpoint'] do |builder|
65
+ begin
66
+ require 'patron'
67
+ builder.adapter :patron
68
+ rescue LoadError
69
+ builder.adapter :net_http
70
+ end
71
+ end
72
+ end
73
+
74
+ # Return request signature with MD5 signature method
75
+ def sign(params)
76
+ Digest::MD5.hexdigest(wrap_with_secret sorted_option_string(params)).upcase
77
+ end
78
+
79
+ # wrapped with secret_key
80
+ def wrap_with_secret(s)
81
+ "#{config['secret_key']}#{s}#{config['secret_key']}"
82
+ end
83
+
84
+ # Return sorted request parameter by request key
85
+ def sorted_option_string(options)
86
+ options.map {|k, v| "#{k}#{v}" }.sort.join
87
+ end
88
+
89
+ # Merge custom parameters with JD system parameters.
90
+ #
91
+ # System paramters below will be merged.
92
+ #
93
+ # timestamp
94
+ # v
95
+ # format
96
+ # sign_method
97
+ # app_key
98
+ # method
99
+ # params_key
100
+ #
101
+ # Current JD API Version is '2.0'.
102
+ # <tt>format</tt> should be json.
103
+ # Only <tt>sign_method</tt> MD5 is supported so far.
104
+ def full_options(params)
105
+ {
106
+ timestamp: Time.zone.now.strftime('%F %T'),
107
+ v: API_VERSION,
108
+ format: :json,
109
+ sign_method: :md5,
110
+ app_key: config['app_key'],
111
+ method: params[:method],
112
+ PARAMS_KEY: params[:fields].to_json.to_s
113
+ }
114
+ end
115
+
116
+ def query_hash(params)
117
+ params = full_options params
118
+ params[:sign] = sign params
119
+ params
120
+ end
121
+
122
+ # Return query string with signature.
123
+ def query_string(params)
124
+ '?' + query_hash(params).to_query
125
+ end
126
+
127
+ # Return full url with signature.
128
+ def url(params)
129
+ format('%s%s', config['endpoint'], query_string(params))
130
+ end
131
+
132
+ # Return a parsed JSON object.
133
+ def parse_result(data)
134
+ MultiJson.decode(data)
135
+ end
136
+
137
+ # Request by get method and return result in JSON format
138
+ def get(params)
139
+ path = query_string(params)
140
+ parse_result session.get(path).body
141
+ end
142
+
143
+ # Request by get method and return result in JSON format
144
+ # Raise OpenTaobao::Error if returned with error_response
145
+ def get!(params)
146
+ response = get params
147
+ raise Error.new(MultiJson.encode response['error_response']) if response.has_key?('error_response')
148
+ response
149
+ end
150
+
151
+ # Request by post method and return result in JSON format
152
+ def post(params)
153
+ parse_result session.post('', query_hash(params).to_query).body
154
+ end
155
+
156
+ # Request by post method and return result in JSON format
157
+ # Raise OpenTaobao::Error if returned with error_response
158
+ def post!(params)
159
+ response = post params
160
+ raise Error.new(MultiJson.encode response['error_response']) if response.has_key?('error_response')
161
+ response
162
+ end
163
+ end
164
+ end
165
+
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module OpenJd
4
+ class Railtie < Rails::Railtie
5
+ generators do
6
+ require 'generators/open_jd/install_generator'
7
+ end
8
+
9
+ initializer 'load jd.yml' do
10
+ config_file = Rails.root + 'config/jd.yml'
11
+ OpenJd.load(config_file) if config_file.file?
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module OpenJd
2
+ VERSION = '0.0.1'
3
+ end
data/lib/open_jd.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'digest'
2
+ require 'faraday'
3
+ require 'multi_json'
4
+ require 'yaml'
5
+
6
+ require 'open_jd/version'
7
+ require 'open_jd/hash_ext'
8
+ require 'open_jd/open_jd'
9
+
10
+ require 'open_jd/railtie' if defined? Rails
data/open_jd.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/open_jd/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Justin Hao']
6
+ gem.email = ['justin@haoyufeng.com']
7
+ gem.description = %q{京东开放平台 Ruby 版,支持Rails3+}
8
+ gem.summary = %q{Open JD API for ruby}
9
+ gem.homepage = 'http://github.com/mambahao/open_jd'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'open_jd'
15
+ gem.license = 'MIT'
16
+ gem.require_paths = ['lib']
17
+ gem.version = OpenJd::VERSION
18
+
19
+ gem.add_dependency 'multi_json', '~> 0'
20
+ gem.add_dependency 'faraday', '~> 0'
21
+ gem.add_development_dependency 'rspec', '~> 0'
22
+ gem.add_development_dependency 'debugger', '~> 0'
23
+ gem.add_development_dependency 'autotest', '~> 0'
24
+ gem.add_development_dependency 'rake', '~> 0'
25
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_jd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin Hao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: debugger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: autotest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: "京东开放平台 Ruby 版,支持Rails3+"
98
+ email:
99
+ - justin@haoyufeng.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE
107
+ - README.md
108
+ - lib/generators/open_jd/install_generator.rb
109
+ - lib/generators/templates/jd.yml
110
+ - lib/open_jd.rb
111
+ - lib/open_jd/hash_ext.rb
112
+ - lib/open_jd/open_jd.rb
113
+ - lib/open_jd/railtie.rb
114
+ - lib/open_jd/version.rb
115
+ - open_jd.gemspec
116
+ homepage: http://github.com/mambahao/open_jd
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.5
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Open JD API for ruby
140
+ test_files: []