Class: Ailurus::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ailurus/client.rb

Overview

Public: Initialize a client object through which to interact with a PANDA server.

config - A Hash of configuration options, including, at a minimum:

:api_key  - An API key for a user on the PANDA server.
:domain   - The hostname of the PANDA server.
:email    - The email address of the PANDA user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ailurus/client.rb', line 39

def initialize(config = {})
  config.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  [
    {
      :description => "API key",
      :env_var => "PANDA_API_KEY",
      :instance_var => :@api_key
    },
    {
      :description => "email address",
      :env_var => "PANDA_EMAIL",
      :instance_var => :@email
    },
    {
      :description => "PANDA server domain",
      :env_var => "PANDA_DOMAIN",
      :instance_var => :@domain
    },
  ].each do |item|
    if not self.instance_variable_defined?(item[:instance_var])
      if not ENV.has_key?(item[:env_var])
        raise ArgumentError, (
          "No #{item[:description]} specified in arguments or " +
          "#{item[:env_var]} environment variable")
      end
      self.instance_variable_set(item[:instance_var], ENV[item[:env_var]])
    end
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



37
38
39
# File 'lib/ailurus/client.rb', line 37

def api_key
  @api_key
end

#domainObject

Returns the value of attribute domain.



37
38
39
# File 'lib/ailurus/client.rb', line 37

def domain
  @domain
end

#emailObject

Returns the value of attribute email.



37
38
39
# File 'lib/ailurus/client.rb', line 37

def email
  @email
end

Instance Method Details

#dataset(slug) ⇒ Object

Public: Return a Dataset instance with the given slug.

slug - The slug to a PANDA Dataset, as described at

http://panda.readthedocs.org/en/1.1.1/api.html#datasets

Returns an Ailurus::Dataset.



125
126
127
# File 'lib/ailurus/client.rb', line 125

def dataset(slug)
  Ailurus::Dataset.new(self, slug)
end

#make_request(endpoint, options = {}) ⇒ Object

Public: Return the parsed JSON from a given API endpoint after adding the appropriate domain and authentication parameters.

endpoint - The path component of the URL to the desired API endpoint

(e.g., /api/1.0/dataset/).

options - A Hash of additional options for the request:

:query  - A Hash of query-string parameters to add to the
          request (default: none).
:method - A Symbol specifying the HTTP method for the request
          (default: :get).
:body   - An object to be converted to JSON and used as the
          request body (default: empty).

Returns the parsed JSON response, regardless of type.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ailurus/client.rb', line 86

def make_request(endpoint, options = {})
  # Handle default option values.
  query = options.fetch(:query, {})
  method = options.fetch(:method, :get)
  body = options.fetch(:body, nil)

  req_url = URI.join(Ailurus::Utils::get_absolute_uri(@domain), endpoint)
  auth_params = {
    :format => "json",
    :email => @email,
    :api_key => @api_key
  }
  req_url.query = URI.encode_www_form(auth_params.merge(query))

  req_class = Net::HTTP.const_get(method.to_s.capitalize)
  req = req_class.new(req_url.request_uri)

  if not body.nil?
    req.body = JSON.generate(body)
    req.content_type = "application/json"
  end

  res = Net::HTTP.start(req_url.hostname, req_url.port) do |http|
    http.request(req)
  end

  if res.body && res.body.length >= 2
    JSON.parse(res.body, :object_class => OpenStruct)
  else
    nil
  end
end