Class: DirectedEdge::Database

Inherits:
Resource
  • Object
show all
Defined in:
lib/directed_edge.rb

Overview

A Database is an encapsulation of a database being accessed via the Directed Edge web-services API. You can request database creation by visiting www.directededge.com and will recieve a user name and password which are then used to connect to your DirectedEdge::Database instance.

Usually when getting started with a DirectedEdge database, users would like to import some pre-existing data, usually from their web application’s database. The Database class has an import method which can be used to import data using Directed Edge’s XML format. Files formatted in that way may be created with the Exporter.

A database is typically instantiated via:

database = DirectedEdge::Database.new('mydatabase', 'mypassword')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, password = '', protocol = 'http', options = {}) ⇒ DirectedEdge::Item

Creates a connection to a Directed Edge database. The name and password should have been provided when the account was created.

Parameters:

  • name (String)

    User name given when the Directed Edge account was created.

  • password (String) (defaults to: '')

    Password given when the Directed Edge account was created.

  • protocol (String) (defaults to: 'http')

    The protocol to connect to the Directed Edge webservices with.



175
176
177
178
179
180
181
182
183
# File 'lib/directed_edge.rb', line 175

def initialize(name, password='', protocol='http', options = {})
  @name = name
  host = options[:host] || ENV['DIRECTEDEDGE_HOST'] || 'webservices.directededge.com'
  url = "#{protocol}://#{name}:#{password}@#{host}/api/v1/#{name}"

  options[:timeout] ||= 10

  super(RestClient::Resource.new(url, options))
end

Instance Attribute Details

#nameObject (readonly)

The name of the database.



157
158
159
# File 'lib/directed_edge.rb', line 157

def name
  @name
end

#resourceObject (readonly)

The REST resource used for connecting to the database.



161
162
163
# File 'lib/directed_edge.rb', line 161

def resource
  @resource
end

Instance Method Details

aggregate, commonly used to do recommendations for a basket of items.

The tags and params parameters are equivalent to those with the normal Item#related call.

Parameters:

  • items (Array) (defaults to: Set.new)

    List of items to base the recommendations on, e.g. all of the items in the basket.

Returns:

  • (Array)

    A set of recommendations for the set of items that is passed in in

See Also:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/directed_edge.rb', line 207

def group_related(items=Set.new, tags=Set.new, params={})
  if !items.is_a?(Array) || items.size < 1
    return with_properties?(params) ? InsertOrderHash.new : []
  end
  params['items'] = items.to_a.join(',')
  params['tags'] = tags.to_a.join(',')
  params['union'] = true
  normalize_params!(params)
  if params['includeProperties'] == 'true'
    property_hash_from_document(read_document('related', params), 'related')
  else
    list_from_document(read_document('related', params), 'related')
  end
end

#import(file_name) ⇒ Object

Imports a Directed Edge XML file to the database.

on the XML format.

See Also:

  • Exporter
  • site}[http://developer.directededge.com/] for more information


192
193
194
# File 'lib/directed_edge.rb', line 192

def import(file_name)
  @resource.put(File.read(file_name), :content_type => 'text/xml')
end