yandex-api-fotki 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aee299768c5b7267a58060a46da1fdc3dd3f48e3
4
- data.tar.gz: 2a5d500310cbe31825942626aad39d080c9181bb
3
+ metadata.gz: 48c767dca5abac75599d785fececa5673efc1b7c
4
+ data.tar.gz: d36bd7b446643ce8d1c153a587d9eaa1b158ec1a
5
5
  SHA512:
6
- metadata.gz: c2ad9e34b57e4d9d7617a9cf83bcd46a5e6143cc952b9d4d0bf70b3c9f69eb4eb0413429466ec92729b5a9fd1e2a3a40a6080dac326e9e23967a96d23aeaa397
7
- data.tar.gz: fd9fab21b806ad41f6f1ebf2aff014642bd1a6203b0dd51e00d92594f49e2fab630ef0d797acddd6094140b875d575a975ca9dbe88fd34a77f4ab0f601334121
6
+ metadata.gz: 69b7f2cfd65a68c1fd9ee237ee1a011b86c0ff12e99164964814205ea458151e4c60312ccc64089fe7e3d6786ad68a3e3945306040ee8830ee52b62f7f479b62
7
+ data.tar.gz: 4c6686e6df1641c081c0133f8a45d94477187843f7c2c570e7e31e3a919212d32b996bb107f5a149dd40b66cad98fd422b6c5a5c31c2fae7841993f892dd1532
@@ -1,11 +1,17 @@
1
1
  require 'rest-client'
2
2
  require 'active_support/core_ext/hash/conversions'
3
3
 
4
- require 'yandex/api/fotki/version'
4
+ require_relative 'fotki/version'
5
+ require_relative 'fotki/me'
6
+ require_relative 'fotki/albums'
7
+ require_relative 'fotki/album'
8
+ require_relative 'fotki/photos'
9
+ require_relative 'fotki/photo'
5
10
 
6
11
  module Yandex
7
12
  module API
8
13
  module Fotki
14
+ API_HOST = 'http://api-fotki.yandex.ru'
9
15
  class RuntimeError < RuntimeError ; end
10
16
  #
11
17
  # Main method for authentification
@@ -16,9 +22,9 @@ module Yandex
16
22
  # @see https://tech.yandex.ru/oauth/doc/dg/reference/web-client-docpage/
17
23
  #
18
24
  def self.oauth(oauth_code)
19
- return self unless @api_urls.nil?
25
+ return self if !@api_urls.nil? && @oauth_code === oauth_code
20
26
  @oauth_code = oauth_code
