kauplus 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,9 @@ require 'kauplus/api'
2
2
  require 'kauplus/client'
3
3
  require 'kauplus/config'
4
4
  require 'kauplus/constants'
5
+ require 'kauplus/params'
5
6
  require 'kauplus/error'
6
- require "kauplus/version"
7
+ require 'kauplus/version'
7
8
 
8
9
  module Kauplus
9
10
  end
@@ -45,15 +45,7 @@ module Kauplus
45
45
  # and parses the json response.
46
46
  #
47
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))
48
+ parse_json RestClient.post(url_for_resource_method(resource_method), Params.generate(payload))
57
49
  end
58
50
 
59
51
  #
@@ -61,7 +53,7 @@ module Kauplus
61
53
  # and parses the json response.
62
54
  #
63
55
  def self.put(resource_method, payload)
64
- parse_json RestClient.put(url_for_resource_method(resource_method), payload.to_query)
56
+ parse_json RestClient.put(url_for_resource_method(resource_method), Params.generate(payload))
65
57
  end
66
58
 
67
59
  #
@@ -96,7 +88,7 @@ module Kauplus
96
88
  # Returns the URL for consuming a specific resource_method.
97
89
  #
98
90
  def self.url_for_resource_method(resource_method)
99
- "https://shop.kauplus.com.br/#{resource_method}"
91
+ "https://shop.kauplus.com.br/#{resource_method}"
100
92
  end
101
93
 
102
94
  #
@@ -107,7 +99,11 @@ module Kauplus
107
99
  parsed_response = JSON.load(body)
108
100
  if Kauplus.config.process_response
109
101
  if parsed_response['code'].to_s[0] == '6'
110
- parsed_response = parsed_response['data']
102
+ if parsed_response['summary']
103
+ parsed_response = {'data' => parsed_response['data'], 'summary' => parsed_response['summary']}
104
+ else
105
+ parsed_response = parsed_response['data']
106
+ end
111
107
  else
112
108
  raise Kauplus::Error.new(
113
109
  :code => parsed_response['code'],
@@ -0,0 +1,49 @@
1
+ module Kauplus
2
+
3
+ #
4
+ # Deals with post/put parameters, generating parameters with
5
+ # the correct keys. If our pull request to RestClient is accepted,
6
+ # this won't be needed anymore.
7
+ #
8
+ class Params
9
+
10
+ def self.generate payload
11
+ (payload.is_a? Hash) ? generate_params_list(payload) : payload
12
+ end
13
+
14
+ def self.generate_params_list payload
15
+ parameters = []
16
+ # Build array with all parameter values
17
+ values = extract_values(payload)
18
+
19
+ # Generate query string with the correct keys
20
+ # and extract each key-value pair of the form
21
+ # "name=value" as key_value
22
+ payload.to_query.split('&').each do |key_value|
23
+ unless key_value.blank?
24
+ key, value = key_value.split('=')
25
+ index = values.find_index{|v| v.to_query(nil).split('=')[1] == value}
26
+ if index
27
+ parameters << [URI.unescape(key), values.delete_at(index)]
28
+ else
29
+ raise "Could not find representation for #{URI.unescape(key)}"
30
+ end
31
+ end
32
+ end
33
+ parameters
34
+ end
35
+
36
+ def self.extract_values(payload, values=[])
37
+ if payload.is_a? Hash
38
+ payload.each {|k, v| extract_values(v, values)}
39
+ elsif payload.is_a? Array
40
+ payload.each {|v| extract_values(v, values)}
41
+ else
42
+ values << payload
43
+ end
44
+ values
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Kauplus
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,46 +1,50 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: kauplus
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.1.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Kauplus Social Commerce
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-25 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2012-01-23 00:00:00 -02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
15
17
  name: rest-client
16
- requirement: &2152725640 !ruby/object:Gem::Requirement
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
17
20
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
21
24
  version: 1.6.6
22
25
  type: :runtime
23
- prerelease: false
24
- version_requirements: *2152725640
25
- - !ruby/object:Gem::Dependency
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
26
28
  name: activesupport
27
- requirement: &2152724940 !ruby/object:Gem::Requirement
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
28
31
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
32
35
  version: 3.1.0
33
36
  type: :runtime
34
- prerelease: false
35
- version_requirements: *2152724940
36
- description: Esta gem é um cliente que permite desenvolvedores se conectarem aos web
37
- services do Kauplus.
38
- email:
37
+ version_requirements: *id002
38
+ description: "Esta gem \xC3\xA9 um cliente que permite desenvolvedores se conectarem aos web services do Kauplus."
39
+ email:
39
40
  - suporte@kauplus.com.br
40
41
  executables: []
42
+
41
43
  extensions: []
44
+
42
45
  extra_rdoc_files: []
43
- files:
46
+
47
+ files:
44
48
  - .gitignore
45
49
  - Gemfile
46
50
  - README
@@ -52,29 +56,35 @@ files:
52
56
  - lib/kauplus/config.rb
53
57
  - lib/kauplus/constants.rb
54
58
  - lib/kauplus/error.rb
59
+ - lib/kauplus/params.rb
55
60
  - lib/kauplus/version.rb
61
+ has_rdoc: true
56
62
  homepage: http://dev.kauplus.com.br/
57
63
  licenses: []
64
+
58
65
  post_install_message:
59
66
  rdoc_options: []
60
- require_paths:
67
+
68
+ require_paths:
61
69
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
70
+ required_ruby_version: !ruby/object:Gem::Requirement
63
71
  none: false
64
- requirements:
65
- - - ! '>='
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
77
  none: false
70
- requirements:
71
- - - ! '>='
72
- - !ruby/object:Gem::Version
73
- version: '0'
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
74
82
  requirements: []
83
+
75
84
  rubyforge_project: kauplus
76
- rubygems_version: 1.8.10
85
+ rubygems_version: 1.6.2
77
86
  signing_key:
78
87
  specification_version: 3
79
88
  summary: Kauplus - Cliente da API
80
89
  test_files: []
90
+