/
embedly_oembed
executable file
·85 lines (66 loc) · 1.98 KB
/
embedly_oembed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
$:.unshift(File.expand_path('../../lib', __FILE__))
%w{embedly json optparse ostruct}.each {|l| require l}
options = OpenStruct.new({
:hostname => nil,
:key => ENV['EMBEDLY_KEY'] == '' ? nil : ENV['EMBEDLY_KEY'],
:verbose => false,
:args => {},
:headers => {}
})
action = File.basename(__FILE__)[/embedly_(\w+)/, 1]
opts = OptionParser.new do |opts|
opts.banner = <<-DOC
Fetch JSON from the embedly #{action} service.
Usage #{action} [OPTIONS] <url> [url] ..
DOC
opts.separator ""
opts.separator "Options:"
opts.on("-H", "--hostname ENDPOINT",
"Embedly host. Default is api.embed.ly.") do |e|
options.hostname = e
end
opts.on("--header NAME=VALUE",
"HTTP header to send with requests.") do |header|
n,v = header.split '='
options.headers[n] = v
end
opts.on("-k", "--key KEY", "Embedly key [default: " +
"EMBEDLY_KEY environmental variable]") do |k|
options.key = k
end
opts.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do
options.key = nil
end
opts.on("-o", "--option NAME=VALUE", "Set option to be passed as " +
"query param.") do |o|
k,v = o.split('=')
options.args[k] = v
end
opts.separator ""
opts.separator "Common Options:"
opts.on("-v", "--verbose", "Run verbosely") do
options.verbose = true
end
opts.on("-h", "--help", "Display this message") do
puts opts
exit
end
opts.separator ""
opts.separator "Bob Corsaro <bob@embed.ly>"
end
opts.parse!
if ARGV.size < 1
$stderr.puts "ERROR: url required"
$stderr.puts opts
exit 1
end
Embedly::Config.logging = true if options.verbose
options.args[:urls] = ARGV
api = Embedly::API.new options.marshal_dump
begin
objs = [api.send(action.to_sym, options.args)].flatten.collect{|o| o.marshal_dump}
puts JSON.pretty_generate(objs)
rescue Embedly::BadResponseException => e
puts "#{e.response.code} :: #{e.response.message}"
end