magento_rest_api 0.1.5 → 0.1.6

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: 73ba74c77a3a36dc898dfac85949b7d0909ff33c
4
- data.tar.gz: b6b6564d80b61938c9439d6074265f74fd84c4cc
3
+ metadata.gz: a0e8c3cdf41552bb9172c55fac3e7f2f2f269806
4
+ data.tar.gz: 3d61c7b791bd136b5cac503db6d3a3049266c600
5
5
  SHA512:
6
- metadata.gz: 1053e0e363796d3ab263b61684ac22109e1ca9d1b93721d3912a2edc246a2b971d49b7637f5d2d523635efc34f88c19e6afa9c978643179469a8d1e7a463a51a
7
- data.tar.gz: 33b81e6728bc54aea595dd6f6a14758306cc6cf29fd53db49072b235874ee5f7fbead044529eded5ad1476608fb199aed4d159daa9979e09e5f99a1465e02bce
6
+ metadata.gz: 578905f627b379febf0ea2a9404d0284fa13853ded6c02b7e360edac90b538ed75ec4d96933921dc6db19239017ac571bbc52e19d8d7cfd8bf6d8fcd392aaea4
7
+ data.tar.gz: 13473b048d6169c1a1bcb9a9dfb43c0ddd2cee728b24e6bf1c3816f7c58a84b6ca95bf62b42d5e85e45d01b370208d857867c9ad8f0ab401daf4f8f5deb3a1a0
data/README.md CHANGED
@@ -32,19 +32,24 @@ GET request (get product information):
32
32
 
33
33
  ```irb
34
34
  % irb
35
- >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
36
- >> json_response = MagentoRestApi::connect('GET','https://your magento/api/rest/products/200', keys)
35
+ >> require 'restfull_oauth'
36
+ >> service = MagentoRestApi::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
37
+ >> send_data = {'foo' => 'bar' }
38
+ >> response = service.connect('GET','https://your magento/api/rest/products', keys, send_data)
37
39
  => #<Net::HTTPOK 200 OK readbody=true>
40
+ >> puts JSON.parse(response.body).to_yaml
38
41
  ```
39
42
 
40
43
  POST request (assign product to website):
41
44
 
42
45
  ```irb
43
46
  % irb
44
- >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
45
- >> post_data = {'website_id' => 1 }.to_json
46
- >> json_response = MagentoRestApi::connect('POST','https://your magento/api/rest/products/200/websites', keys, post_data)
47
+ >> require 'restfull_oauth'
48
+ >> service = MagentoRestApi::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
49
+ >> send_data = {'foo' => 'bar' }
50
+ >> response = RestfullOauth::connect('POST','https://your magento/api/rest/products/200/websites', send_data)
47
51
  => #<Net::HTTPOK 200 OK readbody=true>
52
+ >> puts JSON.parse(response.body).to_yaml
48
53
  ```
49
54
 
50
55
  PUT requests work in the same way as POST requests
@@ -52,6 +57,11 @@ PUT requests work in the same way as POST requests
52
57
  more information about magento's REST API: http://devdocs.magento.com/guides/m1x/api/rest/introduction.html
53
58
  I cannot stress enough to first find out if the API provides enough information for you.
54
59
 
60
+ ## Changes
61
+
62
+ - post_data is jsonified inside gem no need to do it yourself
63
+ - get now also supports parameters the get way
64
+
55
65
  ## Development
56
66
 
57
67
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,3 +1,3 @@
1
1
  module MagentoRestApi
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -30,6 +30,11 @@ module MagentoRestApi
30
30
  end
31
31
 
32
32
  def connect(method, uri, post_data=nil)
33
+ if (method == "GET")
34
+ access_token = prepare_access_token(uri,keys)
35
+ params = post_data ? "?" + URI.encode_www_form(post_data) : ""
36
+ return access_token.request(:get, uri + params)
37
+ end
33
38
  params = params(@config['consumer_key'], @config['token'] )
34
39
  signature_base_string = signature_base_string(method, uri.to_s, params)
35
40
  signing_key = @config['consumer_secret'] + '&' + @config['token_secret']
@@ -78,25 +83,32 @@ module MagentoRestApi
78
83
  header.slice(0..-2)
79
84
  end
80
85
 
81
- def request_data(header, uri, method, post_data=nil)
86
+ def prepare_access_token(uri, keys)
87
+ uri = URI(uri)
88
+ consumer = OAuth::Consumer.new(keys['consumer_key'], keys['consumer_secret'], { :site => uri.host, :scheme => :header })
89
+ token_hash = { :oauth_token => keys['token'], :oauth_token_secret => keys['token_secret'] }
90
+ access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
91
+ return access_token
92
+ end
93
+
94
+ def request_data(header, uri, method, send_data=nil)
82
95
  uri = URI(uri)
83
96
  http = Net::HTTP.new(uri.host, uri.port)
84
97
  if (uri.port == 443)
85
98
  http.use_ssl = true
86
99
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
100
  end
88
- if method == 'POST'
89
- resp, data = http.post(uri.path, post_data, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
101
+ if method == 'DELETE'
90
102
  elsif method == 'PUT'
91
103
  request = Net::HTTP::Put.new(uri.path)
92
104
  headers = { 'Authorization' => header, "Content-Type" => "application/json" }
93
105
  headers.keys.each do |key|
94
106
  request[key] = headers[key]
95
107
  end
96
- request.body = post_data
108
+ request.body = send_data
97
109
  resp, data = http.request(request)
98
110
  else
99
- resp, data = http.get(uri.path, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
111
+ resp, data = http.post(uri.path, send_data, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
100
112
  end
101
113
  resp
102
114
  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.5
4
+ version: 0.1.6
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-14 00:00:00.000000000 Z
11
+ date: 2016-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler