Class: Ayadn::Annotations

Inherits:
Object show all
Defined in:
lib/ayadn/annotations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dic) ⇒ Annotations

Creates basic annotations and optionally adds necessary ones



13
14
15
16
17
18
19
20
# File 'lib/ayadn/annotations.rb', line 13

def initialize(dic)
  dic[:options] = {} if dic[:options].nil?
  @content = base()
  @content += files(dic) if dic[:options][:embed]
  @content += youtube(dic) if dic[:options][:youtube]
  @content += vimeo(dic) if dic[:options][:vimeo]
  @content += nowplaying(dic) if dic[:options][:nowplaying]
end

Instance Attribute Details

#contentObject

This class contains the “annotations” (metadata) templates for posting to ADN



10
11
12
# File 'lib/ayadn/annotations.rb', line 10

def content
  @content
end

Instance Method Details

#baseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ayadn/annotations.rb', line 22

def base
  # Creates basic post metadata
  # Both types are custom types for Ayadn (all types beginning with "com.ayadn" are custom types)
  [
    {
    "type" => "com.ayadn.user",
    "value" => {
      "+net.app.core.user" => {
          "user_id" => Settings.config.identity.handle,
          "format" => "basic"
        },
        "env" => {
          "platform" => Settings.config.platform,
          "ruby" => Settings.config.ruby,
          "locale" => Settings.config.locale
        }
      }
    },
    {
    "type" => "com.ayadn.client",
    "value" => {
      "url" => "https://github.com/ericdke/na",
      "author" => {
          "name" => "Eric Dejonckheere",
          "username" => "ericd",
          "id" => "69904",
          "email" => "[email protected]"
        },
      "version" => Settings.config.version
      }
    }
  ]
end

#files(dic) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ayadn/annotations.rb', line 56

def files(dic)
  # Creates "oembed" metadata for uploaded files
  files = FileOps.make_paths(dic[:options][:embed])
  data = FileOps.upload_files(files)
  data.map do |obj|
    {
      "type" => "net.app.core.oembed",
      "value" => {
         "+net.app.core.file" => {
            "file_id" => obj['data']['id'],
            "file_token" => obj['data']['file_token'],
            "format" => "oembed"
         }
      }
    }
  end
end

#nowplaying(dic) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ayadn/annotations.rb', line 173

def nowplaying(dic)
  return nowplaying_silent(dic) if dic[:options][:no_url] == true || dic[:visible] == false
  [{
    "type" => "com.ayadn.nowplaying",
      "value" => {
        "title" => dic[:title],
        "artist" => dic[:artist],
        "artwork" => dic[:artwork],
        "link" => dic[:link],
        "source" => dic[:source]
      }
  },
  {
    "type" => "net.app.core.oembed",
    "value" => {
      "version" => "1.0",
      "type" => "photo",
      "width" => dic[:width],
      "height" => dic[:height],
      "title" => dic[:title],
      "url" => dic[:artwork],
      "embeddable_url" => dic[:artwork],
      "provider_url" => "https://itunes.apple.com",
      "provider_name" => "iTunes",
      "thumbnail_url" => dic[:artwork_thumb],
      "thumbnail_width" => dic[:width_thumb],
      "thumbnail_height" => dic[:height_thumb]
    }
  }]
end

#nowplaying_silent(dic) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/ayadn/annotations.rb', line 163

def nowplaying_silent(dic)
  [{
    "type" => "com.ayadn.nowplaying",
      "value" => {
        "status" => "no-url",
        "source" => dic[:source]
      }
  }]
end

#vimeo(dic) ⇒ Object



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
153
154
155
156
157
158
159
160
161
# File 'lib/ayadn/annotations.rb', line 124

def vimeo(dic)
  dic[:link] = dic[:options][:vimeo][0]
  req_url = "http://vimeo.com/api/oembed.json?url=#{dic[:link]}"
  resp = CNX.download(req_url)
  begin
    decoded = JSON.parse(resp)
  rescue => e
    Status.new.info("error", resp, "red")
    Errors.global_error({error: e, caller: caller, data: [resp, dic]})
  end
  dic.merge!(decoded)
  [{
    "type" => "net.app.core.oembed",
    "value" => {
      "version" => "1.0",
      "type" => "video",
      "provider_name" => "Vimeo",
      "provider_url" => "http://vimeo.com/",
      "width" => dic['width'],
      "height" => dic['height'],
      "title" => dic['title'],
      "author_name" => dic['author_name'],
      "author_url" => dic['author_url'],
      "embeddable_url" => dic[:link],
      "html" => dic['html'],
      "thumbnail_url" => dic['thumbnail_url'],
      "thumbnail_height" => dic['thumbnail_height'],
      "thumbnail_width" => dic['thumbnail_width']
    }
  },
  {
    "type" => "com.ayadn.vimeo",
      "value" => {
        "title" => dic['title'],
        "link" => dic[:link]
      }
  }]
end

#youtube(dic) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ayadn/annotations.rb', line 74

def youtube(dic)
  # Fetch Youtube metadata for a video
  dic['link'] = dic[:options][:youtube][0]
  req_url = "http://www.youtube.com/oembed?url=#{dic['link']}&format=json"
  resp = CNX.download(req_url)

  begin
    decoded = JSON.parse(resp)
  rescue => e      
    if resp == "Not Found"       
      Status.new.info("error", "video doesn't exist", "red")
    elsif resp == "Unauthorized"
      Status.new.info("error", "unauthorized", "red")
    else
      Status.new.info("error", resp, "red")
    end
    Errors.global_error({error: e, caller: caller, data: [resp, dic]})
  end
  
  # Adds Youtube oembed metadata to the existing metadata
  # Also adds a custom type for Ayadn
  dic.merge!(decoded)
  [{
    "type" => "net.app.core.oembed",
    "value" => {
      "version" => "1.0",
      "type" => "video",
      "provider_name" => "YouTube",
      "provider_url" => "http://youtube.com/",
      "width" => dic['width'],
      "height" => dic['height'],
      "title" => dic['title'],
      "author_name" => dic['author_name'],
      "author_url" => dic['author_url'],
      "embeddable_url" => dic['link'],
      "html" => dic['html'],
      "thumbnail_url" => dic['thumbnail_url'],
      "thumbnail_height" => dic['thumbnail_height'],
      "thumbnail_width" => dic['thumbnail_width']
    }
  },
  {
    "type" => "com.ayadn.youtube",
      "value" => {
        "title" => dic['title'],
        "link" => dic['link']
      }
  }]
end