Sha256: fc90dce1080ec24f5707034dd5ebbc5adf5dd7f387c5e636ab5ba5be5e62f469

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'uri'
require 'faraday_middleware'

module Roe
  class Client
    attr_accessor :endpoint, :format
    attr_accessor *Configuration::VALID_OPTIONS_KEYS

    def initialize(endpoint, format = :json)
      @endpoint = endpoint
      @format = format

      Configuration::VALID_OPTIONS_KEYS.each do |key|
        send("#{key}=", Roe.options[key])
      end
    end

    def resolve(uri, options={})
      response = connection.get do |req|
        req.url endpoint_path, options.merge!(:url => uri, :format => format)
      end
      response.body.oembed || response.body
    end

    private
    def connection
      merged_options = connection_options.merge({
        :headers => {
          'Accept' => "application/#{format}",
          'User-Agent' => user_agent
        },
        :proxy => proxy,
        :url => endpoint_host
      })

      Faraday.new(merged_options) do |builder|
        builder.use Faraday::Request::UrlEncoded
        builder.use Faraday::Response::Rashify
        case format.to_s
        when 'xml'
          builder.use Faraday::Response::ParseXml
        when 'json'
          builder.use Faraday::Response::ParseJson
        end
        builder.use Faraday::Response::RaiseError
        builder.adapter(adapter)
      end
    end

    def endpoint_path
      parsed_endpoint.path
    end

    def endpoint_host
      parsed_endpoint.scheme + '://' + parsed_endpoint.host
    end

    def parsed_endpoint
      @parsed_endpoint ||= URI.parse(@endpoint)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roe-0.1.0 lib/roe/client.rb