kauplus 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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +1 -0
- data/kauplus.gemspec +24 -0
- data/lib/kauplus.rb +9 -0
- data/lib/kauplus/api.rb +22 -0
- data/lib/kauplus/client.rb +116 -0
- data/lib/kauplus/config.rb +40 -0
- data/lib/kauplus/constants.rb +102 -0
- data/lib/kauplus/error.rb +18 -0
- data/lib/kauplus/version.rb +3 -0
- metadata +80 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/kauplus.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "kauplus/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "kauplus"
|
7
|
+
s.version = Kauplus::VERSION
|
8
|
+
s.authors = ["Kauplus Social Commerce"]
|
9
|
+
s.email = ["suporte@kauplus.com.br"]
|
10
|
+
s.homepage = "http://dev.kauplus.com.br/"
|
11
|
+
s.summary = %q{Kauplus - Cliente da API}
|
12
|
+
s.description = %q{Esta gem é um cliente que permite desenvolvedores se conectarem aos web services do Kauplus.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "kauplus"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# dependencies
|
22
|
+
s.add_dependency 'rest-client', '>= 1.6.6'
|
23
|
+
s.add_dependency 'activesupport', '>= 3.1.0'
|
24
|
+
end
|
data/lib/kauplus.rb
ADDED
data/lib/kauplus/api.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Kauplus
|
2
|
+
|
3
|
+
#
|
4
|
+
# Kauplus.api references to Kauplus::API
|
5
|
+
#
|
6
|
+
def self.api
|
7
|
+
API
|
8
|
+
end
|
9
|
+
|
10
|
+
class API
|
11
|
+
|
12
|
+
|
13
|
+
#
|
14
|
+
# Enables dynamic programming for the generation of HTTP resources.
|
15
|
+
#
|
16
|
+
def self.method_missing(method_name, *args)
|
17
|
+
Client.new :resource => method_name
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'rest_client'
|
3
|
+
|
4
|
+
module Kauplus
|
5
|
+
|
6
|
+
class Client
|
7
|
+
|
8
|
+
attr_accessor :resource
|
9
|
+
|
10
|
+
#
|
11
|
+
# Creates a new client with its resource_method.
|
12
|
+
#
|
13
|
+
def initialize args = {}
|
14
|
+
self.resource = args[:resource] || ''
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Enables dynamic programming for the generation of HTTP methods.
|
19
|
+
#
|
20
|
+
def method_missing(method_name, *args)
|
21
|
+
payload = Kauplus::Client.authenticate_payload(args.first)
|
22
|
+
if method_name.match(/(get|post|multipart_post|put|delete|head)_(.*)/)
|
23
|
+
Kauplus::Client.send($1, "#{@resource}/#{$2}", payload)
|
24
|
+
else
|
25
|
+
Kauplus::Client.get("#{@resource}/#{method_name}", payload)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Private methods.
|
31
|
+
#
|
32
|
+
private
|
33
|
+
|
34
|
+
#
|
35
|
+
# Sends an HTTP GET Request to the Kauplus Web Services
|
36
|
+
# and parses the json response.
|
37
|
+
#
|
38
|
+
def self.get(resource_method, payload = {})
|
39
|
+
url = payload.empty? ? url_for_resource_method(resource_method) : "#{url_for_resource_method(resource_method)}?#{payload.to_query}"
|
40
|
+
parse_json RestClient.get(url)
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Sends an HTTP POST Request to the Kauplus Web Services
|
45
|
+
# and parses the json response.
|
46
|
+
#
|
47
|
+
def self.post(resource_method, payload)
|
48
|
+
parse_json RestClient.post(url_for_resource_method(resource_method), payload.to_query)
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Sends an HTTP MULTIPART POST Request to the Kauplus Web Services
|
53
|
+
# and parses the json response.
|
54
|
+
#
|
55
|
+
def self.multipart_post(resource_method, payload)
|
56
|
+
self.post(resource_method, payload.merge(:multipart => true))
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Sends an HTTP PUT Request to the Kauplus Web Services
|
61
|
+
# and parses the json response.
|
62
|
+
#
|
63
|
+
def self.put(resource_method, payload)
|
64
|
+
parse_json RestClient.put(url_for_resource_method(resource_method), payload.to_query)
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Sends an HTTP DELETE Request to the Kauplus Web Services
|
69
|
+
# and parses the json response.
|
70
|
+
#
|
71
|
+
def self.delete(resource_method, payload = {})
|
72
|
+
parse_json RestClient.delete("#{url_for_resource_method(resource_method)}?#{payload.to_query}")
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# Sends an HTTP HEAD Request to the Kauplus Web Services.
|
77
|
+
#
|
78
|
+
def self.head(resource_method, payload = {})
|
79
|
+
RestClient.head("#{url_for_resource_method(resource_method)}?#{payload.to_query}")
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Authenticates the payload by adding the api_key argument to it.
|
84
|
+
#
|
85
|
+
def self.authenticate_payload(payload)
|
86
|
+
if payload.blank?
|
87
|
+
payload = {}
|
88
|
+
end
|
89
|
+
if payload[:api_key].blank? and payload['api_key'].blank?
|
90
|
+
payload[:api_key] = Kauplus.config.api_key
|
91
|
+
end
|
92
|
+
payload
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Returns the URL for consuming a specific resource_method.
|
97
|
+
#
|
98
|
+
def self.url_for_resource_method(resource_method)
|
99
|
+
"https://shop.kauplus.com.br/#{resource_method}"
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# Parses the json response.
|
104
|
+
# Raises a Kauplus::Error if the response is invalid and raise errors is active.
|
105
|
+
#
|
106
|
+
def self.parse_json body
|
107
|
+
parsed_response = JSON.load(body).symbolize_keys
|
108
|
+
if Kauplus.config.raise_errors and parsed_response[:code].to_s[0] != '6'
|
109
|
+
raise Kauplus::Error.new(:code => parsed_response[:code], :error => parsed_response[:error], :messages => parsed_response[:messages])
|
110
|
+
end
|
111
|
+
parsed_response
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Kauplus
|
4
|
+
|
5
|
+
def self.config
|
6
|
+
Kauplus::Config.instance
|
7
|
+
end
|
8
|
+
|
9
|
+
#
|
10
|
+
# Private.
|
11
|
+
#
|
12
|
+
private
|
13
|
+
|
14
|
+
class Config
|
15
|
+
include Singleton
|
16
|
+
|
17
|
+
attr_accessor :api_key, :raise_errors
|
18
|
+
|
19
|
+
#
|
20
|
+
# Private methods.
|
21
|
+
#
|
22
|
+
private
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
# load YAML config if possible
|
26
|
+
if defined? Rails
|
27
|
+
path = "#{Rails.root}/config/kauplus.yml"
|
28
|
+
env = Rails.env
|
29
|
+
else
|
30
|
+
path = "#{Dir.pwd}/config/kauplus.yml"
|
31
|
+
env = 'kauplus'
|
32
|
+
end
|
33
|
+
conf = File.exists?(path) ? YAML.load_file(path)[env] : {}
|
34
|
+
self.api_key = conf['api_key']
|
35
|
+
self.raise_errors = conf['raise_errors'] || false
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Kauplus
|
2
|
+
|
3
|
+
#
|
4
|
+
# Constants
|
5
|
+
#
|
6
|
+
|
7
|
+
#
|
8
|
+
# Conditions of a product
|
9
|
+
#
|
10
|
+
CONDITION = {
|
11
|
+
:new => 1, # Novo
|
12
|
+
:used => 2, # Usado
|
13
|
+
:seminew => 3 # Seminovo
|
14
|
+
}
|
15
|
+
|
16
|
+
#
|
17
|
+
# Available shipping methods
|
18
|
+
#
|
19
|
+
SHIPPING = {
|
20
|
+
:no => 1, # Sem frete
|
21
|
+
:free => 2, # Frete grátis
|
22
|
+
:paid => 3 # Frete pago
|
23
|
+
}
|
24
|
+
|
25
|
+
#
|
26
|
+
# Paid shipping methods
|
27
|
+
#
|
28
|
+
PAID_SHIPPING = {
|
29
|
+
:fixed => 1, # Frete fixo
|
30
|
+
:additional_fixed => 2, # Frete adicional com valor fixo
|
31
|
+
:correios => 3 # Frete dos correios
|
32
|
+
}
|
33
|
+
|
34
|
+
#
|
35
|
+
# Shipping package formats
|
36
|
+
#
|
37
|
+
PACKAGE_FORMAT = {
|
38
|
+
:box => 1, # Caixa/Pacote
|
39
|
+
:cylinder => 2 # Rolo/Prisma
|
40
|
+
}
|
41
|
+
|
42
|
+
#
|
43
|
+
# Correios Web Services types and codes
|
44
|
+
#
|
45
|
+
CORREIOS_SERVICES_TYPES = {
|
46
|
+
:pac=>[41106, "PAC sem contrato"],
|
47
|
+
:sedex=>[40010, "SEDEX sem contrato"],
|
48
|
+
:sedex_a_cobrar=>[40045, "SEDEX a Cobrar, sem contrato"],
|
49
|
+
:sedex_a_cobrar_com_contrato=>[40126, "SEDEX a Cobrar, com contrato"],
|
50
|
+
:sedex_10=>[40215, "SEDEX 10, sem contrato"],
|
51
|
+
:sedex_hoje=>[40290, "SEDEX Hoje, sem contrato"],
|
52
|
+
:sedex_com_contrato_1=>[40096, "SEDEX com contrato"],
|
53
|
+
:sedex_com_contrato_2=>[40436, "SEDEX com contrato"],
|
54
|
+
:sedex_com_contrato_3=>[40444, "SEDEX com contrato"],
|
55
|
+
:sedex_com_contrato_4=>[40568, "SEDEX com contrato"],
|
56
|
+
:sedex_com_contrato_5=>[40606, "SEDEX com contrato"],
|
57
|
+
:e_sedex_com_contrato=>[81019, "e-SEDEX, com contrato"],
|
58
|
+
:pac_com_contrato=>[41068, "PAC com contrato"],
|
59
|
+
:e_sedex_com_contrato_grupo_1=>[81868, "(Grupo 1) e-SEDEX, com contrato"],
|
60
|
+
:e_sedex_com_contrato_grupo_2=>[81833, "(Grupo 2) e-SEDEX, com contrato"],
|
61
|
+
:e_sedex_com_contrato_grupo_3=>[81850, "(Grupo 3) e-SEDEX, com contrato"]
|
62
|
+
}
|
63
|
+
|
64
|
+
CORREIOS_SERVICES_CODES = Hash[CORREIOS_SERVICES_TYPES.map {|k,v| [ v[0], [k, v[1]] ] } ]
|
65
|
+
|
66
|
+
#
|
67
|
+
# Order possible statuses
|
68
|
+
#
|
69
|
+
ORDER_STATUS = {
|
70
|
+
:order_received => 1,
|
71
|
+
:waiting_payment_confirmation => 2,
|
72
|
+
:payment_confirmed => 3,
|
73
|
+
:shipped => 4,
|
74
|
+
:canceled => 5,
|
75
|
+
:finished => 6
|
76
|
+
}
|
77
|
+
|
78
|
+
#
|
79
|
+
# Possible events for an order
|
80
|
+
#
|
81
|
+
ORDER_EVENT = {
|
82
|
+
:receive_order => 1,
|
83
|
+
:wait_payment => 2,
|
84
|
+
:confirm_payment => 3,
|
85
|
+
:reject_payment => 4,
|
86
|
+
:confirm_shipping => 5,
|
87
|
+
:confirm_delivery => 6
|
88
|
+
}
|
89
|
+
|
90
|
+
#
|
91
|
+
# Wizard status
|
92
|
+
#
|
93
|
+
WIZARD_STATUS = {
|
94
|
+
:store_created => 1,
|
95
|
+
:store_setup => 2,
|
96
|
+
:shipping_setup => 3,
|
97
|
+
:categories_setup => 4,
|
98
|
+
:products_setup => 5,
|
99
|
+
:store_active => 6
|
100
|
+
}
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Kauplus
|
2
|
+
class Error < StandardError
|
3
|
+
|
4
|
+
attr_accessor :code, :error, :messages
|
5
|
+
|
6
|
+
def initialize(args = {})
|
7
|
+
@code = args[:code]
|
8
|
+
@error = args[:error]
|
9
|
+
@messages = args[:messages]
|
10
|
+
super(@error)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"(#{@code}) #{super}"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kauplus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kauplus Social Commerce
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-24 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: &2156325300 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.6
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156325300
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &2156317860 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.1.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156317860
|
36
|
+
description: Esta gem é um cliente que permite desenvolvedores se conectarem aos web
|
37
|
+
services do Kauplus.
|
38
|
+
email:
|
39
|
+
- suporte@kauplus.com.br
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README
|
47
|
+
- Rakefile
|
48
|
+
- kauplus.gemspec
|
49
|
+
- lib/kauplus.rb
|
50
|
+
- lib/kauplus/api.rb
|
51
|
+
- lib/kauplus/client.rb
|
52
|
+
- lib/kauplus/config.rb
|
53
|
+
- lib/kauplus/constants.rb
|
54
|
+
- lib/kauplus/error.rb
|
55
|
+
- lib/kauplus/version.rb
|
56
|
+
homepage: http://dev.kauplus.com.br/
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project: kauplus
|
76
|
+
rubygems_version: 1.8.10
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Kauplus - Cliente da API
|
80
|
+
test_files: []
|