Sha256: d1530023708aed8cf515d3f64b068eb4144bff736e129c5089e9fa111b46d44d
Contents?: true
Size: 1.42 KB
Versions: 4
Compression:
Stored size: 1.42 KB
Contents
module AppleNews module Request class Post attr_reader :url attr_accessor :fields def initialize(url) @config = AppleNews.config @url = URI::parse(File.join(@config.api_base, url)) @fields = {} end def call http = Net::HTTP.new(@url.hostname, @url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER # http.set_debug_output $stderr res = http.post(@url, content_body, headers) # puts JSON.parse(res.body) JSON.parse(res.body) end private def multipart @multipart ||= Net::HTTP::Post::Multipart.new(@url.path, @fields, { parts: { 'metadata' => { 'Content-Type' => 'application/json' } } }) end def content_body body = multipart.body_stream.read multipart.body_stream.rewind body end def authorization security = AppleNews::Security.new('POST', @url) security.content_type = "multipart/form-data; boundary=#{Net::HTTP::Post::Multipart::DEFAULT_BOUNDARY}" security.content_body = content_body security.authorization end def headers { 'Authorization' => authorization, 'Content-Type' => "multipart/form-data; boundary=#{Net::HTTP::Post::Multipart::DEFAULT_BOUNDARY}" } end end end end
Version data entries
4 entries across 4 versions & 1 rubygems