21
- me = RestClient.get('http://api-fotki.yandex.ru/api/me/',
27
+ me = RestClient.get("#{API_HOST}/api/me/",
22
28
  Fotki.oauth_hash)
23
29
  @api_urls = Me.new(me)
24
30
  self
@@ -66,126 +72,6 @@ module Yandex
66
72
  Photos
67
73
  end
68
74
  #
69
- # Class Me is wrapper for user service document
70
- #
71
- # @see https://tech.yandex.ru/fotki/doc/operations-ref/service-document-get-docpage/
72
- #
73
- class Me
74
- attr_reader :album, :photo, :tag
75
- #
76
- # Parse response from Fotki.oauth
77
- #
78
- # @param [RestClient::Response] response
79
- #
80
- def initialize(response)
81
- # Nokogiri::XML(me).xpath("/app:service/app:workspace/app:collection[@id='album-list']/@href").text
82
- collention = Fotki.xml_to_hash(response)['service']['workspace']['collection'].map{ |i| { i['id'] => i['href'] }}.inject(:merge)
83
- @album = collention['album-list']
84
- @photo = collention['photo-list']
85
- @tag = collention['tag-list']
86
- end
87
- end
88
- #
89
- # Class Albums is wrapper for albums listing
90
- #
91
- class Albums
92
- #
93
- # List user albums
94
- #
95
- # @param [Hash] options
96
- # @option options [String] sort updated or rupdated or published or rpublished
97
- # @option options [Integer] offset Not implemented
98
- # @option options [Integer] limit 100 is max
99
- #
100
- # @return [Hash] Hash of Fotki::Album were keys is id of album
101
- # @see https://tech.yandex.ru/fotki/doc/operations-ref/albums-collection-get-docpage/
102
- #
103
- def self.list(options = {})
104
- options[:sort] ||= 'updated'
105
- options[:offset] ||= 0
106
- options[:limit] ||= 10
107
-
108
- return @list_cache if !@list_cache.nil? && options === @list_options_cache
109
-
110
- @list_options_cache = options
111
-
112
- list = RestClient.get("#{Fotki.api_urls.album}#{options[:sort]}/?limit=#{options[:limit]}", Fotki.oauth_hash)
113
-
114
- @list_cache = Fotki.xml_to_hash(list)['feed']['entry']
115
- @list_cache = @list_cache.map { |i|
116
- album = Album.new(i)
117
- { album.id => album }
118
- }.inject(:merge)
119
- end
120
- end
121
- #
122
- # Class Album is wrapper for single album
123
- #
124
- class Album
125
- attr_reader :id, :title, :link
126
- #
127
- # <description>
128
- #
129
- # @param [Hash] hash Hash from {Fotki::Albums.list} response
130
- #
131
- def initialize(hash)
132
- @id = parse_id(hash['id'])
133
- @title = hash['title']
134
- @link = hash['link'].map{ |i| { i['rel'] => i['href'] } }.inject(:merge)
135
- end
136
-
137
- private
138
- def parse_id(string)
139
- string.split(':').last.to_i
140
- end
141
- end
142
-
143
- #
144
- # Class Photos provides photos collections
145
- #
146
- class Photos
147
- #
148
- # Uploading photo
149
- #
150
- # @param [Hash] hash
151
- #
152
- # == Required
153
- # @option hash [File] :image IO stream of file. For example +File.new('tmp/file.png')+ or +open('\h\ttp://site.com/image.png')+
154
- # == Optional
155
- # @option hash [String] :title Title of image
156
- # @option hash [String] :summary Description of image
157
- # @option hash [Integer] :album Id of album to upload
158
- # @option hash [String] :access public, friends or private
159
- # @see https://tech.yandex.ru/fotki/doc/concepts/add-photo-docpage/#multipart-format
160
- #
161
- # @return [Fotki::Photo] Instance of Photo class
162
- #
163
- def self.upload(hash)
164
- raise Fotki::RuntimeError, ':image is required' unless hash[:image]
165
- response = RestClient.post(Fotki.api_urls.photo, hash, Fotki.oauth_hash)
166
- Photo.new(response)
167
- end
168
- end
169
- #
170
- # Class Photo is wrapper for single photo
171
- #
172
- # @see https://tech.yandex.ru/fotki/doc/operations-ref/photo-get-docpage/
173
- #
174
- class Photo
175
- attr_reader :id, :links
176
- #
177
- # Photo initialize
178
- #
179
- # @param [RestClient::Response] response
180
- #
181
- def initialize(response)
182
- entry = Fotki.xml_to_hash(response)['entry']
183
- @id = entry['id'].split(':').last.to_i
184
- @links = entry['img'].map{ |i| { i['size'] => i } }.inject(:merge)
185
- # puts JSON.pretty_generate(entry)
186
- end
187
- end
188
- #
189
75
  # XML parser wrapper. Because I'm not sure if it be persistent.
190
76
  #
191
77
  # @param [String] xml XML input
@@ -0,0 +1,24 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ class Album
5
+ attr_reader :id, :title, :link
6
+ #
7
+ # <description>
8
+ #
9
+ # @param [Hash] options Hash from {Fotki::Albums.list} response
10
+ #
11
+ def initialize(options)
12
+ @id = parse_id(options['id'])
13
+ @title = options['title']
14
+ @link = options['link'].map{ |i| { i['rel'] => i['href'] } }.inject(:merge)
15
+ end
16
+
17
+ private
18
+ def parse_id(string)
19
+ string.split(':').last.to_i
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ class Albums
5
+ #
6
+ # List user albums
7
+ #
8
+ # @param [Hash] options
9
+ # @option options [String] sort updated or rupdated or published or rpublished
10
+ # @option options [Integer] offset Not implemented
11
+ # @option options [Integer] limit 100 is max
12
+ #
13
+ # @return [Hash] Hash of Fotki::Album were keys is id of album
14
+ # @see https://tech.yandex.ru/fotki/doc/operations-ref/albums-collection-get-docpage/
15
+ #
16
+ def self.list(options = {})
17
+ options[:sort] ||= 'updated'
18
+ options[:offset] ||= 0
19
+ options[:limit] ||= 10
20
+
21
+ return @list_cache if !@list_cache.nil? && options === @list_options_cache
22
+
23
+ @list_options_cache = options
24
+
25
+ list = RestClient.get("#{Fotki.api_urls.album}#{options[:sort]}/?limit=#{options[:limit]}", Fotki.oauth_hash)
26
+
27
+ @list_cache = Fotki.xml_to_hash(list)['feed']['entry']
28
+ @list_cache = @list_cache.map { |i|
29
+ album = Album.new(i)
30
+ { album.id => album }
31
+ }.inject(:merge)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ #
5
+ # Class Me is wrapper for user service document
6
+ #
7
+ # @see https://tech.yandex.ru/fotki/doc/operations-ref/service-document-get-docpage/
8
+ #
9
+ class Me
10
+ attr_reader :album, :photo, :tag
11
+ #
12
+ # Parse response from Fotki.oauth
13
+ #
14
+ # @param [RestClient::Response] response
15
+ #
16
+ def initialize(response)
17
+ # Nokogiri::XML(me).xpath("/app:service/app:workspace/app:collection[@id='album-list']/@href").text
18
+ collention = Fotki.xml_to_hash(response)['service']['workspace']['collection'].map{ |i| { i['id'] => i['href'] }}.inject(:merge)
19
+ @album = collention['album-list']
20
+ @photo = collention['photo-list']
21
+ @tag = collention['tag-list']
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ #
5
+ # Class Photo is wrapper for single photo
6
+ #
7
+ # @see https://tech.yandex.ru/fotki/doc/operations-ref/photo-get-docpage/
8
+ #
9
+ class Photo
10
+ attr_reader :id, :links
11
+ #
12
+ # Photo initialize
13
+ #
14
+ # @param [RestClient::Response] response
15
+ #
16
+ def initialize(response)
17
+ entry = Fotki.xml_to_hash(response)['entry']
18
+ @id = entry['id'].split(':').last.to_i
19
+ @links = entry['img'].map{ |i| { i['size'] => i } }.inject(:merge)
20
+ # puts JSON.pretty_generate(entry)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ class Photos
5
+ #
6
+ # Uploading photo
7
+ #
8
+ # @param [Hash] options
9
+ #
10
+ # == Required
11
+ # @option options [File] :image IO stream of file. For example +File.new('tmp/file.png')+ or +open('\h\ttp://site.com/image.png')+
12
+ # == Optional
13
+ # @option options [String] :title Title of image
14
+ # @option options [String] :summary Description of image
15
+ # @option options [Integer] :album Id of album to upload
16
+ # @option options [String] :access public, friends or private
17
+ # @see https://tech.yandex.ru/fotki/doc/concepts/add-photo-docpage/#multipart-format
18
+ #
19
+ # @return [Fotki::Photo] Instance of Photo class
20
+ #
21
+ def self.upload(options)
22
+ raise Fotki::RuntimeError, ':image is required' unless options[:image]
23
+ response = RestClient.post(Fotki.api_urls.photo, options, Fotki.oauth_hash)
24
+ Photo.new(response)
25
+ end
26
+ #
27
+ # Get info about photo
28
+ #
29
+ # @param [Hash] options
30
+ #
31
+ # == Required
32
+ # @option options [String] :user Owner of photo
33
+ # @option options [String] :id Photo id
34
+ #
35
+ # @return [Fotki::Photo] Instance of Photo class
36
+ #
37
+ def self.get(options)
38
+ raise Fotki::RuntimeError, ':user is required' unless options[:user]
39
+ raise Fotki::RuntimeError, ':id is required' unless options[:id]
40
+ url = "#{API_HOST}/api/users/#{options[:user]}/photo/#{options[:id].to_s}/"
41
+ headers = options.except(:user, :id).merge(Fotki.oauth_hash)
42
+ response = RestClient.get(url, headers)
43
+ Photo.new(response)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,7 +1,7 @@
1
1
  module Yandex
2
2
  module API
3
3
  module Fotki
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-api-fotki
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - github.com/1v
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-09 00:00:00.000000000 Z
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -128,6 +128,11 @@ files:
128
128
  - bin/setup
129
129
  - lib/yandex-api-fotki.rb
130
130
  - lib/yandex/api/fotki.rb
131
+ - lib/yandex/api/fotki/album.rb
132
+ - lib/yandex/api/fotki/albums.rb
133
+ - lib/yandex/api/fotki/me.rb
134
+ - lib/yandex/api/fotki/photo.rb
135
+ - lib/yandex/api/fotki/photos.rb
131
136
  - lib/yandex/api/fotki/version.rb
132
137
  - yandex-api-fotki.gemspec
133
138
  homepage: https://github.com/1v/yandex-api-fotki