Sha256: f13a3f5e80e37c4e32c6ac0041b575581104e509bf8706e4e458bed03a08bbd4

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 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

        # The API can be really slow, sometimes.
        http.read_timeout = 120
        

        res = http.post(@url, content_body, headers)
        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

7 entries across 7 versions & 1 rubygems

Version Path
apple-news-0.4.2 lib/apple-news/requests/post.rb
apple-news-0.4.1 lib/apple-news/requests/post.rb
apple-news-0.4.0 lib/apple-news/requests/post.rb
apple-news-0.3.1 lib/apple-news/requests/post.rb
apple-news-0.3.0 lib/apple-news/requests/post.rb
apple-news-0.2.5 lib/apple-news/requests/post.rb
apple-news-0.2.4 lib/apple-news/requests/post.rb