Class: Limgur

Inherits:
Object
  • Object
show all
Defined in:
lib/limgur.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Limgur

Returns a new instance of Limgur.



6
7
8
# File 'lib/limgur.rb', line 6

def initialize key
  @key = key
end

Class Method Details

.versionObject



121
122
123
# File 'lib/limgur.rb', line 121

def self.version
  '1.0.2'
end

Instance Method Details

#delete(hash) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/limgur.rb', line 100

def delete hash
  c = Curl::Easy.new "http://imgur.com/api/delete/#{hash}.json"
  c.http_get
  response = Crack::JSON.parse c.body_str

  if response['error']
    response['error']['error_msg']
  else
    'Image was successfully deleted!'
  end
end

#scrot(filename = nil) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/limgur.rb', line 112

def scrot filename=nil
  filename = Time.now.to_s.gsub(' ', '').gsub(':', '') + '.png' if filename.nil?

  if !(system 'scrot ' + filename)
    raise "scrot not installed"
  end
  upload File.expand_path filename
end

#upload(image, options = {}) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/limgur.rb', line 10

def upload image, options={}
  options = {:format => 'Sridn', :output => 'titles'}.merge options
  isURL = true
  begin
    if isURL && URI.parse(image).scheme
      c = Curl::Easy.new 'http://imgur.com/api/upload.json'
      c.http_post Curl::PostField.content('key', @key),
                  Curl::PostField.content('image', image)
      response = Crack::JSON.parse c.body_str
    else
      c = Curl::Easy.new 'http://imgur.com/api/upload.json'
      c.multipart_form_post = true
      c.http_post Curl::PostField.content('key', @key),
                  Curl::PostField.file('image', image)
      response = Crack::JSON.parse c.body_str
    end

    if response['rsp']['stat'] == 'fail'
      $stderr.puts response['rsp']['error_msg']
      "fail\n" if options[:format].count('S') > 0
    else
      output = ''
      
      options[:format].each_char do |char|
        case char
        when 'f'
          output << 'File:            ' if options[:output] == 'titles'
          output << image
        when 'I'
          output << 'Image hash:      ' if options[:output] == 'titles'
          output << response['rsp']['image']['image_hash']
        when 'D'
          output << 'Delete hash:     ' if options[:output] == 'titles'
          output << response['rsp']['image']['delete_hash']
        when 'i'
          output << 'Original image:  ' if options[:output] == 'titles'
          output << '[img]'             if options[:output] == 'bbcode'
          output << '<img src="'        if options[:output] == 'html'
          output << response['rsp']['image']['original_image']
          output << '" />'              if options[:output] == 'html'
          output << '[/img]'            if options[:output] == 'bbcode'
        when 'l'
          output << 'Large thumbnail: ' if options[:output] == 'titles'
          output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
          output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
          output << response['rsp']['image']['large_thumbnail']
          output << '" /></a>'          if options[:output] == 'html'
          output << '[/img][/url]'      if options[:output] == 'bbcode'
        when 's'
          output << 'Small thumbnail: ' if options[:output] == 'titles'
          output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
          output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
          output << response['rsp']['image']['small_thumbnail']
          output << '" /></a>'          if options[:output] == 'html'
          output << '[/img][/url]'      if options[:output] == 'bbcode'
        when 'r'
          output << 'Imgur page:      ' if options[:output] == 'titles'
          output << '[url='             if options[:output] == 'bbcode'
          output << '<a href="'         if options[:output] == 'html'
          output << response['rsp']['image']['imgur_page']
          output << '">Imgur</a>'       if options[:output] == 'html'
          output << ']Imgur[/url]'      if options[:output] == 'bbcode'
        when 'd'
          output << 'Delete page:     ' if options[:output] == 'titles'
          output << '[url='             if options[:output] == 'bbcode'
          output << '<a href="'         if options[:output] == 'html'
          output << response['rsp']['image']['delete_page']
          output << '">Delete</a>'       if options[:output] == 'html'
          output << ']Delete[/url]'      if options[:output] == 'bbcode'
        when 'S'
          output << 'Status:          ' if options[:output] == 'titles'
          output << response['rsp']['stat']
        when 'n'
        else
          $stderr.puts "#{char} not recognized as a format character!"
        end
        output << "\n"
      end
      
      return output
    end
  rescue URI::InvalidURIError
    isURL = false
    retry
  rescue Curl::Err::ReadError
    $stderr.puts 'Please provide a valid image.'
    "invalid image\n" if options[:format].count('S') > 0
  end
end