Module: Woody::Decorators::Insights::FieldTransformer
- Defined in:
- lib/woody/decorators/insights/field_transformer.rb
Class Method Summary collapse
- .apply(transformers, value) ⇒ Object
- .commas ⇒ Object
- .date ⇒ Object
- .datetime ⇒ Object
- .fb_url ⇒ Object
- .percentage ⇒ Object
- .post_url ⇒ Object
- .round(p) ⇒ Object
- .shorten ⇒ Object
- .thumbnail ⇒ Object
- .truncate(length) ⇒ Object
Class Method Details
.apply(transformers, value) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 9 def self.apply(transformers, value) transformers.reduce(value) do |accum, transformer| if transformer.key?('args') self.send(transformer['method'], transformer['args']).(accum) else self.send(transformer['method']).(accum) end end end |
.commas ⇒ Object
19 20 21 22 23 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 19 def self.commas() lambda do |val| val.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end end |
.date ⇒ Object
25 26 27 28 29 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 25 def self.date() lambda do |val| DateTime.parse(val).strftime("%-d %B %Y") end end |
.datetime ⇒ Object
31 32 33 34 35 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 31 def self.datetime() lambda do |val| DateTime.parse(val).strftime("%-d %B %Y at %-l:%M%P") end end |
.fb_url ⇒ Object
37 38 39 40 41 42 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 37 def self.fb_url() lambda do |val| parts = val.split('_') format('https://www.facebook.com/%s/videos/%s/', parts[0], parts[1]) end end |
.percentage ⇒ Object
44 45 46 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 44 def self.percentage() lambda { |val| format('%s%', val.to_s) } end |
.post_url ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 48 def self.post_url() lambda do |val| config = Wes::Cloudkit.config video = Woody::Decorators::Video.new( Wes::Data::API::Video.find('facebook_id', val), config ) brief = Wes::Data::API::Brief.find(:id, video.submission.brief_id) post = brief.insights('posts').find do |p| p['object_id'] == val end format( '/brief/%s/insights/post/%s', brief.id, post['post_id'] ) end end |
.round(p) ⇒ Object
68 69 70 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 68 def self.round(p) lambda { |val| val.round(p) } end |
.shorten ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 72 def self.shorten() lambda do |val| return val.to_s if val < 1000 return format('%sk', (val / 1000)) if val < 1000000 return format('%sm', (val / 1000000)) end end |
.thumbnail ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 80 def self.thumbnail() lambda do |val| config = Wes::Cloudkit.config Woody::Decorators::Video.new( Wes::Data::API::Video.find('facebook_id', val), config ).thumbnail end end |
.truncate(length) ⇒ Object
89 90 91 |
# File 'lib/woody/decorators/insights/field_transformer.rb', line 89 def self.truncate(length) lambda { |val| format('%s...', val[0..(length + 1)]) } end |