ruby-imgur 0.02 → 0.03

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,24 @@
1
- = imgur
1
+ # imgur
2
2
 
3
3
  Library to interface with the imgur API.
4
4
 
5
- == Usage
5
+ ## Usage
6
6
 
7
- 1. Register your application with imgur at https://api.imgur.com/oauth2/addclient
8
- 2. Place a file called .imgurrc in your user's $HOME directory with the following contents
9
- :client_id: "client_id_from_registering_app"
10
- :client_secret: "client_secret_from_registering_app"
11
- :account_username: "username_of_imgur_account"
12
- 3. On first run, you will be presented with a browser window asking you to allow access to your account, do this.
13
- 4. After approval, you will be prompted to enter both the access_token and refresh_token values, which can be retrieved from the URL of the page you are redirected to
7
+ 1. Register your application with imgur at https://api.imgur.com/oauth2/addclient
8
+ 2. Place a file called .imgurrc in your user's $HOME directory with the following contents
9
+ <pre>
10
+ :client_id: "client_id_from_registering_app"
11
+ :client_secret: "client_secret_from_registering_app"
12
+ :account_username: "username_of_imgur_account"
13
+ </pre>
14
+ 3. On first run, you will be presented with a browser window asking you to allow access to your account, do this.
15
+ 4. After approval, you will be prompted to enter both the access_token and refresh_token values, which can be retrieved from the URL of the page you are redirected to
14
16
 
15
- Examples:
17
+ ### Examples:
16
18
 
17
- #Get first page of gallery
19
+ #### Get first page of gallery:
20
+
21
+ ```ruby
18
22
  client = Imgur::Client.new
19
23
  client.images.all(resource: "gallery", page: 0)
20
24
  -> [
@@ -22,18 +26,25 @@ Examples:
22
26
  ...
23
27
  [173] #<Imgur::Client::Image:0x7fa55ad4a5b0 attributes={id:"CmIrs",title:"Day officially made",datetime:1355595301,animated:false,width:245,height:176,size:899725,views:21774,bandwidth:19590612150,account_url:"idontwantoliveonthisplanetanymore",link:"http://i.imgur.com/CmIrs.gif",ups:269,downs:6,score:273,is_album:false}>
24
28
  ]
29
+ ```
30
+
31
+ #### Open image in browser
32
+
33
+ ```ruby
34
+ client = Imgur::Client.new
35
+ image = client.images.all.first
36
+ image.open_in_browser
37
+ ```
25
38
 
26
- == Contributing to imgur
39
+ ## Contributing to imgur
27
40
 
28
41
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
29
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
30
42
  * Fork the project
31
43
  * Start a feature/bugfix branch
32
44
  * Commit and push until you are happy with your contribution
33
45
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
34
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
35
46
 
36
- == Copyright
47
+ ## Copyright
37
48
 
38
49
  Copyright (c) 2012 Eugene Howe. See LICENSE.txt for
39
50
  further details.
data/lib/imgur/client.rb CHANGED
@@ -7,14 +7,24 @@ class Imgur::Client < Cistern::Service
7
7
  collection :images
8
8
  request :get_image
9
9
  request :get_images
10
+ request :upload_image
11
+ request :delete_image
10
12
 
11
13
  model :album
12
14
  collection :albums
13
15
  request :get_album
14
16
  request :get_albums
15
17
 
18
+ model :account
19
+ collection :accounts
20
+ request :get_accounts
21
+ request :get_account
22
+
23
+ model :basic_response
24
+ collection :basic_responses
25
+
16
26
  class Real
17
- attr_accessor :url, :path, :parser, :logger, :config, :authorize_path, :token_path
27
+ attr_accessor :url, :path, :parser, :logger, :config, :authorize_path, :token_path, :connection
18
28
 
19
29
  def initialize(options={})
20
30
  @config = options[:config] || YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml")
@@ -23,6 +33,7 @@ class Imgur::Client < Cistern::Service
23
33
  @url = URI.parse(options[:url] || "https://api.imgur.com")
24
34
  @logger = options[:logger] || Logger.new(nil)
25
35
  @parser = begin; require 'json'; JSON; end
36
+ @connection = RestClient::Resource.new(@url)
26
37
  end
27
38
 
28
39
  def reset!
@@ -48,7 +59,7 @@ class Imgur::Client < Cistern::Service
48
59
 
49
60
  def request(options={})
50
61
  method = (options[:method] || :get).to_s.downcase
51
- path = @url.to_s + ("/3#{options[:path]}" || "/3")
62
+ path = "/3#{options[:path]}" || "/3"
52
63
  query = options[:query] || {}
53
64
  unless @config[:access_token]
54
65
  Launchy.open(@url.to_s + @authorize_path + "?client_id=#{@config[:client_id]}&response_type=token")
@@ -57,6 +68,7 @@ class Imgur::Client < Cistern::Service
57
68
  puts "Copy and paste refresh_token from URL here"
58
69
  refresh_token = $stdin.gets.strip
59
70
  @config[:access_token] = verifier
71
+ @config[:refresh_token] = refresh_token
60
72
  File.open(File.expand_path("~/.imgurrc"), 'w') { |f| YAML.dump(@config, f) }
61
73
  end
62
74
  headers = {
@@ -64,20 +76,22 @@ class Imgur::Client < Cistern::Service
64
76
  "Authorization" => "Bearer #{@config[:access_token]}",
65
77
  }.merge(options[:headers] || {})
66
78
 
67
- request_body = if body = options[:body]
68
- json_body = parser.dump(body)
69
- headers = {
70
- "Content-Type" => "application/json, charset=utf-8",
71
- "Content-Length" => json_body.size.to_s,
72
- }.merge(options[:headers] || {})
73
-
74
- json_body
79
+ request_body = if body = options[:body]
80
+ headers.merge!("Content-Type" => "application/json, charset=utf-8", "Content-Length" => body.to_s.size.to_s,)
81
+ body
75
82
  end
76
83
  request_body ||= options[:params] || {}
77
84
  path = "#{path}?#{query.map{|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")}" unless query.empty?
78
85
  begin
79
- response = RestClient.send(method, path, headers)
80
- rescue RestClient::Forbidden
86
+ response = case method
87
+ when "get"
88
+ @connection[path].get(headers)
89
+ when "post"
90
+ @connection[path].post(request_body, headers)
91
+ when "delete"
92
+ @connection[path].delete(headers)
93
+ end
94
+ rescue RestClient::Forbidden => e
81
95
  self.refresh_token
82
96
  retry
83
97
  end
@@ -85,6 +99,14 @@ class Imgur::Client < Cistern::Service
85
99
  status = parsed_body.delete("status")
86
100
  Imgur::Response.new(status, {}, parsed_body).raise!
87
101
  end
102
+
103
+ def credits
104
+ response = request(
105
+ method: :get,
106
+ path: "/credits"
107
+ ).body["data"]
108
+ "#{response["UserRemaining"]}/#{response["UserLimit"]}"
109
+ end
88
110
  end
89
111
 
90
112
  class Mock
@@ -6,7 +6,22 @@ class Imgur::Client::Account < Imgur::Model
6
6
  attribute :reputation, type: :float
7
7
  attribute :created, type: :integer
8
8
 
9
- def open_in_browser
10
- Launchy.open(link)
9
+ def images
10
+ data = []
11
+ image_array = []
12
+ page = 0
13
+ until data.count > 0 && data.count != 50
14
+ path = "/account/me/images/#{page}"
15
+ data = connection.get_images(path: path).body["data"]
16
+ data.each { |i| image_array << i }
17
+ page += 1
18
+ end
19
+ connection.images.load(image_array)
20
+ end
21
+
22
+ def albums
23
+ path = "/account/me/albums"
24
+ data = connection.get_albums(path: path).body["data"]
25
+ connection.albums.load(data)
11
26
  end
12
27
  end
