lib/peddler/client.rb in peddler-1.6.0 vs lib/peddler/client.rb in peddler-1.6.1
- old
+ new
@@ -127,17 +127,11 @@
@version ||= self.class.version
end
# @!parse attr_writer :body
def body=(str)
- if str
- headers['Content-Type'] = content_type(str)
- else
- headers.delete('Content-Type')
- end
-
- @body = str
+ str ? add_content(str) : clear_content!
end
# @api private
def defaults
@defaults ||= { expects: 200 }
@@ -167,10 +161,11 @@
# @api private
def run
opts = build_options
opts.store(:response_block, Proc.new) if block_given?
res = post(opts)
+ self.body = nil if res.status == 200
parser.new(res, encoding)
rescue Excon::Error => e
handle_error(e)
end
@@ -179,14 +174,22 @@
def find_marketplace
Marketplace.new(primary_marketplace_id)
end
- def content_type(str)
- if str.start_with?('<?xml')
- 'text/xml'
+ def clear_content!
+ headers.delete('Content-Type')
+ @body = nil
+ end
+
+ def add_content(content)
+ if content.start_with?('<?xml')
+ headers['Content-Type'] = 'text/xml'
+ @body = content
else
- "text/tab-separated-values; charset=#{encoding}"
+ headers['Content-Type'] =
+ "text/tab-separated-values; charset=#{encoding}"
+ @body = content.encode(encoding)
end
end
def extract_options(args)
args.last.is_a?(Hash) ? args.pop : {}