magento_client 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/MIT-LICENSE +20 -0
- data/README.md +22 -0
- data/lib/magento_client.rb +46 -0
- data/lib/magento_client/handshake.rb +58 -0
- data/lib/magento_client/version.rb +3 -0
- data/magento_client.gemspec +27 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 863a2bcdfb1270dd14149d8ba79ceaffd88d6e98
|
4
|
+
data.tar.gz: d823b7fd2902ed18899d70398b4ab69e2ab5482b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d8bd43b19c6aaf0ac764d6ee3552436467919698c63a7ab476fe22eec7e6125e6b49d48cee7ceca76a878a4baf782c58467f1594c1dfcf3fa2090fab308a6e09
|
7
|
+
data.tar.gz: c4230df3bf8820f9db387d7614b9e7fe2cea7665539538633b90f56ad519713065b0cf0133512d06dcbe43a2c5fa9538cbf2a1369797c5239c467fc761fe1294
|
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Nicholas Fetchak
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# MagentoClient
|
2
|
+
|
3
|
+
A Ruby interface to Magento's REST API
|
4
|
+
|
5
|
+
The main feature is automatic OAuth handshake with Magento - the OAuth access token is automatically fetched, so automated scripts can use Magento's REST API without needing any manual steps
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```
|
10
|
+
magento = MagentoClient.new({
|
11
|
+
:url => 'http://my_magento_store.com',
|
12
|
+
:key => 'OAuth Consumer Key',
|
13
|
+
:secret => 'OAuth Consumer Secret',
|
14
|
+
:username => 'Magento Admin username',
|
15
|
+
:password => 'Magento Admin password',
|
16
|
+
})
|
17
|
+
|
18
|
+
products = magento.get('/api/rest/products')
|
19
|
+
|
20
|
+
puts "Fetched #{products.count} products"
|
21
|
+
```
|
22
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'oauth'
|
2
|
+
require 'httparty'
|
3
|
+
require 'http-cookie'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'multi_json'
|
6
|
+
|
7
|
+
require 'magento_client/version'
|
8
|
+
require 'magento_client/handshake'
|
9
|
+
|
10
|
+
class MagentoClient
|
11
|
+
include Handshake
|
12
|
+
|
13
|
+
def initialize(config = {})
|
14
|
+
@config = OpenStruct.new(config)
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(params)
|
18
|
+
MultiJson.load(access_token.get(params).body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def access_token
|
22
|
+
return @access_token if @access_token
|
23
|
+
|
24
|
+
code = get_verifier_code
|
25
|
+
@access_token = request_token.get_access_token(:oauth_verifier => code)
|
26
|
+
end
|
27
|
+
|
28
|
+
def request_token
|
29
|
+
@request_token ||= consumer.get_request_token
|
30
|
+
end
|
31
|
+
|
32
|
+
def consumer
|
33
|
+
@consumer ||= OAuth::Consumer.new(@config.key, @config.secret,
|
34
|
+
:site => @config.url,
|
35
|
+
:request_token_path => "/index.php/oauth/initiate",
|
36
|
+
:authorize_path => "/index.php/admin/oauth_authorize",
|
37
|
+
:access_token_path => "/index.php/oauth/token",
|
38
|
+
:http_method => :get
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def reset
|
43
|
+
@consumer = @request_token = nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class MagentoClient
|
2
|
+
module Handshake
|
3
|
+
def get_verifier_code
|
4
|
+
jar = HTTP::CookieJar.new
|
5
|
+
|
6
|
+
authorize_url = request_token.authorize_url
|
7
|
+
response = HTTParty.get(authorize_url)
|
8
|
+
jar.parse(response.headers['set-cookie'], authorize_url)
|
9
|
+
|
10
|
+
doc = Nokogiri::HTML.fragment(response.body)
|
11
|
+
form = extract_form(doc.css('form#loginForm'))
|
12
|
+
form[:vars]['login[username]'] = @config.username
|
13
|
+
form[:vars]['login[password]'] = @config.password
|
14
|
+
|
15
|
+
|
16
|
+
response = HTTParty.post(form[:action],
|
17
|
+
:body => form[:vars],
|
18
|
+
:headers => { 'Cookie' => HTTP::Cookie.cookie_value(jar.cookies(form[:action])) }
|
19
|
+
)
|
20
|
+
jar.parse(response.headers['set-cookie'], form[:action])
|
21
|
+
|
22
|
+
doc = Nokogiri::HTML.fragment(response.body)
|
23
|
+
form = extract_form(doc.css('form#oauth_authorize_confirm'))
|
24
|
+
raise "Didn't get a token value" unless form[:vars]['oauth_token']
|
25
|
+
|
26
|
+
|
27
|
+
response = HTTParty.get(form[:action],
|
28
|
+
:query => form[:vars],
|
29
|
+
:headers => { 'Cookie' => HTTP::Cookie.cookie_value(jar.cookies(form[:action])) }
|
30
|
+
)
|
31
|
+
jar.parse(response.headers['set-cookie'], form[:action])
|
32
|
+
verifier_code = extract_verifier_code(response.body)
|
33
|
+
raise "Didn't get a verifier code" unless verifier_code
|
34
|
+
|
35
|
+
verifier_code
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def extract_verifier_code(body)
|
42
|
+
body.match(/Verifier code: (\w+)/) do
|
43
|
+
return $1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def extract_form(form)
|
48
|
+
{
|
49
|
+
:vars => form.css('input').each_with_object({}) do |e, obj|
|
50
|
+
obj[e.attr('name')] = e.attr('value')
|
51
|
+
end,
|
52
|
+
|
53
|
+
:action => form.attr('action')
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "magento_client/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "magento_client"
|
9
|
+
spec.version = MagentoClient::VERSION
|
10
|
+
spec.authors = ["Nicholas Fetchak"]
|
11
|
+
spec.email = ["gitcommits@fetchak.com"]
|
12
|
+
spec.summary = "A Ruby interface to Magento's REST API"
|
13
|
+
spec.description = "A Ruby interface to Magento's REST API with automatic OAuth handshaking"
|
14
|
+
spec.homepage = "https://github.com/fetchak/magento_client"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "oauth", "~> 0"
|
23
|
+
spec.add_dependency "httparty", "~> 0"
|
24
|
+
spec.add_dependency "http-cookie", "~> 1.0"
|
25
|
+
spec.add_dependency "nokogiri", "~> 1.6"
|
26
|
+
spec.add_dependency "multi_json", "~> 1"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magento_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Fetchak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: oauth
|
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: httparty
|
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: http-cookie
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nokogiri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.6'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: multi_json
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1'
|
83
|
+
description: A Ruby interface to Magento's REST API with automatic OAuth handshaking
|
84
|
+
email:
|
85
|
+
- gitcommits@fetchak.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- MIT-LICENSE
|
92
|
+
- README.md
|
93
|
+
- lib/magento_client.rb
|
94
|
+
- lib/magento_client/handshake.rb
|
95
|
+
- lib/magento_client/version.rb
|
96
|
+
- magento_client.gemspec
|
97
|
+
homepage: https://github.com/fetchak/magento_client
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.5
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: A Ruby interface to Magento's REST API
|
121
|
+
test_files: []
|
122
|
+
has_rdoc:
|