Class: Active::Asset

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/active/asset.rb

Direct Known Subclasses

Activity, Article, Result, Training

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.facetObject

We have several different types of data in the Search index. To restrict a search to a particular type, use the facet parameter. The available values are:

activities - things like running events or camps
results - race results from results.active.com
training - training plans
articles - articles on active.com and ihoops.com

This method should be overridden in child classes to return the appropriate type string.



199
200
201
# File 'lib/active/asset.rb', line 199

def facet
  ''
end

.factory(data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/active/asset.rb', line 100

def factory(data)
  begin
  category = data['meta']['category']
  rescue NoMethodError
    category = nil
  end
  
  type = case category
  when 'Activities'
    Active::Activity
  when 'Articles'
    Active::Article
  when 'Training plans'
    Active::Training
  else
    Active::Asset
  end
  type.new(data)
end

.find(asset_ids = nil) ⇒ Object

this code smells



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active/asset.rb', line 121

def find(asset_ids=nil)
  raise Active::InvalidOption, "Couldn't find Asset without an ID" if asset_ids.nil?
  query    = Active::Query.new
  ids       = asset_ids.kind_of?(Array) ? asset_ids : [asset_ids]
  query.options[:meta][:assetId] = ids.collect{ |id| id.gsub("-","%2d") }

  # Executes the actual search API call
  res = query.search

  # Ensure we have found all of the IDs requested, otherwise raise an error
  # that includes which ID(s) are missing.
  if res['numberOfResults'] != ids.length
    missing_ids = Array.new(ids)
    res['_results'].each do |r|
      found_id = r['meta']['assetId'] & missing_ids
      missing_ids -= found_id
    end
    raise Active::RecordNotFound, "Couldn't find record with asset_id: #{missing_ids.join(',')}"
  end

  a = []
  res['_results'].collect do |d|
    t      = self.new(d)
    a << t
  end

  if a.length == 1
    return a.first
  else
    return a
  end
end

.find_by_url(url) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/active/asset.rb', line 156

def find_by_url(url)
  raise Active::InvalidOption, "Couldn't find Asset without a url" if url.nil?
  query                = Active::Query.new
  query.options[:m] << "site:#{url}"
  
  # Executes the actual search API call
  res = query.search
  if res['numberOfResults'].to_i < 1
    raise Active::RecordNotFound, "Couldn't find record with asset_id: #{url}"
  end

  a = []
  res['_results'].collect do |d|
    t      = self.new(d)
    a << t
  end

  if a.length == 1
    return a.first
  else
    return a
  end
end

Instance Method Details

#addressObject



85
86
87
88
89
90
91
92
93
# File 'lib/active/asset.rb', line 85

def address
  if self.meta['address']
    return self.meta['address']
  elsif self.meta['location']
    return self.meta['location']
  else
    return "#{self.meta.city}, #{self.meta.state}"
  end
end

#asset_idObject



77
78
79
80
81
82
83
# File 'lib/active/asset.rb', line 77

def asset_id
  aid = self.meta!.assetId
  if aid.kind_of?(Array)
    aid = aid.first
  end
  aid.upcase
end

#descriptionObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/active/asset.rb', line 54

def description
  return @description if @description
  if self.meta!.summary?
    # Notice we have to use self['hash'] to get the original value so we don't stackoverflow
    @description = ActiveSupport::Multibyte::Unicode.tidy_bytes( self.meta.summary )
    @description = @description.gsub(/<\/?[^>]*>/, "")
    @description = ::HTMLEntities.new.decode( @description )
  end
  @description
end

#start_dateObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active/asset.rb', line 65

def start_date
  if self.meta!.startDate?
    if self.meta!.startTime?
      Time.parse("#{self.meta.startDate} #{self.meta.startTime}")
    else
      Date.parse(self.meta.startDate)
    end
  else
    nil
  end
end

#titleObject

  • No punctuation: Returns the value of the hash for that key, or nil if none exists.

  • Assignment (=): Sets the attribute of the given method name.

  • Existence (?): Returns true or false depending on whether that key has been set.

  • Bang (!): Forces the existence of this key, used for deep Mashes. Think of it as “touch” for mashes.

Basic Example

mash = Mash.new
mash.name? # => false
mash.name = "Bob"
mash.name # => "Bob"
mash.name? # => true

Hash Conversion Example

hash = {:a => {:b => 23, :d => {:e => "abc"}}, :f => [{:g => 44, :h => 29}, 12]}
mash = Mash.new(hash)
mash.a.b # => 23
mash.a.d.e # => "abc"
mash.f.first.g # => 44
mash.f.last # => 12

Bang Example

mash = Mash.new
mash.author # => nil
mash.author! # => <Mash>

mash = Mash.new
mash.author!.name = "Michael Bleigh"
mash.author # => <Mash name="Michael Bleigh">


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active/asset.rb', line 41

def title
  return @title if @title
  if self.title?
    # Notice we have to use self['hash'] to get the original value so we don't stackoverflow
    @title = ActiveSupport::Multibyte::Unicode.tidy_bytes(self['title'])
    @title = @title.split("|")[0].strip if @title.include?("|")
    @title = @title.gsub(/<\/?[^>]*>/, "")
    @title = @title.gsub("...", "")
    @title = ::HTMLEntities.new.decode( @title )
  end
  @title
end

#zipObject



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

def zip
  self.meta['zip'] # an inheritated object defines zip so we need to override it.
end