flickrie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ TODO.md
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem "faraday-stack", :require => false
4
+ gem 'activesupport'
5
+ gem "rake"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Janko Marohnić
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Flickrie
2
+
3
+ ## About
4
+
5
+ This gem is a nice wrapper for the Flickr API an intuitive interface.
6
+
7
+ The reason why I did this gem is because the other ones either weren't
8
+ well maintained, or they were too literal in the sense that the response from
9
+ the API call wasn't processed almost at all. It doesn't seem too bad
10
+ at first, but after a while you realize it's not pretty. So I wanted to
11
+ make it pretty :)
12
+
13
+ ## Examples of usage
14
+
15
+ You first need to set the API key.
16
+
17
+ ```ruby
18
+ require 'flickrie'
19
+ Flickr.api_key = "some_api_key"
20
+ ```
21
+
22
+ Then you can search for photos.
23
+
24
+ ```ruby
25
+ photos = Flickr.photos_from_set(819234) # => [#<Photo: id="8232348", ...>, #<Photo: id="8194318", ...>, ...]
26
+
27
+ photo = photos.first
28
+ photo.id # => "8232348"
29
+ photo.url # => "http://www.flickr.com/photos/67313352@N04/8232348"
30
+ photo.title # => "Samantha and me"
31
+ photo.owner # => #<User: nsid="67313352@N04", ...>
32
+ photo.owner.nsid # => "67313352@N04"
33
+ ```
34
+
35
+ You can also throw in some parameters to get more information about photos. For example,
36
+
37
+ ```ruby
38
+ photos = Flickr.photos_from_set(819234, :extras => 'owner_name,last_update,tags,views')
39
+
40
+ photo = photos.first
41
+ photo.tags # => "cave cold forrest"
42
+ photo.owner.username # => "jsmith"
43
+ photo.updated_at # => 2012-04-20 23:29:17 +0200
44
+ photo.views_count # => 24
45
+ ```
46
+
47
+ On the list of available parameters you can read in the [Flickr API documentation](http://www.flickr.com/services/api/), under the corresponding API method name (in the above case it's `flickr.photosets.getPhotos`).
48
+
49
+ You can also get additional info on a single photo:
50
+
51
+ ```ruby
52
+ photo = Flickr.get_photo_info(8232348)
53
+
54
+ photo.description # => "In this photo, Samantha and me found a secret tunnel..."
55
+ photo.comments_count # => 6
56
+ photo.visibility.public? # => true
57
+ photo.can_download? # => true
58
+ photo.owner.real_name # => "John Smith"
59
+ photo.location.country.name # => "United Stated"
60
+ ```
61
+
62
+ You can also get this info on an existing photo:
63
+
64
+ ```ruby
65
+ photo.description # => nil
66
+ photo.get_info
67
+ photo.description # => "In this photo Peter said something really funny..."
68
+ ```
69
+
70
+ If you want to display photos from flickr in your app, this is probably the most useful part:
71
+
72
+ ```ruby
73
+ photo = Flickr.photo_get_sizes(8232348)
74
+
75
+ photo.medium!(800)
76
+ photo.size # => "Medium 800"
77
+ photo.source_url # => "http://farm8.staticflickr.com/7049/6946979188_25bb44852b_c.jpg"
78
+ photo.width # => 600
79
+ photo.height # => 800
80
+
81
+ photo.small!(320)
82
+ photo.size # => "Small 320"
83
+ photo.source_url # => "http://farm8.staticflickr.com/7049/6946979188_25bb44852b_n.jpg"
84
+ photo.width # => 240
85
+ photo.width # => 320
86
+ ```
87
+
88
+ If you want sizes to be available to photos you're fetching from a set, it's a bad idea to call `#get_info` on each photo. Instead, you should pass in these options:
89
+
90
+ ```ruby
91
+ photos = Flickr.photos_from_set(1242379, :extras => 'url_sq url_q url_t url_s url_n url_m url_z url_c url_l url_o')
92
+ photo = photos.first
93
+ photo.medium!(640)
94
+ photo.source_url # => "http://farm8.staticflickr.com/7049/6946979188_25bb44852b_z.jpg"
95
+ ```
96
+
97
+ These are just some of the cool things you can do. To see a full list of available methods, I encourage you to read the [wiki](https://github.com/janko-m/flickrie/wiki). I promise, I will document the methods properly in near future :)
98
+
99
+ ## A few words
100
+
101
+ Now, I covered only a few out of many Flickr's API methods using this approach, but I'll constantly update this gem with new API methods. For all of the methods I didn't cover, you can call them using `Flickr.client`, like this:
102
+
103
+ ```ruby
104
+ response = Flickr.client.get("flickr.photos.getContext", :photo_id => 2842732)
105
+ reponse.body # =>
106
+ # {
107
+ # "count" => {"_content" => 99},
108
+ # "prevphoto" => {
109
+ # "id" => "6946978706",
110
+ # "secret" => "b38270bbd6",
111
+ # ...
112
+ # }
113
+ # "nextphoto" => {
114
+ # "id" => "6946979704",
115
+ # "secret" => "74513ff732",
116
+ # ...
117
+ # }
118
+ # }
119
+ ```
120
+
121
+ It's not nearly as pretty, but at least you can get to the data
122
+
123
+ ## Issues
124
+
125
+ Please, feel free to post any issues that you're having, I will try to
126
+ help you in any way I can.
127
+
128
+ ## Currently covered API methods
129
+
130
+ ### people
131
+ - `flickr.people.findByEmail`
132
+ - `flickr.people.findByUsername`
133
+ - `flickr.people.getInfo`
134
+ - `flickr.people.getPublicPhotos`
135
+
136
+ ### photos
137
+ - `flickr.photos.getInfo`
138
+ - `flickr.photos.getSizes`
139
+ - `flickr.photos.search`
140
+
141
+ ### photos.licenses
142
+ - `flickr.photos.licenses.getInfo`
143
+
144
+ ### photosets
145
+ - `flickr.photosets.getInfo`
146
+ - `flickr.photosets.getList`
147
+ - `flickr.photosets.getPhotos`
148
+
149
+ ## License
150
+
151
+ [MIT](http://github.com/janko-m/flickrie/blob/master/LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+
3
+ CURRENT_DIR = File.expand_path(File.dirname(__FILE__))
4
+
5
+ task :test do
6
+ Dir["#{CURRENT_DIR}/test/**/*_test.rb"].each do |test|
7
+ system "ruby -Ilib -Itest #{test}"
8
+ end
9
+ end
10
+
11
+ namespace :test do
12
+ test_rbs = Dir["#{CURRENT_DIR}/test/*_test.rb"]
13
+ test_rbs.map { |t| File.basename(t).chomp('_test.rb') }.each do |test_name|
14
+ task(test_name.to_sym) do
15
+ system "ruby -Ilib -Itest test/#{test_name}_test.rb"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,84 @@
1
+ # activity
2
+
3
+ # auth
4
+
5
+ # auth.oauth
6
+
7
+ # blogs
8
+
9
+ # collections
10
+
11
+ # commons
12
+
13
+ # contacts
14
+
15
+ # favorites
16
+
17
+ # galleries
18
+
19
+ # groups
20
+
21
+ # groups.members
22
+
23
+ # groups.pools
24
+
25
+ # interestingness
26
+
27
+ # machinetags
28
+
29
+ # panda
30
+
31
+ # people
32
+
33
+ - flickr.people.findByEmail
34
+ - flickr.people.findByUsername
35
+ - flickr.people.getInfo
36
+ - flickr.people.getPublicPhotos
37
+
38
+ # photos
39
+
40
+ - flickr.photos.getInfo
41
+ - flickr.photos.getSizes
42
+ - flickr.photos.search
43
+
44
+ # photos.comments
45
+
46
+ # photos.geo
47
+
48
+ # photos.licenses
49
+
50
+ - flickr.photos.licenses.getInfo
51
+
52
+ # photos.notes
53
+
54
+ # photos.people
55
+
56
+ # photos.suggestions
57
+
58
+ # photos.transform
59
+
60
+ # photos.upload
61
+
62
+ # photosets
63
+
64
+ - flickr.photosets.getInfo
65
+ - flickr.photosets.getList
66
+ - flickr.photosets.getPhotos
67
+
68
+ # photosets.comments
69
+
70
+ # places
71
+
72
+ # prefs
73
+
74
+ # push
75
+
76
+ # reflection
77
+
78
+ # stats
79
+
80
+ # tags
81
+
82
+ # test
83
+
84
+ # urls
data/flickrie.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/flickrie/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Janko Marohnić"]
6
+ gem.email = ["janko.marohnic@gmail.com"]
7
+ gem.description = %q{This gem is a nice wrapper for the Flickr API an intuitive interface.}
8
+ gem.summary = %q{The reason why I did this gem is because the other ones either weren't well maintained, or they were too literal in the sense that the response from the API call wasn't processed almost at all. It doesn't seem too bad at first, but after a while you realize it's not pretty. So I wanted to make it pretty :)}
9
+ gem.homepage = "https://github.com/janko-m/flickrie"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "flickrie"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Flickrie::VERSION
17
+
18
+ gem.add_dependency 'faraday-stack'
19
+ gem.add_dependency 'activesupport'
20
+ end
@@ -0,0 +1,103 @@
1
+ require 'faraday_stack'
2
+
3
+ module Flickrie
4
+ class << self
5
+ attr_reader :api_key, :timeout, :open_timeout
6
+
7
+ def api_key=(api_key); @client = nil; @api_key = api_key end
8
+ def timeout=(api_key); @client = nil; @timeout = api_key end
9
+ def open_timeout=(api_key); @client = nil; @open_timeout = api_key end
10
+
11
+ def client
12
+ @client ||= begin
13
+ client = FaradayStack.build Client,
14
+ :url => 'http://api.flickr.com/services/rest/',
15
+ :params => {
16
+ :format => 'json',
17
+ :nojsoncallback => '1',
18
+ :api_key => self.api_key
19
+ },
20
+ :request => {
21
+ :open_timeout => self.open_timeout || 8,
22
+ :timeout => self.timeout || 8
23
+ }
24
+
25
+ client.builder.insert_before FaradayStack::ResponseJSON, StatusCheck
26
+ client
27
+ end
28
+ end
29
+ end
30
+
31
+ class Error < StandardError
32
+ end
33
+
34
+ class StatusCheck < Faraday::Response::Middleware
35
+ def on_complete(env)
36
+ if env[:body]['stat'] != 'ok'
37
+ raise Error, env[:body]['message']
38
+ end
39
+ end
40
+ end
41
+
42
+ class Client < Faraday::Connection
43
+ def get(method, params = {})
44
+ super() do |req|
45
+ req.params[:method] = method
46
+ req.params.update(params)
47
+ end
48
+ end
49
+
50
+ # people
51
+ def find_user_by_email(email)
52
+ get 'flickr.people.findByEmail', :find_email => email
53
+ end
54
+
55
+ def find_user_by_username(username)
56
+ get 'flickr.people.findByUsername', :username => username
57
+ end
58
+
59
+ def get_user_info(user_nsid)
60
+ get 'flickr.people.getInfo', :user_id => user_nsid
61
+ end
62
+
63
+ def public_media_from_user(user_nsid, params = {})
64
+ params = {:user_id => user_nsid}.merge(params)
65
+ params[:extras] = [params[:extras], 'media'].compact.join(',')
66
+ get 'flickr.people.getPublicPhotos', params
67
+ end
68
+
69
+ # photos
70
+ def get_media_info(media_id)
71
+ get 'flickr.photos.getInfo', :photo_id => media_id
72
+ end
73
+
74
+ def get_media_sizes(media_id)
75
+ get 'flickr.photos.getSizes', :photo_id => media_id
76
+ end
77
+
78
+ def search_media(params = {})
79
+ params[:extras] = [params[:extras], 'media'].compact.join(',')
80
+ get 'flickr.photos.search', params
81
+ end
82
+
83
+ # licenses
84
+ def get_licenses
85
+ get 'flickr.photos.licenses.getInfo'
86
+ end
87
+
88
+ # photosets
89
+ def get_set_info(set_id)
90
+ get 'flickr.photosets.getInfo', :photoset_id => set_id
91
+ end
92
+
93
+ def sets_from_user(user_nsid)
94
+ get 'flickr.photosets.getList', :user_id => user_nsid
95
+ end
96
+
97
+ def media_from_set(set_id, params = {})
98
+ params = {:photoset_id => set_id}.merge(params)
99
+ params[:extras] = [params[:extras], 'media'].compact.join(',')
100
+ get 'flickr.photosets.getPhotos', params
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,38 @@
1
+ module Flickrie
2
+ class License
3
+ attr_reader :id, :name, :url
4
+
5
+ private
6
+
7
+ def initialize(argument)
8
+ if argument.is_a?(Hash)
9
+ @id = argument['id']
10
+ @name = argument['name']
11
+ @url = argument['url']
12
+ elsif argument.is_a?(String)
13
+ hash = self.class.response_array.find do |hash|
14
+ hash['id'] == argument
15
+ end
16
+ initialize(hash)
17
+ end
18
+ end
19
+
20
+ def self.response_array
21
+ [
22
+ {"id"=>"0", "name"=>"All Rights Reserved", "url"=>""},
23
+ {"id"=>"1", "name"=>"Attribution-NonCommercial-ShareAlike License", "url"=>"http://creativecommons.org/licenses/by-nc-sa/2.0/"},
24
+ {"id"=>"2", "name"=>"Attribution-NonCommercial License", "url"=>"http://creativecommons.org/licenses/by-nc/2.0/"},
25
+ {"id"=>"3", "name"=>"Attribution-NonCommercial-NoDerivs License", "url"=>"http://creativecommons.org/licenses/by-nc-nd/2.0/"},
26
+ {"id"=>"4", "name"=>"Attribution License", "url"=>"http://creativecommons.org/licenses/by/2.0/"},
27
+ {"id"=>"5", "name"=>"Attribution-ShareAlike License", "url"=>"http://creativecommons.org/licenses/by-sa/2.0/"},
28
+ {"id"=>"6", "name"=>"Attribution-NoDerivs License", "url"=>"http://creativecommons.org/licenses/by-nd/2.0/"},
29
+ {"id"=>"7", "name"=>"No known copyright restrictions", "url"=>"http://www.flickr.com/commons/usage/"},
30
+ {"id"=>"8", "name"=>"United States Government Work", "url"=>"http://www.usa.gov/copyright.shtml"}
31
+ ]
32
+ end
33
+
34
+ def self.from_hash(licenses_hash)
35
+ licenses_hash.map { |info| new(info) }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ module Flickrie
2
+ class Location
3
+ %w[latitude longitude accuracy context place_id woeid].each do |attr_name|
4
+ define_method(attr_name) do
5
+ @info[attr_name]
6
+ end
7
+ end
8
+
9
+ %w[neighbourhood locality county region country].each do |place_name|
10
+ define_method(place_name) do
11
+ if @info[place_name]
12
+ Struct.new(:name, :place_id, :woeid).new \
13
+ @info[place_name]['_content'],
14
+ @info[place_name]['place_id'],
15
+ @info[place_name]['woeid']
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def initialize(info = {})
23
+ @info = info
24
+ end
25
+ end
26
+ end
27
+
28
+ __END__
29
+
30
+ {
31
+ "latitude" => 37.792608,
32
+ "longitude" => -122.402672,
33
+ "accuracy" => "14",
34
+ "context" => "0",
35
+ "neighbourhood" => {
36
+ "_content" => "Financial District",
37
+ "place_id" => "GddgqTpTUb8LgT93hw",
38
+ "woeid" => "23512022"
39
+ },
40
+ "locality" => {
41
+ "_content" => "San Francisco",
42
+ "place_id" => "7.MJR8tTVrIO1EgB",
43
+ "woeid" => "2487956"
44
+ },
45
+ "county" => {
46
+ "_content" => "San Francisco",
47
+ "place_id" => ".7sOmlRQUL9nK.kMzA",
48
+ "woeid" => "12587707"
49
+ },
50
+ "region" => {
51
+ "_content" => "California",
52
+ "place_id" => "NsbUWfBTUb4mbyVu",
53
+ "woeid" => "2347563"
54
+ },
55
+ "country" => {
56
+ "_content" => "United States",
57
+ "place_id" => "nz.gsghTUb4c2WAecA",
58
+ "woeid" => "23424977"
59
+ },
60
+ "place_id" =>"GddgqTpTUb8LgT93hw",
61
+ "woeid" =>"23512022"
62
+ }
@@ -0,0 +1,23 @@
1
+ module Flickrie
2
+ module Media
3
+ class Note
4
+ attr_reader :id, :author, :coordinates, :content,
5
+ :width, :height
6
+
7
+ def to_s
8
+ @content
9
+ end
10
+
11
+ def initialize(hash)
12
+ @id = hash['id']
13
+ @author = User.new \
14
+ 'nsid' => hash['author'],
15
+ 'username' => hash['authorname']
16
+ @content = hash['_content']
17
+ @coordinates = [hash['x'].to_i, hash['y'].to_i]
18
+ @width = hash['w'].to_i
19
+ @height = hash['h'].to_i
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module Flickrie
2
+ module Media
3
+ class Visibility
4
+ def public?; @visibility[0].to_i == 1 if @visibility[0] end
5
+ def friends?; @visibility[1].to_i == 1 if @visibility[1] end
6
+ def family?; @visibility[2].to_i == 1 if @visibility[2] end
7
+ def contacts?; @visibility[3].to_i == 1 if @visibility[3] end
8
+
9
+ def initialize(*visibility)
10
+ @visibility = visibility.flatten
11
+ end
12
+ end
13
+ end
14
+ end