Sha256: f2a98a8029ca3952c10fa36671216e0fa1e995cc5d4540b9a41d0dfe61de9cbe

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

require "oembedr/parsed_url"
require "faraday_middleware"

module Oembedr
  class Client
    attr_accessor :resource_url, :parsed_url

    def initialize url
      self.resource_url = url
      self.parsed_url = ParsedUrl.new(url)

    end

    # Sets up a Faraday::Connection object
    #
    # @param options [optional]
    #
    # @return Faraday::Connection
    def connection options = {}
      the_options = {
        :headers => {
          "Accept" => "application/json",
          "User-Agent" => Oembedr.user_agent
        },
        :url => parsed_url.host
      }.merge(options)

      Faraday.new(the_options) do |builder|
        builder.use Faraday::Request::UrlEncoded
        builder.use Faraday::Response::ParseJson
        builder.use Faraday::Response::RaiseError
        builder.adapter Oembedr.adapter
      end
    end

    # Requests the oembeddable resource
    #
    # @return Faraday::Response
    def get options = {}
      additional_params = options.delete(:params) || {}
      connection(options).get do |req|
        req.url parsed_url.path
        req.params = additional_params.merge({
          :url => resource_url,
          :format => "json"
        })
      end
    end

    # Returns a boolean true/false if we can process this request
    #
    # @return Boolean
    def ready?
      !!parsed_url.host
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oembedr-1.1.1 lib/oembedr/client.rb
oembedr-1.1.0 lib/oembedr/client.rb
oembedr-1.0.0 lib/oembedr/client.rb
oembedr-0.0.3 lib/oembedr/client.rb
oembedr-0.0.2 lib/oembedr/client.rb