tiendanube 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tiendanube.rb +10 -18
- data/lib/tiendanube/auth.rb +42 -0
- data/lib/tiendanube/resource.rb +113 -0
- metadata +19 -5
- data/lib/class_exten.rb +0 -6
- data/lib/tiendanube/store.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed29d36cceded71937b1cadf6253b79213676e45
|
4
|
+
data.tar.gz: e4f85a1a05a550b3d3f43e200c1a1057bec0a7de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5d3addb2faa315c1382fa960a560ee34a79d788aae6b7ae0bd9f479727078c207b5aca4127403a22ba6eda58f2e513fc5b571610372d73b2b9e0ac662b5d244
|
7
|
+
data.tar.gz: 079653131e573fa3fd72c188ee29aabf32f5d80a44ae104575f23f71725186146e98d1e3872457730ae13a04ed66d450cbe1fdc53fc2d96037fc32aefde5aff1
|
data/lib/tiendanube.rb
CHANGED
@@ -1,23 +1,15 @@
|
|
1
1
|
require 'net/https'
|
2
2
|
require 'json'
|
3
|
-
require '
|
4
|
-
require 'tiendanube/
|
3
|
+
# require './lib/tiendanube/resource'
|
4
|
+
# require './lib/tiendanube/auth'
|
5
|
+
require 'tiendanube/resource'
|
6
|
+
require 'tiendanube/auth'
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
+
module TiendaNube
|
9
|
+
API_URL = 'https://api.tiendanube.com'
|
8
10
|
API_Version = 'v1'
|
9
|
-
|
10
|
-
|
11
|
-
@store_id = params[:store]
|
12
|
-
@access_token = params[:access_token]
|
13
|
-
@https = Net::HTTP.new(URI_TiendaNube.host, URI_TiendaNube.port)
|
14
|
-
@https.use_ssl = true
|
15
|
-
@https.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
16
|
-
@https.ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs')
|
11
|
+
class << self
|
12
|
+
attr_accessor :access_token, :client_id, :client_secret, :store_id, :language
|
17
13
|
end
|
18
|
-
|
19
|
-
|
20
|
-
request['Authentication'] = "bearer #{@access_token}"
|
21
|
-
Store.new(@https.request(request).body)
|
22
|
-
end
|
23
|
-
end
|
14
|
+
self.language = 'es'
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module TiendaNube
|
2
|
+
class Auth
|
3
|
+
class << self
|
4
|
+
attr_accessor :client_id, :client_secret
|
5
|
+
end
|
6
|
+
Auth_URL = { :ar => "https://www.tiendanube.com/apps",
|
7
|
+
:br => "https://www.nuvemshop.com.br/apps"}
|
8
|
+
def self.config
|
9
|
+
yield self
|
10
|
+
end
|
11
|
+
def initialize(country = :ar)
|
12
|
+
@client_id = Auth.client_id
|
13
|
+
@client_secret = Auth.client_secret
|
14
|
+
@country = country
|
15
|
+
end
|
16
|
+
def authorize_url
|
17
|
+
[Auth_URL[@country], @client_id, "authorize"].join("/")
|
18
|
+
end
|
19
|
+
def get_access_token(code)
|
20
|
+
params = {
|
21
|
+
:client_id => @client_id,
|
22
|
+
:client_secret => @client_secret,
|
23
|
+
:grant_type => 'authorization_code',
|
24
|
+
:code => code
|
25
|
+
}
|
26
|
+
uri = URI([Auth_URL[@country], 'authorize/token'].join('/'))
|
27
|
+
request = Net::HTTP.post_form(uri ,params)
|
28
|
+
if request.code != "200"
|
29
|
+
return {:error => "server error"}
|
30
|
+
else
|
31
|
+
json = JSON.parse(request.body)
|
32
|
+
if json["error_description"]
|
33
|
+
{:error => json["error_description"]}
|
34
|
+
else
|
35
|
+
{ :store_id => json["user_id"],
|
36
|
+
:access_token => json["access_token"],
|
37
|
+
:scope => json["scope"] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module TiendaNube
|
2
|
+
module Resource
|
3
|
+
class << self
|
4
|
+
attr_accessor :api_url, :api_version, :store_id, :access_token
|
5
|
+
end
|
6
|
+
self.api_url = 'https://api.tiendanube.com'
|
7
|
+
self.api_version = 'v1'
|
8
|
+
def self.config
|
9
|
+
yield self
|
10
|
+
end
|
11
|
+
def self.base_url
|
12
|
+
"#{self.api_url}/#{self.api_version}/#{self.store_id}"
|
13
|
+
end
|
14
|
+
def self.send_request(uri, request)
|
15
|
+
request['Authentication'] = "bearer #{self.access_token}"
|
16
|
+
request['Content-Type'] = "application/json"
|
17
|
+
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
18
|
+
http.request request
|
19
|
+
end
|
20
|
+
result = {
|
21
|
+
:status => [response.code, response.message],
|
22
|
+
:headers => response.to_hash,
|
23
|
+
:body => JSON.parse(response.body)
|
24
|
+
}
|
25
|
+
if links = response["link"]
|
26
|
+
result[:pages] = links.split(", ").inject({}) do |hash, link|
|
27
|
+
array = link.split("; ")
|
28
|
+
hash[$1] = array[0].gsub(/^<(.*)>$/, '\1') if array[1].match(/rel=\"([a-z]+)\"/)
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
end
|
32
|
+
return result
|
33
|
+
end
|
34
|
+
def self.uri(path)
|
35
|
+
unless (uri=URI(path)).absolute?
|
36
|
+
uri = URI(self.base_url)
|
37
|
+
uri.path = "/#{self.api_version}/#{self.store_id}/#{path}"
|
38
|
+
end
|
39
|
+
return uri
|
40
|
+
end
|
41
|
+
def self.get(path, query={})
|
42
|
+
uri = self.uri(path)
|
43
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
44
|
+
request = Net::HTTP::Get.new uri
|
45
|
+
self.send_request(uri, request)
|
46
|
+
end
|
47
|
+
def self.delete(path)
|
48
|
+
uri = self.uri(path)
|
49
|
+
request = Net::HTTP::Delete.new uri
|
50
|
+
self.send_request(uri, request)
|
51
|
+
end
|
52
|
+
def self.post(path, data)
|
53
|
+
uri = self.uri(path)
|
54
|
+
request = Net::HTTP::Post.new uri
|
55
|
+
request.body = data.to_json
|
56
|
+
self.send_request(uri, request)
|
57
|
+
end
|
58
|
+
def self.put(path, data)
|
59
|
+
uri = self.uri(path)
|
60
|
+
request = Net::HTTP::Put.new uri
|
61
|
+
request.body = data.to_json
|
62
|
+
self.send_request(uri, request)
|
63
|
+
end
|
64
|
+
class Base
|
65
|
+
class << self
|
66
|
+
attr_accessor :name
|
67
|
+
end
|
68
|
+
def self.all(query={})
|
69
|
+
Resource.get(self.name, query)
|
70
|
+
end
|
71
|
+
def self.get(id)
|
72
|
+
Resource.get("#{self.name}/#{id}")
|
73
|
+
end
|
74
|
+
def self.delete(id)
|
75
|
+
Resource.delete("#{self.name}/#{id}")
|
76
|
+
end
|
77
|
+
def self.create(data)
|
78
|
+
Resource.post(self.name, data)
|
79
|
+
end
|
80
|
+
def self.update(id, data)
|
81
|
+
Resource.put("#{self.name}/#{id}", data)
|
82
|
+
end
|
83
|
+
def self.from_url(url)
|
84
|
+
Resource.get(url)
|
85
|
+
end
|
86
|
+
# class BaseDependant < Base
|
87
|
+
# def self.url(product_id, id=nil)
|
88
|
+
# end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
class Store
|
92
|
+
def self.get
|
93
|
+
Resource.get('store')
|
94
|
+
end
|
95
|
+
end
|
96
|
+
class Product < Resource::Base
|
97
|
+
self.name = 'products'
|
98
|
+
end
|
99
|
+
class Category < Resource::Base
|
100
|
+
self.name = 'categories'
|
101
|
+
end
|
102
|
+
class ProductVariant < Resource::Base
|
103
|
+
class << self
|
104
|
+
attr_accessor :product_id
|
105
|
+
end
|
106
|
+
self.name = 'variants'
|
107
|
+
def self.url(id = nil)
|
108
|
+
url = "#{Resource.base_url}/products/#{self.product_id}/#{self.name}"
|
109
|
+
url = "#{url}/#{id}" if id
|
110
|
+
return url
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiendanube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego de Estrada
|
@@ -9,17 +9,31 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
13
|
-
dependencies:
|
12
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.8'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.8'
|
14
28
|
description: A simple gem to use tiendanube's api
|
15
29
|
email: nicolas@tiendanube.com
|
16
30
|
executables: []
|
17
31
|
extensions: []
|
18
32
|
extra_rdoc_files: []
|
19
33
|
files:
|
20
|
-
- lib/class_exten.rb
|
21
34
|
- lib/tiendanube.rb
|
22
|
-
- lib/tiendanube/
|
35
|
+
- lib/tiendanube/auth.rb
|
36
|
+
- lib/tiendanube/resource.rb
|
23
37
|
homepage: http://rubygems.org/gems/tiendanube
|
24
38
|
licenses:
|
25
39
|
- MIT
|
data/lib/class_exten.rb
DELETED
data/lib/tiendanube/store.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
class TiendaNube
|
2
|
-
class Store
|
3
|
-
def initialize(data)
|
4
|
-
@data = JSON.parse(data)
|
5
|
-
end
|
6
|
-
def data
|
7
|
-
@data
|
8
|
-
end
|
9
|
-
def method_missing(method, *args, &block)
|
10
|
-
str_method = method.to_s
|
11
|
-
if @data.include?(str_method)
|
12
|
-
@data[str_method]
|
13
|
-
else
|
14
|
-
super
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|