magento_rest_api 0.1.4.1 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bf269ff6826682f97ec0abe3f352df1d57ea10d
4
- data.tar.gz: 7af27aef372924ada3937b2ce49891a288490ed2
3
+ metadata.gz: 73ba74c77a3a36dc898dfac85949b7d0909ff33c
4
+ data.tar.gz: b6b6564d80b61938c9439d6074265f74fd84c4cc
5
5
  SHA512:
6
- metadata.gz: 75948fa22cef0afbe844a9d7e7fdff2541083a907fea99cde66e4b4f9f41c25d2bea973277d81d3043c58389dd99e6ad44aa066e1d071e0be1823168cd3c2432
7
- data.tar.gz: d1f958c82284c4b338e2e3bba4aeae79fef8f9a6cd2ea78e2ce71a9e0b29066bc68d6f5a283c93e258d9b9abe4d8864e76b17fdd9bae10433c5d18e53bf473eb
6
+ metadata.gz: 1053e0e363796d3ab263b61684ac22109e1ca9d1b93721d3912a2edc246a2b971d49b7637f5d2d523635efc34f88c19e6afa9c978643179469a8d1e7a463a51a
7
+ data.tar.gz: 33b81e6728bc54aea595dd6f6a14758306cc6cf29fd53db49072b235874ee5f7fbead044529eded5ad1476608fb199aed4d159daa9979e09e5f99a1465e02bce
@@ -1,3 +1,3 @@
1
1
  module MagentoRestApi
2
- VERSION = "0.1.4.1"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -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
- # % irb
19
- # >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
20
- # >> json_response = MagentoRestApi::connect('GET','https://your magento/api/rest/products/200', keys)
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
- def self.generate_nonce(size=6)
24
- Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
25
- end
26
+ class Connection
27
+ def initialize(config = {})
28
+ @config = config
29
+ self
30
+ end
26
31
 
27
- def self.url_encode(string)
28
- CGI::escape(string)
29
- end
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
- def self.params(consumer_key, token)
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
- def self.signature_base_string(method, uri, params)
43
- 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']}")
44
- encoded = method + '&' + url_encode(uri) + '&' + encoded_params
45
- end
43
+ def generate_nonce(size=6)
44
+ Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
45
+ end
46
46
 
47
- def self.sign(key, base_string)
48
- digest = OpenSSL::Digest.new('sha1')
49
- hmac = OpenSSL::HMAC.digest(digest, key, base_string)
50
- Base64.encode64(hmac).chomp.gsub(/\n/, '')
51
- end
47
+ def url_encode(string)
48
+ CGI::escape(string)
49
+ end
52
50
 
53
- def self.create_header(params)
54
- header = "OAuth "
55
- params.each do |k, v|
56
- header += "#{k}=\"#{v}\","
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
- def self.request_data(header, uri, method, post_data=nil)
62
- uri = URI(uri)
63
- http = Net::HTTP.new(uri.host, uri.port)
64
- http.use_ssl = true
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
- request.body = post_data
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
- def self.connect(method, uri, keys, post_data=nil)
83
- params = params(keys['consumer_key'], keys['token'] )
84
- signature_base_string = signature_base_string(method, uri.to_s, params)
85
- signing_key = keys['consumer_secret'] + '&' + keys['token_secret']
86
- params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string))
87
- header_string = create_header(params)
88
- json_response = request_data(header_string, uri, method, post_data)
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.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-13 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler