ruby-imgur 0.01 → 0.02

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,6 @@ PATH
4
4
  ruby-imgur (0.01)
5
5
  addressable
6
6
  cistern (~> 0.1.3)
7
- excon
8
7
  launchy
9
8
  multi_json
10
9
  rest-client
@@ -17,7 +16,6 @@ GEM
17
16
  cistern (0.1.4)
18
17
  coderay (1.0.8)
19
18
  diff-lcs (1.1.3)
20
- excon (0.16.10)
21
19
  launchy (2.1.2)
22
20
  addressable (~> 2.3)
23
21
  method_source (0.8.1)
@@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.add_dependency "multi_json"
19
19
  gem.add_dependency "cistern", "~> 0.1.3"
20
20
  gem.add_dependency "addressable"
21
- gem.add_dependency "excon"
22
21
  gem.add_dependency "launchy"
23
22
  gem.add_dependency "rest-client"
24
23
  end
@@ -1,7 +1,6 @@
1
1
  require 'imgur/version'
2
2
  require 'cistern'
3
3
  require 'logger'
4
- require 'excon'
5
4
  require 'launchy'
6
5
  require 'yaml'
7
6
  require 'rest-client'
@@ -5,10 +5,16 @@ class Imgur::Client < Cistern::Service
5
5
 
6
6
  model :image
7
7
  collection :images
8
+ request :get_image
8
9
  request :get_images
9
10
 
11
+ model :album
12
+ collection :albums
13
+ request :get_album
14
+ request :get_albums
15
+
10
16
  class Real
11
- attr_accessor :url, :path, :connection, :parser, :logger, :config, :authorize_path, :token_path
17
+ attr_accessor :url, :path, :parser, :logger, :config, :authorize_path, :token_path
12
18
 
13
19
  def initialize(options={})
14
20
  @config = options[:config] || YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml")
@@ -17,9 +23,11 @@ class Imgur::Client < Cistern::Service
17
23
  @url = URI.parse(options[:url] || "https://api.imgur.com")
18
24
  @logger = options[:logger] || Logger.new(nil)
19
25
  @parser = begin; require 'json'; JSON; end
26
+ end
20
27
 
21
- Excon.defaults[:ssl_verify_peer] = false
22
- @connection = Excon.new(@url.to_s)
28
+ def reset!
29
+ @config = nil
30
+ @config = YAML.load_file(File.expand_path("~/.imgurrc"))
23
31
  end
24
32
 
25
33
  def refresh_token
@@ -34,12 +42,13 @@ class Imgur::Client < Cistern::Service
34
42
  @config[:access_token] = new_params["access_token"]
35
43
  @config[:refresh_token] = new_params["refresh_token"]
36
44
  File.open(File.expand_path("~/.imgurrc"), "w") { |f| YAML.dump(@config, f) }
45
+ self.reset!
37
46
  return true
38
47
  end
39
48
 
40
49
  def request(options={})
41
50
  method = (options[:method] || :get).to_s.downcase
42
- path = "/3#{options[:path]}" || "/3"
51
+ path = @url.to_s + ("/3#{options[:path]}" || "/3")
43
52
  query = options[:query] || {}
44
53
  unless @config[:access_token]
45
54
  Launchy.open(@url.to_s + @authorize_path + "?client_id=#{@config[:client_id]}&response_type=token")
@@ -66,14 +75,12 @@ class Imgur::Client < Cistern::Service
66
75
  end
67
76
  request_body ||= options[:params] || {}
68
77
  path = "#{path}?#{query.map{|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")}" unless query.empty?
69
- options = {:method => method, :path => path, :headers => headers}
70
- response = @connection.request(options)
71
- response = if response.status == 403
72
- self.refresh_token
73
- @connection.request(options).body
74
- else
75
- response.body
76
- end
78
+ begin
79
+ response = RestClient.send(method, path, headers)
80
+ rescue RestClient::Forbidden
81
+ self.refresh_token
82
+ retry
83
+ end
77
84
  parsed_body = response.strip.empty? ? {} : parser.load(response)
78
85
  status = parsed_body.delete("status")
79
86
  Imgur::Response.new(status, {}, parsed_body).raise!
