yandex-api-fotki 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
6
+ addons:
7
+ code_climate:
8
+ repo_token: a6ea7a1c1b0fc9e77f061c17d327fee2659bc72cd341153402fcec1ab61c6ea4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yandex-api-fotki.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 1v
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Yandex::Api::Fotki
2
+
3
+ [![Build Status](https://travis-ci.org/1v/yandex-api-fotki.svg?branch=master)](https://travis-ci.org/1v/yandex-api-fotki)
4
+ [![Dependency Status](https://gemnasium.com/badges/github.com/1v/yandex-api-fotki.svg)](https://gemnasium.com/github.com/1v/yandex-api-fotki)
5
+ [![Code Climate](https://codeclimate.com/github/1v/yandex-api-fotki/badges/gpa.svg)](https://codeclimate.com/github/1v/yandex-api-fotki)
6
+ [![Test Coverage](https://codeclimate.com/github/1v/yandex-api-fotki/badges/coverage.svg)](https://codeclimate.com/github/1v/yandex-api-fotki/coverage)
7
+
8
+ API wrapper for Yandex Fotki
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'yandex-api-fotki'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install yandex-api-fotki
25
+
26
+ ## Usage
27
+ Инструкция как получить OAUTH_CODE https://tech.yandex.ru/oauth/doc/dg/tasks/get-oauth-token-docpage/
28
+ ```ruby
29
+ fotki = Yandex::API::Fotki.oauth(OAUTH_CODE)
30
+ photo = fotki.photos.upload(:image => File.new('/file.png'),
31
+ :access => 'private',
32
+ :album => 123456,
33
+ :title => "My Image")
34
+ orig = photo.links['orig']['href']
35
+ => http://img-fotki.yandex.ru/get/117982/392595458.57fe/0_26753c_713f7b6a_orig
36
+ ```
37
+
38
+ ## Documentation
39
+
40
+ http://www.rubydoc.info/github/1v/yandex-api-fotki/
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yandex/api/fotki"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,196 @@
1
+ require 'rest-client'
2
+ require 'active_support/core_ext/hash/conversions'
3
+
4
+ require "yandex/api/fotki/version"
5
+
6
+ module Yandex
7
+ module API
8
+ module Fotki
9
+ class RuntimeError < RuntimeError ; end
10
+ #
11
+ # Main method for authentification
12
+ #
13
+ # @param [String] oauth_code OAuth code can be recieved here https://tech.yandex.ru/oauth/doc/dg/reference/web-client-docpage
14
+ #
15
+ # @return [Fotki]
16
+ # @see https://tech.yandex.ru/oauth/doc/dg/reference/web-client-docpage/
17
+ #
18
+ def self.oauth(oauth_code)
19
+ return self unless @api_urls.nil?
20
+ @oauth_code = oauth_code
21
+ me = RestClient.get('http://api-fotki.yandex.ru/api/me/',
22
+ Fotki.oauth_hash)
23
+ @api_urls = Me.new(me)
24
+ self
25
+ end
26
+ #
27
+ # Getter for instance variable
28
+ #
29
+ #
30
+ # @return [String]
31
+ #
32
+ def self.oauth_code
33
+ @oauth_code
34
+ end
35
+ #
36
+ # OAuth header for RestClient
37
+ #
38
+ #
39
+ # @return [Hash]
40
+ #
41
+ def self.oauth_hash
42
+ { 'Authorization': "OAuth #{Fotki.oauth_code}" }
43
+ end
44
+ #
45
+ # User api urls
46
+ #
47
+ # @return [Fotki::Me]
48
+ #
49
+ def self.api_urls
50
+ @api_urls
51
+ end
52
+ #
53
+ # Fotki::Albums wrapper
54
+ #
55
+ # @return [Albums]
56
+ #
57
+ def self.albums
58
+ Albums
59
+ end
60
+ #
61
+ # Fotki::Photos wrapper
62
+ #
63
+ # @return [Fotki::Photos]
64
+ #
65
+ def self.photos
66
+ Photos
67
+ end
68
+ #
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] hash
96
+ # @option hash [String] sort updated or rupdated or published or rpublished
97
+ # @option hash [Integer] offset Not implemented
98
+ # @option hash [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(hash = { :sort => 'updated', :offset => 0, :limit => 10 })
104
+ return @list_cache unless @list_cache.nil?
105
+
106
+ list = RestClient.get("#{Fotki.api_urls.album}#{hash[:sort]}/?limit=#{hash[:limit]}", Fotki.oauth_hash)
107
+
108
+ @list_cache = Fotki.xml_to_hash(list)['feed']['entry']
109
+ @list_cache = @list_cache.map { |i|
110
+ album = Album.new(i)
111
+ { album.id => album }
112
+ }.inject(:merge)
113
+ end
114
+ end
115
+ #
116
+ # Class Album is wrapper for single album
117
+ #
118
+ class Album
119
+ attr_reader :id, :title, :link
120
+ #
121
+ # <description>
122
+ #
123
+ # @param [Hash] hash Hash from {Fotki::Albums.list} response
124
+ #
125
+ def initialize(hash)
126
+ @id = parse_id(hash['id'])
127
+ @title = hash['title']
128
+ @link = hash['link'].map{ |i| { i['rel'] => i['href'] } }.inject(:merge)
129
+ end
130
+
131
+ private
132
+ def parse_id(string)
133
+ string.split(':').last.to_i
134
+ end
135
+ end
136
+
137
+ #
138
+ # Class Photos provides photos collections
139
+ #
140
+ class Photos
141
+ #
142
+ # Uploading photo
143
+ #
144
+ # @param [Hash] hash
145
+ #
146
+ # == Required
147
+ # @option hash [File] :image IO stream of file. For example +File.new('tmp/file.png')+ or +open('\h\ttp://site.com/image.png')+
148
+ # == Optional
149
+ # @option hash [String] :title Title of image
150
+ # @option hash [String] :summary Description of image
151
+ # @option hash [Integer] :album Id of album to upload
152
+ # @option hash [String] :access public, friends or private
153
+ # @see https://tech.yandex.ru/fotki/doc/concepts/add-photo-docpage/#multipart-format
154
+ #
155
+ # @return [Fotki::Photo] Instance of Photo class
156
+ #
157
+ def self.upload(hash)
158
+ raise Fotki::RuntimeError, ':image is required' unless hash[:image]
159
+ response = RestClient.post(Fotki.api_urls.photo, hash, Fotki.oauth_hash)
160
+ Photo.new(response)
161
+ end
162
+ end
163
+ #
164
+ # Class Photo is wrapper for single photo
165
+ #
166
+ # @see https://tech.yandex.ru/fotki/doc/operations-ref/photo-get-docpage/
167
+ #
168
+ class Photo
169
+ attr_reader :id, :links
170
+ #
171
+ # Photo initialize
172
+ #
173
+ # @param [RestClient::Response] response
174
+ #
175
+ def initialize(response)
176
+ entry = Fotki.xml_to_hash(response)['entry']
177
+ @id = entry['id'].split(':').last.to_i
178
+ @links = entry['img'].map{ |i| { i['size'] => i } }.inject(:merge)
179
+ # puts JSON.pretty_generate(entry)
180
+ end
181
+ end
182
+
183
+ private
184
+ #
185
+ # XML parser wrapper. Because I'm not sure if it be persistent.
186
+ #
187
+ # @param [String] xml XML input
188
+ #
189
+ # @return [Hash] Hash
190
+ #
191
+ def self.xml_to_hash(xml)
192
+ Hash.from_xml(xml)
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,7 @@
1
+ module Yandex
2
+ module API
3
+ module Fotki
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yandex/api/fotki/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yandex-api-fotki"
8
+ spec.version = Yandex::API::Fotki::VERSION
9
+ spec.authors = ["github.com/1v"]
10
+ spec.email = ["forwardin@yandex.ru"]
11
+
12
+ spec.summary = %q{Yandex Fotki API wrapper}
13
+ spec.description = %q{Yandex Fotki API wrapper}
14
+ spec.homepage = "https://github.com/1v/yandex-api-fotki"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_runtime_dependency "rest-client", "~> 2.0", ">= 2.0.0"
25
+ spec.add_runtime_dependency "activesupport", ">= 4.0.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "codeclimate-test-reporter"
31
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yandex-api-fotki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - github.com/1v
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 4.0.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 4.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.13'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.13'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: codeclimate-test-reporter
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: Yandex Fotki API wrapper
104
+ email:
105
+ - forwardin@yandex.ru
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".codeclimate.yml"
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".rubocop.yml"
114
+ - ".travis.yml"
115
+ - Gemfile
116
+ - LICENSE.txt
117
+ - README.md
118
+ - Rakefile
119
+ - bin/console
120
+ - bin/setup
121
+ - lib/yandex/api/fotki.rb
122
+ - lib/yandex/api/fotki/version.rb
123
+ - yandex-api-fotki.gemspec
124
+ homepage: https://github.com/1v/yandex-api-fotki
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.5.1
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Yandex Fotki API wrapper
148
+ test_files: []
149
+ has_rdoc: