Sha256: ac52d046d7d3a88d6055c26220bb832a392c7be58ab599061efb79b7323bf268

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'faraday'
require 'faraday_middleware'

module MadMimi
  # Defines HTTP request methods
  module Request
    def get(path, options={}, raw = false)
      request(:get, path, options, raw)
    end

    def post(path, options={}, raw = false, ssl = false)
      request(:post, path, options, raw, ssl)
    end

    private
      def request(method, path, options, raw = false, ssl = false)
        response = connection(raw, ssl).send(method) do |request|
          options = options.merge(MadMimi.authentication)
          case method
          when :get
            request.url(path, options)
          when :post
            request.path = path
            request.body = options unless options.empty?
          end
        end
        raw ? response : response.body
      end

      def connection(raw = false, ssl = false)
        options = {
          :headers => {'Accept' => "application/xml", 'User-Agent' => 'MadMimi Gem'},
          :ssl => { :verify => false },
          :url => MadMimi.api_url(ssl)
        }

        Faraday::Connection.new(options) do |builder|
          builder.adapter Faraday.default_adapter
          unless raw
            builder.use Faraday::Response::ParseXml
          end
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mad_mimi-0.0.3 lib/mad_mimi/request.rb
mad_mimi-0.0.2 lib/mad_mimi/request.rb