Class: Imgur::Client::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur/client.rb,
lib/imgur/requests/get_image.rb,
lib/imgur/requests/get_albums.rb,
lib/imgur/requests/get_images.rb,
lib/imgur/requests/delete_image.rb,
lib/imgur/requests/get_accounts.rb,
lib/imgur/requests/upload_image.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



224
225
226
# File 'lib/imgur/client.rb', line 224

def initialize(options={})
  @url = options[:url] || "https://api.imgur.com"
end

Class Method Details

.dataObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
203
204
205
206
# File 'lib/imgur/client.rb', line 138

def self.data
  @data ||= begin
              image_id     = random_id
                 = random_number
               = random_id
              album_id     = random_id
              comment_id   = random_id

               = {
                "id"         => ,
                "url"        => "#{}.imgur.com",
                "bio"        => nil,
                "reputation" => 435.5,
                "created"    => 1349051610,
              }

              image = {
                "id"         => image_id,
                "title"      => "test image",
                "datetime"   => 1349051625,
                "type"       => "image/jpeg",
                "animated"   => false,
                "width"      => 2490,
                "height"     => 3025,
                "size"       => 618969,
                "views"      => 625622,
                "bandwidth"  => 387240623718,
                "ups"        => 1889,
                "downs"      => 58,
                "score"      => 18935622,
                "account_id" => ,
              }

              album = {
                "id"          => album_id,
                "title"       => "test album",
                "description" => nil,
                "privacy"     => "public",
                "cover"       => image_id,
                "order"       => 0,
                "layout"      => "blog",
                "datetime"    => 1347058832,
                "link"        => "https://imgur.com/a/#{album_id}",
                "account_id"  => ,
              }

              comment = {
                "id"        => comment_id,
                "image_id"  => image_id,
                "author"    => ,
                "author_id" => ,
                "on_album"  => false,
                "ups"       => 10,
                "downs"     => 2,
                "points"    => 23.4,
                "datetime"  => 1349051670,
                "parent_id" => 2,
                "deleted"   => false,
                "children"  => [],
              }

              {
                :images   => {image_id   => image},
                :accounts => { => },
                :albums   => {album_id   => album},
                :comments => {comment_id => comment},
              }
            end
end

.random_idObject



118
119
120
121
122
123
124
125
126
# File 'lib/imgur/client.rb', line 118

def self.random_id
  characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  selection  = ''
  7.times do
    position = rand(characters.length)
    selection << characters[position..position]
  end
  selection
end

.random_numberObject



128
129
130
131
132
133
134
135
136
# File 'lib/imgur/client.rb', line 128

def self.random_number
  characters = '1234567890'
  selection  = ''
  7.times do
    position = rand(characters.length)
    selection << characters[position..position]
  end
  selection.to_i
end

.reset!Object



208
209
210
# File 'lib/imgur/client.rb', line 208

def self.reset!
  @data = nil
end

Instance Method Details

#creditsObject



220
221
222
# File 'lib/imgur/client.rb', line 220

def credits
  "980/1000"
end

#dataObject



212
213
214
# File 'lib/imgur/client.rb', line 212

def data
  self.class.data
end

#delete_image(deletehash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/imgur/requests/delete_image.rb', line 14

def delete_image(deletehash)
  image = self.data[:images].values.detect { |i| i["deletehash"] == deletehash }

  self.data[:images].delete_if do |id, image|
    image["deletehash"] == deletehash
  end

  basic_response = {
    "data"    => true,
    "status"  => 200,
    "success" => true,
  }

  response(body: basic_response)
end

#get_accounts(params = {}) ⇒ Object



13
14
15
16
# File 'lib/imgur/requests/get_accounts.rb', line 13

def get_accounts(params={})
  accounts = self.data[:accounts].values.first
  response(body: {"data" => accounts})
end

#get_albums(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/imgur/requests/get_albums.rb', line 13

def get_albums(params={})
  albums = self.data[:albums].values
  albums = if params[:path] =~ /^\/account/
             {"data" => albums.select { |a| !a["account_id"].nil? } }
           else
             {"data" => albums}
           end

  response(body: albums)
end

#get_image(id) ⇒ Object



14
15
16
17
18
# File 'lib/imgur/requests/get_image.rb', line 14

def get_image(id)
  image = self.data[:images][id["id"]]

  response(:body => {"data" => image})
end

#get_images(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/imgur/requests/get_images.rb', line 13

def get_images(params={})
  images = self.data[:images].values
  images = if params[:path] =~ /^\/account/
             {"data" => images.select { |i| !i["account_id"].nil? } }
           else
             {"data" => images}
           end

  response(body: images)
end

#random_idObject



216
217
218
# File 'lib/imgur/client.rb', line 216

def random_id
  self.class.random_id
end

#response(options = {}) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/imgur/client.rb', line 228

def response(options={})
  status  = options[:status] || 200
  body    = options[:body]
  headers = {
    "Content-Type" => "application/json; charset=utf-8"
  }.merge(options[:headers] || {})

  Imgur::Response.new(status, headers, body).raise!
end

#upload_image(options = {}) ⇒ Object



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
# File 'lib/imgur/requests/upload_image.rb', line 15

def upload_image(options={})
  images     = self.data[:images]
  image_id   = self.random_id
   = self.data[:accounts].values.first["id"]

  image = {
    "id"         => image_id,
    "title"      => options[:title],
    "datetime"   => 1349051625,
    "type"       => "image/jpeg",
    "animated"   => false,
    "width"      => 2490,
    "height"     => 3025,
    "size"       => 618969,
    "views"      => 625622,
    "bandwidth"  => 387240623718,
    "ups"        => 1889,
    "downs"      => 58,
    "score"      => 18935622,
    "deletehash" => self.random_id,
  }

  images[image_id] = image

  response(:body => {"data" => image})
end