@@ -0,0 +1,19 @@
1
+ class Imgur::Client::Accounts < Cistern::Collection
2
+ include Imgur::PagedCollection
3
+ include Imgur::Collection
4
+
5
+ model Imgur::Client::Account
6
+
7
+ model_root "data"
8
+ model_request :get_account
9
+
10
+ collection_root "data"
11
+ collection_request :get_accounts
12
+
13
+ def all(options={})
14
+ path = "/account/me"
15
+
16
+ data = connection.get_accounts(path: path).body["data"]
17
+ connection.accounts.load([data])
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ class Imgur::Client::BasicResponse < Imgur::Model
2
+ attribute :data
3
+ attribute :success, type: :boolean
4
+ attribute :status, type: :integer
5
+ end
@@ -0,0 +1,13 @@
1
+ class Imgur::Client::BasicResponses < Cistern::Collection
2
+ include Imgur::PagedCollection
3
+ include Imgur::Collection
4
+
5
+ model Imgur::Client::BasicResponse
6
+
7
+ model_root "data"
8
+ model_request :get_basic_response
9
+
10
+ collection_root "data"
11
+ collection_request :get_basic_responses
12
+
13
+ end
@@ -16,8 +16,14 @@ class Imgur::Client::Image < Imgur::Model
16
16
  attribute :downs, type: :integer
17
17
  attribute :score, type: :integer
18
18
  attribute :is_album, type: :boolean
19
+ attribute :deletehash
19
20
 
20
21
  def open_in_browser
21
22
  Launchy.open(link)
22
23
  end
24
+
25
+ def delete
26
+ data = connection.delete_image(deletehash).body
27
+ connection.basic_responses.new(data)
28
+ end
23
29
  end
@@ -24,4 +24,18 @@ class Imgur::Client::Images < Cistern::Collection
24
24
  data = connection.get_images(path: path).body["data"]
25
25
  connection.images.load(data)
26
26
  end
27
+
28
+ def upload(options={})
29
+ raise ArgumentError, ":image is missing: File name or URL requrired" unless options[:image]
30
+ options[:image] = case options[:image]
31
+ when /^(http|ftp)/
32
+ options[:image]
33
+ when /\.(jp(e)?g|gif|bmp|png|tif(f)?)$/i
34
+ File.open(options[:image], 'rb')
35
+ else
36
+ raise ArgumentError, "Invalid image value"
37
+ end
38
+ data = connection.upload_image(options).body["data"]
39
+ connection.images.new(data)
40
+ end
27
41
  end
@@ -0,0 +1,12 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def delete_image(deletehash)
4
+ path = "/image/#{deletehash}"
5
+
6
+ request(
7
+ :method => :delete,
8
+ :path => path,
9
+ )
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def get_account(id)
4
+ path = "/#{id}"
5
+
6
+ request(
7
+ :method => :get,
8
+ :path => path,
9
+ )
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def get_accounts(params={})
4
+ path = params[:path]
5
+ request(
6
+ :method => :get,
7
+ :path => path,
8
+ )
9
+ end
10
+ end
11
+ end
@@ -1,6 +1,11 @@
1
1
  class Imgur::Client
2
2
  class Real
3
3
  def get_albums(params={})
4
+ path = params[:path]
5
+ request(
6
+ method: :get,
7
+ path: path,
8
+ )
4
9
  end
5
10
  end
6
11
  end
@@ -1,7 +1,7 @@
1
1
  class Imgur::Client
2
2
  class Real
3
3
  def get_image(id)
4
- path = "/#{id}"
4
+ path = "/image/#{id["id"]}"
5
5
 
