Sha256: d8e7b7717cecfd6c5ed4a99d2ca93463791ada50bcd4d681a446451957a973ad

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

module Masheri
  class RpcClient
    def config
      Masheri.config
    end

    # Test Mashery API with echo service
    def echo(value)
      call_remote('test.echo', value)
    end

    def query_builder(klass)
      Masheri::QueryBuilder.new(klass)
    end

    # Use the Mashery query language to grab data
    def query(string)
      call_remote("object.query", params: string)
    end

    # Create an object
    def create(type, params)
      # TODO: Handle params!
      call_remote [type_name, "create"].join('.')
    end

    def call_remote(method, opts = {})
      perform('method' => method, 'params' => Array(opts[:params]), 'id' => 1)
    end

    def full_url
      build_url(config.site_id, config.key, config.signature)
    end

    alias :url :full_url

    protected

    def perform(params)
      JSON.parse(post!(full_url, params))
    end

    def post!(full_url, params)
      ::RestClient.post(full_url, params.to_json, {
        "Content-Type"   => "application/json",
        "Accept"         => "text/plain",
        "Content-Length" => params.size
      })
    end

    def build_url(site_id, apikey, signature)
      query      = URI.encode_www_form(apikey: apikey, sig: signature)
      uri        = URI::HTTP.build(host: config.host, path: "/v2/json-rpc/#{site_id}", query: query.to_s)
      uri.scheme = "https"
      uri.to_s
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
masheri-0.6.1 lib/masheri/rpc_client.rb
masheri-0.6.0 lib/masheri/rpc_client.rb
masheri-0.5.6 lib/masheri/rpc_client.rb
masheri-0.5.5 lib/masheri/rpc_client.rb
masheri-0.5.2 lib/masheri/rpc_client.rb
masheri-0.5.1 lib/masheri/rpc_client.rb
masheri-0.5.0 lib/masheri/rpc_client.rb
masheri-0.4.0 lib/masheri/rpc_client.rb