Class: Viddlereo::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/viddlereo/resource.rb

Direct Known Subclasses

Echo, Playlist, Session, Upload, Video

Constant Summary collapse

BASE_URI =
"http://api.viddler.com/api/v2/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResource

Returns a new instance of Resource.



5
6
7
8
9
10
11
# File 'lib/viddlereo/resource.rb', line 5

def initialize
  self.class.params.each do |p|
    if p.data_type == Array
      self.send("#{p.name}=", [])
    end
  end
end

Class Method Details

.auth_requiredObject



30
31
32
# File 'lib/viddlereo/resource.rb', line 30

def self.auth_required
  @auth_required = true
end

.auth_required?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/viddlereo/resource.rb', line 34

def self.auth_required?
  @auth_required
end

.clientObject



26
27
28
# File 'lib/viddlereo/resource.rb', line 26

def self.client
  RestClient
end

.default_optionsObject



17
18
19
20
21
22
23
24
# File 'lib/viddlereo/resource.rb', line 17

def self.default_options
  defaults = {
    :key => Viddlereo.key
  }

  defaults[:sessionid] = Viddlereo.session_id if Viddlereo.has_session?
  defaults
end

.get(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/viddlereo/resource.rb', line 47

def self.get(options = {})
  prepare_request
  method_name = options.delete(:method)
  begin
    client.get uri(method_name), :params => options.merge(default_options)
  rescue RestClient::BadRequest => e
    raise Viddlereo::Error.from_rest_client_exception(e)
  end
end

.inherited(child) ⇒ Object



13
14
15
# File 'lib/viddlereo/resource.rb', line 13

def self.inherited(child)
  child.send(:include, HappyMapper)
end

.param(name, data_type) ⇒ Object



67
68
69
70
71
# File 'lib/viddlereo/resource.rb', line 67

def self.param(name, data_type)
  @params ||= []
  @params << Viddlereo::Param.new(name, data_type)
  attr_accessor name
end

.paramsObject



73
74
75
# File 'lib/viddlereo/resource.rb', line 73

def self.params
  @params || []
end

.post(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/viddlereo/resource.rb', line 57

def self.post(options = {})
  prepare_request
  method_name = options.delete(:method)
  begin
    response = client.post uri(method_name), options.merge(default_options)
  rescue RestClient::BadRequest => e
    raise Viddlereo::Error.from_rest_client_exception(e)
  end
end

.resource_name(name) ⇒ Object



38
39
40
# File 'lib/viddlereo/resource.rb', line 38

def self.resource_name(name)
  @resource_name = name
end

.uri(method_name = nil) ⇒ Object



42
43
44
45
# File 'lib/viddlereo/resource.rb', line 42

def self.uri(method_name = nil)
  BASE_URI + "viddler." + (@resource_name || name.downcase.split("::").last) +
    (!method_name.nil? ? ".#{method_name}" : "") + ".xml"
end

Instance Method Details

#paramsObject



77
78
79
80
81
82
83
84
# File 'lib/viddlereo/resource.rb', line 77

def params
  @params = {}
  self.class.params.each do |p|
    @params[p.name] = self.send(p.name)
  end

  @params
end