Class: Encosion::Base
- Inherits:
-
Object
- Object
- Encosion::Base
- Defined in:
- lib/encosion/base.rb
Overview
The base for all Encosion objects
Instance Attribute Summary collapse
-
#read_token ⇒ Object
Returns the value of attribute read_token.
-
#write_token ⇒ Object
Returns the value of attribute write_token.
Class Method Summary collapse
-
.all(*args) ⇒ Object
This is an alias for find(:all).
-
.error_check(header, body) ⇒ Object
Checks the HTTP response and handles any errors.
-
.find(*args) ⇒ Object
Does a GET to search photos and other good stuff.
-
.get(server, port, secure, path, command, options) ⇒ Object
Performs an HTTP GET.
-
.post(server, port, secure, path, command, options, instance) ⇒ Object
Performs an HTTP POST.
Instance Attribute Details
#read_token ⇒ Object
Returns the value of attribute read_token.
32 33 34 |
# File 'lib/encosion/base.rb', line 32 def read_token @read_token end |
#write_token ⇒ Object
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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/encosion/base.rb', line 96 def error_check(header,body) if header.status_code == 200 return true if body.nil? error = body['error'] unless error.nil? = "#{body['error']["name"]}: #{body['error']["message"]} (code #{body['error']['code']})" body['errors'].each do |error| += "\n#{error.values.first} (code #{error.values.last})" end if body.has_key? 'errors' raise BrightcoveException, 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) = (args) case args.first when :all then find_all() else find_from_ids(args,) 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 |
# File 'lib/encosion/base.rb', line 56 def get(server,port,secure,path,command,) http = HTTPClient.new url = secure ? 'https://' : 'http://' url += "#{server}:#{port}#{path}" .merge!({'command' => command }) query_string = .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) return body end |
.post(server, port, secure, path, command, options, instance) ⇒ Object
Performs an HTTP POST
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/encosion/base.rb', line 76 def post(server,port,secure,path,command,,instance) http = HTTPClient.new url = secure ? 'https://' : 'http://' url += "#{server}:#{port}#{path}" content = { 'json' => { 'method' => command, 'params' => }.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 |