6
6
  request(
7
7
  :method => :get,
@@ -0,0 +1,13 @@
1
+ class Imgur::Client
2
+ class Real
3
+ def upload_image(options)
4
+ path = "/upload"
5
+
6
+ request(
7
+ :method => :post,
8
+ :path => path,
9
+ :body => options,
10
+ )
11
+ end
12
+ end
13
+ end
data/lib/imgur/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Imgur
2
- VERSION = "0.02"
2
+ VERSION = "0.03"
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "accounts" do
4
+ let(:client) { create_client }
5
+
6
+ it "should have images" do
7
+ account = client.accounts.all.first
8
+ images = account.images
9
+ images.should_not be_empty
10
+ images.first.should be_a_kind_of(Imgur::Client::Image)
11
+ end
12
+
13
+ it "should have albums" do
14
+ account = client.accounts.all.first
15
+ albums = account.albums
16
+ albums.should_not be_empty
17
+ albums.first.should be_a_kind_of(Imgur::Client::Album)
18
+
19
+ album = albums.select { |a| a.privacy == "public" }.last
20
+ album_images = album.images
21
+ album_images.should_not be_empty
22
+ album_images.first.should be_a_kind_of(Imgur::Client::Image)
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "clients" do
4
+ let (:client) { create_client }
5
+ it "should have credits" do
6
+ client.credits.should_not be_nil
7
+ pending if client.credits == "0/1000"
8
+ end
9
+ end
data/spec/images_spec.rb CHANGED
@@ -8,4 +8,22 @@ describe "images" do
8
8
  images.should_not be_empty
9
9
  images.first.should be_a_kind_of(Imgur::Client::Image)
10
10
  end
11
+
12
+ it "should upload an image from filesystem" do
13
+ image = File.expand_path("spec/support/ruby_logo.jpg")
14
+ response = client.images.upload(image: image, title: "ruby_logo_jpg")
15
+ response.should be_a_kind_of(Imgur::Client::Image)
16
+
17
+ response = response.delete
18
+ response.should be_a_kind_of(Imgur::Client::BasicResponse)
19
+ end
20
+
21
+ it "should upload an image from the internet" do
22
+ image = "http://www.ruby-lang.org/images/logo.gif"
23
+ response = client.images.upload(image: image, title: "ruby_logo_gif")
24
+ response.should be_a_kind_of(Imgur::Client::Image)
25
+
26
+ response = response.delete
27
+ response.should be_a_kind_of(Imgur::Client::BasicResponse)
28
+ end
11
29
  end
Binary file
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.02'
4
+ version: '0.03'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-16 00:00:00.000000000 Z
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -103,7 +103,7 @@ files:
103
103
  - Gemfile
104
104
  - Gemfile.lock
105
105
  - LICENSE.txt
106
- - README.rdoc
106
+ - README.md
107
107
  - Rakefile
108
108
  - imgur.gemspec
109
109
  - lib/imgur.rb
@@ -112,21 +112,31 @@ files:
112
112
  - lib/imgur/logger.rb
113
113
  - lib/imgur/model.rb
114
114
  - lib/imgur/models/account.rb
115
+ - lib/imgur/models/accounts.rb
115
116
  - lib/imgur/models/album.rb
116
117
  - lib/imgur/models/albums.rb
118
+ - lib/imgur/models/basic_response.rb
119
+ - lib/imgur/models/basic_responses.rb
117
120
  - lib/imgur/models/comment.rb
118
121
  - lib/imgur/models/image.rb
119
122
  - lib/imgur/models/images.rb
120
123
  - lib/imgur/paged_collection.rb
124
+ - lib/imgur/requests/delete_image.rb
125
+ - lib/imgur/requests/get_account.rb
126
+ - lib/imgur/requests/get_accounts.rb
121
127
  - lib/imgur/requests/get_album.rb
122
128
  - lib/imgur/requests/get_albums.rb
123
129
  - lib/imgur/requests/get_image.rb
124
130
  - lib/imgur/requests/get_images.rb
131
+ - lib/imgur/requests/upload_image.rb
125
132
  - lib/imgur/response.rb
126
133
  - lib/imgur/version.rb
134
+ - spec/accounts_spec.rb
135
+ - spec/clients_spec.rb
127
136
  - spec/images_spec.rb
128
137
  - spec/spec_helper.rb
129
138
  - spec/support/client.rb
139
+ - spec/support/ruby_logo.jpg
130
140
  homepage: http://github.com/ehowe/ruby-imgur.git
131
141
  licenses: []
132
142
  post_install_message:
@@ -152,7 +162,10 @@ signing_key:
152
162
  specification_version: 3
153
163
  summary: ''
154
164
  test_files:
165
+ - spec/accounts_spec.rb
166
+ - spec/clients_spec.rb
155
167
  - spec/images_spec.rb
156
168
  - spec/spec_helper.rb
157
169
  - spec/support/client.rb
170
+ - spec/support/ruby_logo.jpg
158
171
  has_rdoc: