flickr_party 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.markdown +16 -11
  3. data/lib/flickr_party.rb +51 -48
  4. metadata +13 -17
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 124c9c8ab8c4ad7dc62b449929431c0b85b45d4e
4
+ data.tar.gz: 25dc6fe51bc1ed997b3545bade79a10125f4e7e4
5
+ SHA512:
6
+ metadata.gz: e65a692fce5a7f94c904117f1cbc731b3460a240b86f6ca5310dde7c67c1e7cd24befeb12109a3d6d8f2a6bb95166d694d8f096454c9d8c7f714e4125ca1a865
7
+ data.tar.gz: 04cb7b50f136cedd81a139a978cf14873e4c679199727cbb6b66ae290d30471d538af7a7f6ebfe261461a6314993810c79bb814fda75a0fbf9a275ccdb248fa5
data/README.markdown CHANGED
@@ -1,19 +1,16 @@
1
- Flickr Party
2
- ============
1
+ # Flickr Party
3
2
 
4
3
  Flickr Party is the simplest possible thing that might work for you. We build on HTTParty, and do a bit of `method_missing` magic to accept any method that the official Flickr API supports.
5
4
 
6
5
  You can also (optionally) do authenticated API calls using the `auth_url` and `complete_auth` methods (see below).
7
6
 
8
- Installation
9
- ------------
7
+ ## Installation
10
8
 
11
9
  ```sh
12
10
  gem install flickr_party
13
11
  ```
14
-
15
- Usage
16
- -----
12
+
13
+ ## Usage
17
14
 
18
15
  ```ruby
19
16
  require 'rubygems'
@@ -33,9 +30,17 @@ token = f.complete_auth
33
30
 
34
31
  # call any API method by calling it on the FlickrParty object directly
35
32
  # pass in any needed parameters as a hash
36
- # pass in the auth_token if authentication is required for this call
37
- data = f.flickr.activity.userPhotos('timeframe' => '10d', 'auth_token' => token)
33
+ data = f.flickr.activity.userPhotos('timeframe' => '10d')
38
34
 
39
- # data is presented vary raw, in the "rsp" key...
40
- data['rsp'] # => data
35
+ puts data['photos'].first.url
41
36
  ```
37
+
38
+ ## Copyright
39
+
40
+ Copyright (c) 2014, Tim Morgan
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/flickr_party.rb CHANGED
@@ -2,82 +2,85 @@ require 'rubygems'
2
2
  require 'httparty'
3
3
  require 'digest/md5'
4
4
 
5
+ require_relative 'flickr_party/methods'
6
+ require_relative 'flickr_party/photo'
7
+ require_relative 'flickr_party/parser'
8
+
5
9
  class FlickrParty
6
10
 
7
11
  ENDPOINT = 'http://api.flickr.com/services/rest/'
8
-
9
- class PhotoURLError < RuntimeError; end
10
-
12
+
13
+ class UnknownMethodNameError < StandardError; end
14
+ class PhotoURLError < StandardError; end
15
+
11
16
  include HTTParty
12
17
  format :xml
13
-
18
+ parser FlickrParser
19
+
14
20
  attr_accessor :token
15
-
16
- THIRD_LEVEL_METHODS = %w(add browse search delete create find echo login null)
17
-
21
+
18
22
  def initialize(api_key, secret, method=nil, token=nil)
19
23
  @api_key = api_key
20
24
  @secret = secret
21
25
  @method = method
22
26
  @token = token
23
27
  end
24
-
25
- def method_missing(method_name, args={}, test=nil)
26
- if @method.to_s.count('.') == 2 or method_name.to_s =~ /[A-Z]/ or THIRD_LEVEL_METHODS.include?(method_name.to_s)
27
- args = self.class.stringify_hash_keys(args)
28
- args.merge!('api_key' => @api_key, 'method' => @method + '.' + method_name.to_s)
29
- if @token
30
- args.merge!('auth_token' => @token)
31
- end
32
- args_to_s = ""
33
- args.sort.each{|a| args_to_s += a[0].to_s + a[1].to_s }
34
- sig = Digest::MD5.hexdigest(@secret.to_s + args_to_s)
35
- args.merge!(:api_sig => sig)
36
- self.class.post(ENDPOINT, :body => args)
28
+
29
+ def method_missing(method_part, args={}, test=nil)
30
+ full_method_name = concat_method(method_part)
31
+ if is_full_method?(full_method_name)
32
+ args = merge_args(args, full_method_name)
33
+ post(args)
37
34
  else
38
- if @method
39
- method = @method + '.' + method_name.to_s
40
- else
41
- method = method_name.to_s
42
- end
43
- self.class.new(@api_key, @secret, method, @token)
35
+ # not an API call -- return a chainable object
36
+ self.class.new(@api_key, @secret, full_method_name, @token)
44
37
  end
45
38
  end
46
-
39
+
47
40
  def auth_url(perms='read', extra = nil)
48
- @frob = self.flickr.auth.getFrob['rsp']['frob']
41
+ @frob = self.flickr.auth.getFrob['frob']
49
42
  sig = Digest::MD5.hexdigest("#{@secret}api_key#{@api_key}extra#{extra}frob#{@frob}perms#{perms}")
50
43
  "http://flickr.com/services/auth/?api_key=#{@api_key}&perms=#{perms}&frob=#{@frob}&api_sig=#{sig}&extra=#{extra}"
51
44
  end
52
-
53
- def complete_auth(frob=nil)
54
- @frob = frob if frob
55
- response = self.flickr.auth.getToken('frob' => @frob)['rsp']
56
- if response['stat'] == 'fail'
57
- raise response['err']['msg']
58
- else
59
- @auth = response['auth']
60
- @token = @auth['token']
45
+
46
+ def complete_auth(frob='put_your_frob_here')
47
+ @frob ||= frob
48
+ @auth = self.flickr.auth.getToken('frob' => @frob)['auth']
49
+ @token = @auth['token']
50
+ end
51
+
52
+ private
53
+
54
+ def post(args)
55
+ self.class.post(ENDPOINT, :body => args).tap do |response|
56
+ if Array === response['photos']
57
+ response['photos'].map! do |photo_hash|
58
+ Photo.new(photo_hash)
59
+ end
60
+ elsif Hash === response['photos']
61
+ Photo.new(response['photos'])
62
+ end
61
63
  end
62
64
  end
63
65
 
64
- def photo_url(photo_hash, size=nil)
65
- if %w(m s t b).include?(size)
66
- "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['secret']}_#{size}.jpg"
67
- elsif size == 'o' and photo_hash['originalsecret'] and photo_hash['originalformat']
68
- "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['originalsecret']}_#{size}.#{photo_hash['originalformat']}"
69
- elsif size.nil? or size == '-'
70
- "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['secret']}.jpg"
71
- else
72
- raise PhotoURLError, "Invalid size or missing keys in photo_hash. Valid sizes are m, s, t, b, o, and nil. For original (o) size, photo_hash must contain both 'originalsecret' and 'originalformat'."
66
+ def merge_args(arguments, method_name)
67
+ self.class.stringify_hash_keys(arguments).tap do |args|
68
+ args.merge!('api_key' => @api_key, 'method' => method_name.to_s)
69
+ args.merge!('auth_token' => @token) if @token
70
+ args.merge!('api_sig' => arg_signature(args))
73
71
  end
74
72
  end
75
-
73
+
74
+ def arg_signature(args)
75
+ args_to_s = args.sort.inject('') { |s, (k, v)| s += k.to_s + v.to_s }
76
+ Digest::MD5.hexdigest(@secret.to_s + args_to_s)
77
+ end
78
+
76
79
  def self.stringify_hash_keys(hash)
77
80
  hash.inject({}) do |options, (key, value)|
78
81
  options[key.to_s] = value
79
82
  options
80
83
  end
81
84
  end
82
-
85
+
83
86
  end
metadata CHANGED
@@ -1,30 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flickr_party
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.3.1
4
+ version: 0.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tim Morgan
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-11 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- prerelease: false
16
- type: :runtime
17
14
  name: httparty
18
- version_requirements: !ruby/object:Gem::Requirement
19
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
20
16
  requirements:
21
- - - ! '>='
17
+ - - ">="
22
18
  - !ruby/object:Gem::Version
23
19
  version: '0'
24
- requirement: !ruby/object:Gem::Requirement
25
- none: false
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  description:
@@ -37,26 +34,25 @@ files:
37
34
  - lib/flickr_party.rb
38
35
  homepage: http://github.com/seven1m/flickr_party
39
36
  licenses: []
37
+ metadata: {}
40
38
  post_install_message:
41
39
  rdoc_options: []
42
40
  require_paths:
43
41
  - lib
44
42
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
43
  requirements:
47
- - - ! '>='
44
+ - - ">="
48
45
  - !ruby/object:Gem::Version
49
46
  version: '0'
50
47
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
48
  requirements:
53
- - - ! '>='
49
+ - - ">="
54
50
  - !ruby/object:Gem::Version
55
51
  version: '0'
56
52
  requirements: []
57
53
  rubyforge_project:
58
- rubygems_version: 1.8.25
54
+ rubygems_version: 2.2.2
59
55
  signing_key:
60
- specification_version: 3
56
+ specification_version: 4
61
57
  summary: Lightweight wrapper for Flickr API using HTTParty
62
58
  test_files: []