Module: Dydra::Client

Included in:
Command
Defined in:
lib/dydra/client.rb

Overview

Implements a Dydra.com XML-RPC API client.

See Also:

Class Method Summary (collapse)

Class Method Details

+ authenticate!(options = {})

This method returns an undefined value.

Authenticates with Dydra.com.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :token (String) — default: nil
  • :user (String) — default: nil
  • :password (String) — default: nil


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dydra/client.rb', line 37

def self.authenticate!(options = {})
  $dydra ||= {}
  if options[:token]
    $dydra[:token] = options[:token]
  elsif options[:user]
    $dydra[:user] = options[:user]
    $dydra[:pass] = options[:password] || options[:pass] || raise(AuthenticationError, "You must supply a password")
  elsif File.exists?(credentials_file)
    require 'yaml'
    $dydra.merge!(YAML.load_file(credentials_file))
    if !$dydra[:user] && !($dydra[:pass] || $dydra[:token])
      raise AuthenticationError, "You need to specify :user and :pass or :token in #{credentials_file}"
    end
  elsif ENV['DYDRA_TOKEN']
    $dydra[:token] = ENV['DYDRA_TOKEN']
  else
    raise AuthenticationError, "You need to give either :user and :password, or :token, or set your ENV['DYDRA_TOKEN'] to authenticate."
  end
end

+ (String) credentials_file

Returns the path of the file which we'll read and write credentials to.

Returns:

  • (String)


162
163
164
# File 'lib/dydra/client.rb', line 162

def self.credentials_file
  File.join(ENV['HOME'], '.dydra', 'credentials')
end

+ (Object) delete(where)

DELETE from the Dydra.com REST API.

Parameters:

  • where (String)

Returns:

  • (Object)


85
86
87
# File 'lib/dydra/client.rb', line 85

def self.delete(where)
  resource(where).delete({:user_agent => "Dydra API client #{Dydra::VERSION}"})
end

+ (Object) get(where, options = {})

GET from the Dydra.com REST API.

Parameters:

  • where (String)
  • any (Hash{Symbol => Object})

    additional options

Returns:

  • (Object)


76
77
78
# File 'lib/dydra/client.rb', line 76

def self.get(where, options = {})
  resource(where).get({:accept => :json, :user_agent => "Dydra API client #{Dydra::VERSION}"}.merge(options))
end

+ (Hash) get_json(where, options = {})

GET parsed JSON from the Dydra.com REST API.

Parameters:

  • where (String)
  • any (Hash{Symbol => Object})

    additional options

Returns:

  • (Hash)


65
66
67
# File 'lib/dydra/client.rb', line 65

def self.get_json(where, options = {})
  JSON.parse(get(where, options))
end

+ (Object) post(where, what, options = {})

POST to the Dydra.com REST API.

Parameters:

  • where (String)
  • any (Hash{Symbol => Object})

    additional options

Returns:

  • (Object)


96
97
98
99
# File 'lib/dydra/client.rb', line 96

def self.post(where, what, options = {})
  what = what.to_json unless options[:content_type]
  resource(where).post(what, {:content_type => 'application/json', :user_agent => "Dydra API client #{Dydra::VERSION}"}.merge(options))
end

+ reset!

This method returns an undefined value.

Clears the current user state.



153
154
155
# File 'lib/dydra/client.rb', line 153

def self.reset!
  $dydra = {}
end

+ (RestClient::Resource) resource(location)

Provides a RestClient::Resource configured with authentication information for the given URL fragment.

Parameters:

  • location (String)

Returns:

  • (RestClient::Resource)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dydra/client.rb', line 107

def self.resource(location)
  $dydra ||= {}
  opts = {}
  opts[:timeout] = ENV['DYDRA_REQUEST_TIMEOUT'].to_i if ENV['DYDRA_REQUEST_TIMEOUT']
  if $dydra[:token]
    RestClient::Resource.new(Dydra::URL.join(location).to_s, opts.merge(:user => $dydra[:token]))
  elsif $dydra[:user]
    RestClient::Resource.new(Dydra::URL.join(location).to_s, opts.merge(:user => $dydra[:user], :password => $dydra[:pass]))
  else
    raise AuthenticationError, "You need to run Dydra.setup! before performing an API operation"
  end
end

+ (XMLRPC::Client) rpc

Returns a Dydra.com RPC API client.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dydra/client.rb', line 16

def self.rpc
  url = Dydra::URL.host.to_s.sub(/\/$/, '')
  port = Dydra::URL.port || 80
  $dydra ||= {}
  if $dydra[:token]
    XMLRPC::Client.new3('host' => url, 'port' => port, 'path' => "/rpc?auth_token=#{$dydra[:token]}")
  elsif $dydra[:user]
    XMLRPC::Client.new3('host' => url, 'port' => port, 'path' => '/rpc', 'user' => $dydra[:user], 'password' => $dydra[:pass])
  else
    raise AuthenticationError, "You need to run Dydra.setup! before performing an API operation"
  end
end

+ (Boolean) setup?

Returns true if #setup! has been run.

Returns:

  • (Boolean)

    true or false



144
145
146
147
# File 'lib/dydra/client.rb', line 144

def self.setup?
  $dydra ||= {}
  $dydra[:setup?]
end