Class: Helix::Base

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

Direct Known Subclasses

Library, Media, Playlist, Tag, User

Constant Summary collapse

METHODS_DELEGATED_TO_CLASS =
[ :guid_name, :resource_label_sym, :plural_resource_label ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Base

Returns a new instance of Base.


85
86
87
88
# File 'lib/helix/base.rb', line 85

def initialize(opts)
  @attributes = opts[:attributes]
  @config     = opts[:config]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym) ⇒ String

Raises an error for missing method calls.

Parameters:

  • method_sym (Symbol)

    The method attempting to be called.

Returns:

  • (String)

    An error for the method attempting to be called.


158
159
160
161
162
163
164
# File 'lib/helix/base.rb', line 158

def method_missing(method_sym)
  begin
    @attributes[method_sym.to_s]
  rescue
    raise NoMethodError, "#{method_sym} is not recognized within #{self.class.to_s}'s methods or @attributes"
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes


15
16
17
# File 'lib/helix/base.rb', line 15

def attributes
  @attributes
end

#configObject

Returns the singleton instance of the Helix::Config.


96
97
98
# File 'lib/helix/base.rb', line 96

def config
  @config ||= Helix::Config.instance
end

Class Method Details

.allArray

Note:

this method takes no query options, unlike find_all

Fetches all accessible records, places them into instances, and returns them as an array.

Examples:

Helix::Video.find_all(query: 'string_to_match') #=> [video1,video2]

Parameters:

  • opts (Hash)

    a hash of options for parameters passed into the HTTP GET

Returns:

  • (Array)

    The array of instance objects for a class.


43
44
45
# File 'lib/helix/base.rb', line 43

def self.all
  find_all
end

.configObject

Returns the singleton instance of the Helix::Config.


91
92
93
# File 'lib/helix/base.rb', line 91

def self.config
  Helix::Config.instance
end

.find_all(original_opts = {}) ⇒ Array

Fetches all accessible records, places them into instances, and returns them as an array.

Examples:

Helix::Video.find_all(query: 'string_to_match') #=> [video1,video2]

Parameters:

  • opts (Hash)

    a hash of options for parameters passed into the HTTP GET

Returns:

  • (Array)

    The array of instance objects for a class.

Raises:


26
27
28
29
30
31
32
33
34
# File 'lib/helix/base.rb', line 26

def self.find_all(original_opts={})
  raise Helix::NoConfigurationLoaded.new if config.nil?
  opts           = original_opts.clone
  RestClient.log = 'helix.log' if opts.delete(:log)
  data_sets = get_data_sets(opts)
  return [] if data_sets.nil?
  data_sets.map { |attrs| self.new( attributes: massage_attrs(attrs),
                                    config:     config) }
end

.get_data_sets(opts) ⇒ Array

Does a GET call to the api and defaults to content_type xml and signature_type view.

Parameters:

  • opts (Hash)

    a hash of options for parameters passed into the HTTP GET

Returns:

  • (Array)

    The array of attributes (for a model) in hash form.


53
54
55
56
57
58
59
# File 'lib/helix/base.rb', line 53

def self.get_data_sets(opts)
  label        = self.plural_resource_label
  url          = config.build_url(content_type:   opts[:content_type] || :xml,
                                  resource_label: label)
  # We allow opts[:sig_type] for internal negative testing only.
  data_sets    = config.get_aggregated_data_sets(url, label, {sig_type: :view}.merge(opts))
end

.guid_nameString

Creates a string that associates to the class id.

Examples:

Helix::Video.guid_name #=> "video_id"

Returns:

  • (String)

    The guid name for a specific class.


67
68
69
# File 'lib/helix/base.rb', line 67

def self.guid_name
  "#{resource_label_sym}_id"
end

.plural_resource_labelString

Creates a string associated with a class name pluralized

Examples:

Helix::Video.plural_resource_label #=> "videos"

Returns:

  • (String)

    The class name pluralized


77
78
79
# File 'lib/helix/base.rb', line 77

def self.plural_resource_label
  "#{resource_label_sym}s"
end

.where(opts = {}) ⇒ Array

Fetches all accessible records, places them into instances, and returns them as an array.

Examples:

Helix::Video.find_all(query: 'string_to_match') #=> [video1,video2]

Parameters:

  • opts (Hash) (defaults to: {})

    a hash of options for parameters passed into the HTTP GET

Returns:

  • (Array)

    The array of instance objects for a class.


37
38
39
# File 'lib/helix/base.rb', line 37

def self.where(opts={})
  find_all(opts)
end

Instance Method Details

#custom_field(k) ⇒ String

Looks up the custom field value for a given name.

Examples:

video.custom_field('stars_kevin_bacon') #=> "true"

Returns:

  • (String)

    The value whose key is the given custom field name.


106
107
108
# File 'lib/helix/base.rb', line 106

def custom_field(k)
  custom_fields[k]
end

#custom_fieldsHash

Returns all custom fields.

Examples:

video.custom_fields #=> { "stars_kevin_bacon" => "true" }

Returns:

  • (Hash)

    The custom fields in the form { name1 => value1, name2 => value2, ... }


116
117
118
# File 'lib/helix/base.rb', line 116

def custom_fields
  modified_attributes['custom_fields']
end

#guidString

Creates a string that associates to the class id.

Examples:

video = Helix::Video.create({title: "My new title"})
video.guid #=> "9e0989v234sf4"

Returns:

  • (String)

    The guid for the class instance.


127
128
129
# File 'lib/helix/base.rb', line 127

def guid
  @attributes[guid_name]
end

#load(opts = {}) ⇒ Base Also known as: reload

Loads in the record from a HTTP GET response.

Parameters:

  • opts (Hash) (defaults to: {})

    a hash of attributes to update the instance with.

Returns:

  • (Base)

    Returns an instance of the class.


135
136
137
138
139
140
141
142
143
# File 'lib/helix/base.rb', line 135

def load(opts={})
  memo_cfg      = config
  base_url_opts = {content_type: (opts[:content_type] || :json)}
  url           = memo_cfg.build_url(base_url_opts.merge(guid: self.guid, resource_label: plural_resource_label))
  # We allow opts[:sig_type] for internal negative testing only.
  raw_attrs     = memo_cfg.get_response(url, {sig_type: :view}.merge(opts))
  @attributes   = massage_raw_attrs(raw_attrs)
  self
end

#raw_responseString

Raw response should return the response from the config.

Returns:

  • (String)

    A response from the server in form of JSON or XML


169
170
171
# File 'lib/helix/base.rb', line 169

def raw_response
  self.config.response
end

#to_jsonObject


146
147
148
# File 'lib/helix/base.rb', line 146

def to_json
  {resource_label_sym => @attributes}.to_json
end

#to_xmlObject


150
151
152
# File 'lib/helix/base.rb', line 150

def to_xml
  modified_attributes.to_xml({root: resource_label_sym})
end