Class: Encosion::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/encosion/base.rb

Overview

The base for all Encosion objects

Direct Known Subclasses

Image, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#read_tokenObject

Returns the value of attribute read_token.



32
33
34
# File 'lib/encosion/base.rb', line 32

def read_token
  @read_token
end

#write_tokenObject

Returns the value of attribute write_token.



32
33
34
# File 'lib/encosion/base.rb', line 32

def write_token
  @write_token
end

Class Method Details

.all(*args) ⇒ Object

This is an alias for find(:all)



50
51
52
# File 'lib/encosion/base.rb', line 50

def all(*args)
  find(:all, *args)
end

.error_check(header, body) ⇒ Object

Checks the HTTP response and handles any errors



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/encosion/base.rb', line 98

def error_check(header,body)
  if header.status_code == 200
    return true if body.nil?
#          puts body['error']
    if body.has_key? 'error' && !body['error'].nil?
      message = "Brightcove responded with an error: #{body['error']} (code #{body['code']})"
      body['errors'].each do |error| 
        message += "\n#{error.values.first} (code #{error.values.last})"
      end if body.has_key? 'errors'
      raise BrightcoveException, message
    end
  else
    # should only happen if the Brightcove API is unavailable (even BC errors return a 200)
    raise BrightcoveException, body + " (status code: #{header.status_code})"
  end
end

.find(*args) ⇒ Object

Does a GET to search photos and other good stuff



40
41
42
43
44
45
46
# File 'lib/encosion/base.rb', line 40

def find(*args)
  options = extract_options(args)
  case args.first
  when :all   then find_all(options)
  else        find_from_ids(args,options)
  end
end

.get(server, port, secure, path, command, options) ⇒ Object

Performs an HTTP GET



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/encosion/base.rb', line 56

def get(server,port,secure,path,command,options)
  http = HTTPClient.new
  url = secure ? 'https://' : 'http://'
  url += "#{server}:#{port}#{path}"
  
  options.merge!({'command' => command })
  query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
  
  response = http.get(url, query_string)
  
  body = response.body.content.strip == 'null' ? nil : JSON.parse(response.body.content.strip)   # if the call returns 'null' then there were no valid results
  header = response.header
  
  error_check(header,body)
  
  # puts "url: #{url}\nquery_string:#{query_string}"

  return body
end

.post(server, port, secure, path, command, options, instance) ⇒ Object

Performs an HTTP POST



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/encosion/base.rb', line 78

def post(server,port,secure,path,command,options,instance)
  http = HTTPClient.new
  url = secure ? 'https://' : 'http://'
  url += "#{server}:#{port}#{path}"
  
  content = { 'json' => { 'method' => command, 'params' => options }.to_json }    # package up the variables as a JSON-RPC string
  content.merge!({ 'file' => instance.file }) if instance.respond_to?('file')             # and add a file if there is one

  response = http.post(url, content)
  # get the header and body for error checking
  body = JSON.parse(response.body.content.strip)
  header = response.header

  error_check(header,body)
  # if we get here then no exceptions were raised
  return body
end