Sha256: f70023b764ea3fbc86b7084a0e6897663139b058f25ed0ec0783aa207c10fc63

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 Bytes

Contents

require 'httparty'
require 'yajl'
require 'addressable/uri'
require 'wp/api/endpoints'

module WP::API
  class Client
    include HTTParty
    include Endpoints

    attr_accessor :host

    def initialize(host:, scheme: 'http')
      @scheme = scheme
      @host = host
      fail ':host is required' unless host.is_a?(String) && host.length > 0
    end

    protected

    def get(resource, query = {})
      path = url_for(resource, query)
      body = Client.get(path).body
      parse(body)
    end

    private

    def url_for(fragment, query)
      url = "#{@scheme}://#{@host}/wp-json/#{fragment}"
      url << ("?" + params(query)) unless query.empty?

      url
    end

    def params(query)
      uri = Addressable::URI.new
      uri.query_values = query
      uri.query
    end

    def parse(string)
      @parser ||= Yajl::Parser.new
      @parser.parse(string)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wp-api-0.0.2 lib/wp/api/client.rb