Sha256: 6fd5b93f3919b18e7e1f569c85b736286df6dca3d884af441243a4903d4a5057

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# coding: utf-8
require 'base64'
require 'json'
require 'net/http'

module Evostream
  class Client
    # Define the same set of accessors as the Evostream module
    attr_accessor *Configuration::VALID_CONFIG_KEYS

    def initialize(options = {})
      # Merge the config values from the module and those passed
      # to the client.
      merged_options = Evostream.options.merge(options)

      # Copy the merged values to this client and ignore those
      # not part of our configuration
      Configuration::VALID_CONFIG_KEYS.each do |key|
        send("#{key}=", merged_options[key])
      end
    end

    def method_missing(method, *args)
      params = !args.empty? ? args.first : {}
      response = api_call(method, params)
      json = JSON.parse(response.body)
      if json['status'] == 'SUCCESS'
        json['data']
      else
        super
      end
    end

    private
    def api_call(method, params)
      url = URI.parse(service_url(method, params))
      request = Net::HTTP::Get.new(url.to_s)
      response = Net::HTTP.start(url.host, url.port) {|http|
        http.request(request)
      }
      response
    end

    def service_url(service, params)
      url = "#{base_url}/#{service}"
      unless params.empty?
        url + "?params=#{encode_params(params)}"
      else
        url
      end
    end

    def base_url
      "http://#{@host}:#{@port}#{@path_prefix}"
    end

    def encode_params(params)
      Base64.encode64(params.map {|k, v| "#{k}=#{v}" }.join(' ')).chomp
    end

  end # Client
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evostream-0.0.2 lib/evostream/client.rb