magento_rest_api 0.1.4.1 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/magento_rest_api/version.rb +1 -1
- data/lib/magento_rest_api.rb +74 -60
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73ba74c77a3a36dc898dfac85949b7d0909ff33c
|
4
|
+
data.tar.gz: b6b6564d80b61938c9439d6074265f74fd84c4cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1053e0e363796d3ab263b61684ac22109e1ca9d1b93721d3912a2edc246a2b971d49b7637f5d2d523635efc34f88c19e6afa9c978643179469a8d1e7a463a51a
|
7
|
+
data.tar.gz: 33b81e6728bc54aea595dd6f6a14758306cc6cf29fd53db49072b235874ee5f7fbead044529eded5ad1476608fb199aed4d159daa9979e09e5f99a1465e02bce
|
data/lib/magento_rest_api.rb
CHANGED
@@ -6,7 +6,6 @@ require 'base64'
|
|
6
6
|
require 'net/http'
|
7
7
|
require 'json'
|
8
8
|
require 'yaml'
|
9
|
-
|
10
9
|
require "magento_rest_api/version"
|
11
10
|
|
12
11
|
module MagentoRestApi
|
@@ -15,76 +14,91 @@ module MagentoRestApi
|
|
15
14
|
# % authenticate
|
16
15
|
#
|
17
16
|
# Example:
|
18
|
-
#
|
19
|
-
|
20
|
-
# >>
|
17
|
+
#
|
18
|
+
#% irb
|
19
|
+
# >> require 'restfull_oauth'
|
20
|
+
# >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
|
21
|
+
# >> post_data = {'foo' => 'bar' }.to_json
|
22
|
+
# >> response = MagentoRestApi::connect('POST','https://your magento server/api/rest/products/200', post_data)
|
21
23
|
# => #<Net::HTTPOK 200 OK readbody=true>
|
24
|
+
# >> puts JSON.parse(response.body).to_yaml
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
class Connection
|
27
|
+
def initialize(config = {})
|
28
|
+
@config = config
|
29
|
+
self
|
30
|
+
end
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
32
|
+
def connect(method, uri, post_data=nil)
|
33
|
+
params = params(@config['consumer_key'], @config['token'] )
|
34
|
+
signature_base_string = signature_base_string(method, uri.to_s, params)
|
35
|
+
signing_key = @config['consumer_secret'] + '&' + @config['token_secret']
|
36
|
+
params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string))
|
37
|
+
header_string = create_header(params)
|
38
|
+
json_response = request_data(header_string, uri, method, post_data)
|
39
|
+
end
|
30
40
|
|
31
|
-
|
32
|
-
params = {
|
33
|
-
'oauth_consumer_key' => consumer_key,
|
34
|
-
'oauth_nonce' => generate_nonce,
|
35
|
-
'oauth_signature_method' => 'HMAC-SHA1',
|
36
|
-
'oauth_token' => token,
|
37
|
-
'oauth_version' => '1.0',
|
38
|
-
'oauth_timestamp' => Time.now.to_i.to_s
|
39
|
-
}
|
40
|
-
end
|
41
|
+
private
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
43
|
+
def generate_nonce(size=6)
|
44
|
+
Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
Base64.encode64(hmac).chomp.gsub(/\n/, '')
|
51
|
-
end
|
47
|
+
def url_encode(string)
|
48
|
+
CGI::escape(string)
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
def params(consumer_key, token)
|
52
|
+
params = {
|
53
|
+
'oauth_consumer_key' => consumer_key,
|
54
|
+
'oauth_nonce' => generate_nonce,
|
55
|
+
'oauth_signature_method' => 'HMAC-SHA1',
|
56
|
+
'oauth_token' => token,
|
57
|
+
'oauth_version' => '1.0',
|
58
|
+
'oauth_timestamp' => Time.now.to_i.to_s
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
def signature_base_string(method, uri, params)
|
63
|
+
encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}")
|
64
|
+
encoded = method + '&' + url_encode(uri) + '&' + encoded_params
|
65
|
+
end
|
66
|
+
|
67
|
+
def sign(key, base_string)
|
68
|
+
digest = OpenSSL::Digest.new('sha1')
|
69
|
+
hmac = OpenSSL::HMAC.digest(digest, key, base_string)
|
70
|
+
Base64.encode64(hmac).chomp.gsub(/\n/, '')
|
57
71
|
end
|
58
|
-
header.slice(0..-2)
|
59
|
-
end
|
60
72
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
66
|
-
if method == 'POST'
|
67
|
-
resp, data = http.post(uri.path, post_data, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
|
68
|
-
elsif method == 'PUT'
|
69
|
-
request = Net::HTTP::Put.new(uri.path)
|
70
|
-
headers = { 'Authorization' => header, "Content-Type" => "application/json" }
|
71
|
-
headers.keys.each do |key|
|
72
|
-
request[key] = headers[key]
|
73
|
+
def create_header(params)
|
74
|
+
header = "OAuth "
|
75
|
+
params.each do |k, v|
|
76
|
+
header += "#{k}=\"#{v}\","
|
73
77
|
end
|
74
|
-
|
75
|
-
resp, data = http.request(request)
|
76
|
-
else
|
77
|
-
resp, data = http.get(uri.path, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
|
78
|
+
header.slice(0..-2)
|
78
79
|
end
|
79
|
-
resp
|
80
|
-
end
|
81
80
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
81
|
+
def request_data(header, uri, method, post_data=nil)
|
82
|
+
uri = URI(uri)
|
83
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
84
|
+
if (uri.port == 443)
|
85
|
+
http.use_ssl = true
|
86
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
87
|
+
end
|
88
|
+
if method == 'POST'
|
89
|
+
resp, data = http.post(uri.path, post_data, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
|
90
|
+
elsif method == 'PUT'
|
91
|
+
request = Net::HTTP::Put.new(uri.path)
|
92
|
+
headers = { 'Authorization' => header, "Content-Type" => "application/json" }
|
93
|
+
headers.keys.each do |key|
|
94
|
+
request[key] = headers[key]
|
95
|
+
end
|
96
|
+
request.body = post_data
|
97
|
+
resp, data = http.request(request)
|
98
|
+
else
|
99
|
+
resp, data = http.get(uri.path, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
|
100
|
+
end
|
101
|
+
resp
|
102
|
+
end
|
89
103
|
end
|
90
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento_rest_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel.van.den.Oord
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|