Module: OmnyStudioClient

Defined in:
lib/omnystudio_client.rb,
lib/omnystudio_client/clip.rb,
lib/omnystudio_client/program.rb,
lib/omnystudio_client/clip_collection.rb,
lib/omnystudio_client/program_collection.rb

Overview

Author:

  • Jay Arella

Defined Under Namespace

Classes: Clip, ClipCollection, ConnectionError, Program, ProgramCollection

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_base_urlObject

Returns the value of attribute api_base_url


12
13
14
# File 'lib/omnystudio_client.rb', line 12

def api_base_url
  @api_base_url
end

.tokenObject

Returns the value of attribute token


12
13
14
# File 'lib/omnystudio_client.rb', line 12

def token
  @token
end

Class Method Details

.clipsObject

Returns a new instance of OmnyStudioClient::ClipCollection.

Examples:

Make a new Clips instance

omnystudio.clips #=> new OmnyStudioClient::ClipCollection

79
80
81
# File 'lib/omnystudio_client.rb', line 79

def clips
  self::ClipCollection.new
end

.connection(options = {}) ⇒ Object

Note:

This is a generalized REST method that is used by both the Clip and Program class. If it's successful, it returns a struct representing the data. If it fails, it raises a ConnectionError.

Examples:

Get a list of programs with #connection

omnystudio.connection({
  method: :get,
  url: "https://cms.omnystudio.fm/api/programs"
})#=> Array of structs representing programs

Options Hash (options):

  • :method (Symbol)

    Request method

  • :params (Hash)

    Request params in a hash

  • :payload (String)

    If the request method is `POST`, the body of the post

  • :url (String)

    Request url


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omnystudio_client.rb', line 43

def connection options={}
  request_headers = default_headers.merge({ params: options[:params] })

  begin
    response = RestClient::Request.execute(
      url: options[:url],
      method: options[:method],
      headers: request_headers,
      payload: options[:body].to_json
    )
  rescue RestClient::ExceptionWithResponse => err
    raise ConnectionError.new("OmnyStudio ConnectionError: #{err.response.description}, Request: #{err.response.request.method} #{err.response.request.url}")
  end

  JSON.parse(response.body, object_class: OpenStruct)
end

.default_headersObject

Returns a hash with the default request headers, includes the token.

Examples:

Calling #default_headers after initializing with a token

omnystudio = OmnyStudioClient.new({
  token: '910'
})
omnystudio.default_headers #=> { content_type: "application/json", authorization: "Token token=910", params: {} }

67
68
69
70
71
72
73
# File 'lib/omnystudio_client.rb', line 67

def default_headers
  {
    content_type: "application/json",
    authorization: "OmnyToken #{@token}",
    params: {}
  }
end

.new(options = {}) ⇒ Object

Returns a new instance of OmnyStudioClient.

Examples:

Initialize OmnyStudioClient into a variable

omnystudio = OmnyStudioClient.new({
  token: '910'
})
omnystudio #=> new OmnyStudioClient

Options Hash (options):


23
24
25
26
27
28
# File 'lib/omnystudio_client.rb', line 23

def new options={}
  @api_base_url = options[:api_base_url] || "https://api.omnystudio.com/v0"
  @token = options[:token]

  self
end

.program(id = nil) ⇒ Object

Returns a new instance of OmnyStudioClient::Program.

Examples:

Make a new Program instance

omnystudio.program("12345") #=> new OmnyStudioClient::Program with id 12345

87
88
89
# File 'lib/omnystudio_client.rb', line 87

def program(id=nil)
  self::Program.new(id)
end

.programsObject

Returns a new instance of OmnyStudioCilent::ProgramCollection.

Examples:

Make a new Programs instance

omnystudio.programs #=> new OmnyStudioClient::ProgramCollection

95
96
97
# File 'lib/omnystudio_client.rb', line 95

def programs
  self::ProgramCollection.new
end