Sha256: 9ceb6a8ba6b3d19d63f09d945a6cbb6af33c45fb24c7c91afb66bd7dc288bf05

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

module Mashery
  class RpcClient
    def config
      Mashery.config
    end

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

    def query_builder(klass)
      Mashery::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
mashery_rails-0.6.9.pre4 lib/mashery/rpc_client.rb
mashery_rails-0.6.9.pre3 lib/mashery/rpc_client.rb
mashery_rails-0.6.9.pre1 lib/mashery/rpc_client.rb
mashery_rails-0.6.8 lib/mashery/rpc_client.rb
mashery_rails-0.6.7 lib/mashery/rpc_client.rb
mashery_rails-0.6.6 lib/mashery/rpc_client.rb
mashery_rails-0.6.3 lib/mashery/rpc_client.rb
mashery_rails-0.6.2 lib/mashery/rpc_client.rb