@@ -0,0 +1,12 @@
1
+ class Imgur::Client::Account < Imgur::Model
2
+ identity :id
3
+
4
+ attribute :url
5
+ attribute :bio
6
+ attribute :reputation, type: :float
7
+ attribute :created, type: :integer
8
+
9
+ def open_in_browser
10
+ Launchy.open(link)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ class Imgur::Client::Album < Imgur::Model
2
+ identity :id
3
+
4
+ attribute :title
5
+ attribute :link
6
+ attribute :datetime, type: :integer
7
+ attribute :description
8
+ attribute :privacy
9
+ attribute :cover
10
+ attribute :order, type: :integer
11
+ attribute :layout
12
+
13
+ def images
14
+ url = "/album/#{id}/images"
15
+ data = connection.get_images(path: url).body["data"]
16
+ connection.images.load(data)
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ class Imgur::Client::Albums < Cistern::Collection
2
+ include Imgur::PagedCollection
3
+ include Imgur::Collection
4
+
5
+ model Imgur::Client::Album
6
+
7
+ model_root "data"
8
+ model_request :get_album
9
+
10
+ collection_root "data"
11
+ collection_request :get_albums
12
+
13
+ def get(id)
14
+ connection.albums.new(connection.get_album(id).body["data"])
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ class Imgur::Client::Comment < Imgur::Model
2
+ identity :id
3
+
4
+ attribute :image_id
5
+ attribute :caption
6
+ attribute :author
7
+ attribute :author_id, type: :integer
8
+ attribute :on_album, type: :boolean
9
+ attribute :album_cover
10
+ attribute :ups, type: :integer
11
+ attribute :downs, type: :integer
12
+ attribute :points, type: :float
13
+ attribute :datetime, type: :integer
14
+ attribute :parent_id, type: :integer
15
+ attribute :deleted, type: :boolean
16
+ attribute :children, type: :array
17
+ end
@@ -9,4 +9,19 @@ class Imgur::Client::Images < Cistern::Collection
9
9
 
10
10
  collection_root "data"
11
11
  collection_request :get_images
12
+
13
+ def all(options={})
14
+ path = ""
15
+ path = path + "/#{options[:resource] || "gallery"}"
16
+ if options[:section]
17
+ path = path + "/#{options[:section]}"
18
+ elsif options[:subreddit]
19
+ path = path + "/r/#{options[:subreddit]}"
20
+ end
21
+ path = path + "/#{options[:sort]}" if options[:sort]
22
+ path = path + "/#{options[:page] || 0}.json"
23
+
24
+ data = connection.get_images(path: path).body["data"]
25
+ connection.images.load(data)
26
+ end
12
27
  end
@@ -0,0 +1,12 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def get_album(id)
4
+ path = "/album/#{id}"
5
+
6
+ request(
7
+ :method => :get,
8
+ :path => path,
9
+ )
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def get_albums(params={})
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def get_image(id)
4
+ path = "/#{id}"
5
+
6
+ request(
7
+ :method => :get,
8
+ :path => path,
9
+ )
10
+ end
11
+ end
12
+ end
@@ -1,16 +1,7 @@
1
1
  class Imgur::Client
2
2
  class Real
3
3
  def get_images(params={})
4
- path = ""
5
- path = path + "/#{params[:resource] || "gallery"}"
6
- if params[:section]
7
- path = path + "/#{params[:section]}"
8
- elsif params[:subreddit]
9
- path = path + "/r/#{params[:subreddit]}"
10
- end
11
- path = path + "/#{params[:sort]}" if params[:sort]
12
- path = path + "/#{params[:page] || 0}.json"
13
-
4
+ path = params[:path]
14
5
  request(
15
6
  :method => :get,
16
7
  :path => path,
@@ -1,3 +1,3 @@
1
1
  module Imgur
2
- VERSION = "0.01"
2
+ VERSION = "0.02"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-imgur
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.01'
4
+ version: '0.02'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: excon
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :runtime
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
62
  - !ruby/object:Gem::Dependency
79
63
  name: launchy
80
64
  requirement: !ruby/object:Gem::Requirement
@@ -127,9 +111,16 @@ files:
127
111
  - lib/imgur/collection.rb
128
112
  - lib/imgur/logger.rb
129
113
  - lib/imgur/model.rb
114
+ - lib/imgur/models/account.rb
115
+ - lib/imgur/models/album.rb
116
+ - lib/imgur/models/albums.rb
117
+ - lib/imgur/models/comment.rb
130
118
  - lib/imgur/models/image.rb
131
119
  - lib/imgur/models/images.rb
132
120
  - lib/imgur/paged_collection.rb
121
+ - lib/imgur/requests/get_album.rb
122
+ - lib/imgur/requests/get_albums.rb
123
+ - lib/imgur/requests/get_image.rb
133
124
  - lib/imgur/requests/get_images.rb
134
125
  - lib/imgur/response.rb
135
126
  - lib/imgur/version.rb