ideaoforder-fleakr 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/.gitignore +3 -0
  2. data/README.rdoc +381 -0
  3. data/Rakefile +73 -0
  4. data/TODO +15 -0
  5. data/VERSION +1 -0
  6. data/fleakr.gemspec +157 -0
  7. data/lib/fleakr.rb +185 -0
  8. data/lib/fleakr/api.rb +8 -0
  9. data/lib/fleakr/api/file_parameter.rb +47 -0
  10. data/lib/fleakr/api/method_request.rb +66 -0
  11. data/lib/fleakr/api/option.rb +175 -0
  12. data/lib/fleakr/api/parameter.rb +35 -0
  13. data/lib/fleakr/api/parameter_list.rb +97 -0
  14. data/lib/fleakr/api/response.rb +35 -0
  15. data/lib/fleakr/api/upload_request.rb +75 -0
  16. data/lib/fleakr/api/value_parameter.rb +36 -0
  17. data/lib/fleakr/core_ext.rb +3 -0
  18. data/lib/fleakr/core_ext/false_class.rb +7 -0
  19. data/lib/fleakr/core_ext/hash.rb +22 -0
  20. data/lib/fleakr/core_ext/true_class.rb +7 -0
  21. data/lib/fleakr/objects.rb +12 -0
  22. data/lib/fleakr/objects/authentication_token.rb +60 -0
  23. data/lib/fleakr/objects/collection.rb +50 -0
  24. data/lib/fleakr/objects/comment.rb +49 -0
  25. data/lib/fleakr/objects/contact.rb +39 -0
  26. data/lib/fleakr/objects/error.rb +22 -0
  27. data/lib/fleakr/objects/group.rb +36 -0
  28. data/lib/fleakr/objects/image.rb +50 -0
  29. data/lib/fleakr/objects/photo.rb +149 -0
  30. data/lib/fleakr/objects/photo_context.rb +49 -0
  31. data/lib/fleakr/objects/search.rb +42 -0
  32. data/lib/fleakr/objects/set.rb +54 -0
  33. data/lib/fleakr/objects/tag.rb +56 -0
  34. data/lib/fleakr/objects/user.rb +103 -0
  35. data/lib/fleakr/support.rb +2 -0
  36. data/lib/fleakr/support/attribute.rb +46 -0
  37. data/lib/fleakr/support/object.rb +111 -0
  38. data/test/fixtures/auth.checkToken.xml +8 -0
  39. data/test/fixtures/auth.getFullToken.xml +8 -0
  40. data/test/fixtures/auth.getToken.xml +8 -0
  41. data/test/fixtures/contacts.getList.xml +7 -0
  42. data/test/fixtures/contacts.getPublicList.xml +7 -0
  43. data/test/fixtures/groups.pools.getPhotos.xml +7 -0
  44. data/test/fixtures/people.findByEmail.xml +6 -0
  45. data/test/fixtures/people.findByUsername.xml +6 -0
  46. data/test/fixtures/people.getInfo.xml +18 -0
  47. data/test/fixtures/people.getPublicGroups.xml +7 -0
  48. data/test/fixtures/people.getPublicPhotos.xml +7 -0
  49. data/test/fixtures/photos.comments.getList.xml +7 -0
  50. data/test/fixtures/photos.getContext.xml +6 -0
  51. data/test/fixtures/photos.getInfo.xml +20 -0
  52. data/test/fixtures/photos.getSizes.xml +10 -0
  53. data/test/fixtures/photos.search.xml +7 -0
  54. data/test/fixtures/photosets.comments.getList.xml +7 -0
  55. data/test/fixtures/photosets.getList.xml +13 -0
  56. data/test/fixtures/photosets.getPhotos.xml +7 -0
  57. data/test/fixtures/tags.getListPhoto.xml +9 -0
  58. data/test/fixtures/tags.getListUser.xml +10 -0
  59. data/test/fixtures/tags.getRelated.xml +9 -0
  60. data/test/test_helper.rb +141 -0
  61. data/test/unit/fleakr/api/file_parameter_test.rb +63 -0
  62. data/test/unit/fleakr/api/method_request_test.rb +94 -0
  63. data/test/unit/fleakr/api/option_test.rb +179 -0
  64. data/test/unit/fleakr/api/parameter_list_test.rb +176 -0
  65. data/test/unit/fleakr/api/parameter_test.rb +34 -0
  66. data/test/unit/fleakr/api/response_test.rb +49 -0
  67. data/test/unit/fleakr/api/upload_request_test.rb +149 -0
  68. data/test/unit/fleakr/api/value_parameter_test.rb +41 -0
  69. data/test/unit/fleakr/core_ext/false_class_test.rb +13 -0
  70. data/test/unit/fleakr/core_ext/hash_test.rb +32 -0
  71. data/test/unit/fleakr/core_ext/true_class_test.rb +13 -0
  72. data/test/unit/fleakr/objects/authentication_token_test.rb +61 -0
  73. data/test/unit/fleakr/objects/comment_test.rb +66 -0
  74. data/test/unit/fleakr/objects/contact_test.rb +84 -0
  75. data/test/unit/fleakr/objects/error_test.rb +21 -0
  76. data/test/unit/fleakr/objects/group_test.rb +46 -0
  77. data/test/unit/fleakr/objects/image_test.rb +81 -0
  78. data/test/unit/fleakr/objects/photo_context_test.rb +80 -0
  79. data/test/unit/fleakr/objects/photo_test.rb +247 -0
  80. data/test/unit/fleakr/objects/search_test.rb +94 -0
  81. data/test/unit/fleakr/objects/set_test.rb +82 -0
  82. data/test/unit/fleakr/objects/tag_test.rb +98 -0
  83. data/test/unit/fleakr/objects/user_test.rb +91 -0
  84. data/test/unit/fleakr/support/attribute_test.rb +126 -0
  85. data/test/unit/fleakr/support/object_test.rb +129 -0
  86. data/test/unit/fleakr_test.rb +186 -0
  87. metadata +195 -0
@@ -0,0 +1,175 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = Option
5
+ #
6
+ # Top-level class for creating specific option instances based on type
7
+ #
8
+ class Option
9
+
10
+ MAPPING = {
11
+ :tags => 'TagOption',
12
+ :viewable_by => 'ViewOption',
13
+ :level => 'LevelOption',
14
+ :type => 'TypeOption',
15
+ :hide? => 'HiddenOption'
16
+ }
17
+
18
+ # Initialize a new option for the specified type and value
19
+ #
20
+ def self.for(type, value)
21
+ class_for(type).new(type, value)
22
+ end
23
+
24
+ def self.class_for(type) # :nodoc:
25
+ class_name = MAPPING[type] || 'SimpleOption'
26
+ "Fleakr::Api::#{class_name}".constantize
27
+ end
28
+
29
+ end
30
+
31
+ # = SimpleOption
32
+ #
33
+ # Simple name / value option pair
34
+ #
35
+ class SimpleOption
36
+
37
+ attr_reader :type, :value
38
+
39
+ # Create an option of the specified type and value
40
+ #
41
+ def initialize(type, value)
42
+ @type = type
43
+ @value = value
44
+ end
45
+
46
+ # Generate hash representation of this option
47
+ #
48
+ def to_hash
49
+ {type => value}
50
+ end
51
+
52
+ end
53
+
54
+ # = TagOption
55
+ #
56
+ # Represents values for tags
57
+ #
58
+ class TagOption < SimpleOption
59
+
60
+ # Tag with specified values. Value passed will be converted to an array if it isn't
61
+ # already
62
+ #
63
+ def initialize(type, value)
64
+ super type, value
65
+ @value = Array(self.value)
66
+ end
67
+
68
+ # Hash representation of tag values (separated by spaces). Handles multi-word tags
69
+ # by enclosing each tag in double quotes.
70
+ #
71
+ def to_hash
72
+ tags = value.map {|tag| "\"#{tag}\"" }
73
+ {type => tags.join(' ')}
74
+ end
75
+
76
+ end
77
+
78
+ # = ViewOption
79
+ #
80
+ # Specify who is able to view the photo.
81
+ #
82
+ class ViewOption < SimpleOption
83
+
84
+ # Specify who this is viewable by (e.g. everyone / friends / family).
85
+ #
86
+ def initialize(type, value)
87
+ super type, Array(value)
88
+ end
89
+
90
+ # Is this publicly viewable? (i.e. :everyone)
91
+ #
92
+ def public?
93
+ value == [:everyone]
94
+ end
95
+
96
+ # Is this viewable by friends? (i.e. :friends)
97
+ #
98
+ def friends?
99
+ value.include?(:friends)
100
+ end
101
+
102
+ # Is this viewable by family? (i.e. :family)
103
+ #
104
+ def family?
105
+ value.include?(:family)
106
+ end
107
+
108
+ # Hash representation of photo permissions
109
+ #
110
+ def to_hash
111
+ {:is_public => public?.to_i, :is_friend => friends?.to_i, :is_family => family?.to_i}
112
+ end
113
+
114
+ end
115
+
116
+ # = LevelOption
117
+ #
118
+ # Specify the "safety level" of this photo (e.g. safe / moderate / restricted)
119
+ #
120
+ class LevelOption < SimpleOption
121
+
122
+ def value # :nodoc:
123
+ case @value
124
+ when :safe then 1
125
+ when :moderate then 2
126
+ when :restricted then 3
127
+ end
128
+ end
129
+
130
+ # Hash representation of the safety_level for this photo
131
+ #
132
+ def to_hash
133
+ {:safety_level => value}
134
+ end
135
+
136
+ end
137
+
138
+ # = TypeOption
139
+ #
140
+ # Specify the type of this photo (e.g. photo / screenshot / other)
141
+ #
142
+ class TypeOption < SimpleOption
143
+
144
+ def value # :nodoc:
145
+ case @value
146
+ when :photo then 1
147
+ when :screenshot then 2
148
+ when :other then 3
149
+ end
150
+ end
151
+
152
+ # Hash representation of this type
153
+ #
154
+ def to_hash
155
+ {:content_type => value}
156
+ end
157
+
158
+ end
159
+
160
+ # = HiddenOption
161
+ #
162
+ # Specify whether this photo should be hidden from search
163
+ #
164
+ class HiddenOption < SimpleOption
165
+
166
+ # Hash representation of whether to hide this photo
167
+ #
168
+ def to_hash
169
+ {:hidden => (value ? 2 : 1)}
170
+ end
171
+
172
+ end
173
+
174
+ end
175
+ end
@@ -0,0 +1,35 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = Parameter
5
+ #
6
+ # Base class for other parameters that get passed to the Flickr API - see
7
+ # #FileParameter and #ValueParameter for examples
8
+ #
9
+ class Parameter
10
+
11
+ attr_reader :name
12
+
13
+ # A new named parameter (never used directly)
14
+ #
15
+ def initialize(name, include_in_signature = true)
16
+ @name = name
17
+ @include_in_signature = include_in_signature
18
+ end
19
+
20
+ # Should this parameter be used when generating the signature?
21
+ #
22
+ def include_in_signature?
23
+ (@include_in_signature == true) ? true : false
24
+ end
25
+
26
+ # Used for sorting when generating a signature
27
+ #
28
+ def <=>(other)
29
+ self.name <=> other.name
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,97 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = ParameterList
5
+ #
6
+ # Represents a list of parameters that get passed as part of a
7
+ # MethodRequest or UploadRequest. These can be transformed as necessary
8
+ # into query strings (using #to_query) or form data (using #to_form)
9
+ #
10
+ class ParameterList
11
+
12
+ # Create a new parameter list with optional parameters:
13
+ # [:authenticate?] Request will automatically be authenticated if Fleakr.token is available
14
+ # set this to false to force it not to authenticate
15
+ #
16
+ # Any additional name / value pairs will be created as individual
17
+ # ValueParameters as part of the list. Example:
18
+ #
19
+ # >> list = Fleakr::Api::ParameterList.new(:foo => 'bar')
20
+ # => #<Fleakr::Api::ParameterList:0x1656e6c @list=... >
21
+ # >> list[:foo]
22
+ # => #<Fleakr::Api::ValueParameter:0x1656da4 @include_in_signature=true, @name="foo", @value="bar">
23
+ #
24
+ def initialize(options = {})
25
+ # TODO: need to find a way to move the unexpected behavior in Fleakr.token elsewhere
26
+ @api_options = options.extract!(:authenticate?)
27
+
28
+ @list = Hash.new
29
+
30
+ options.each {|k,v| self << ValueParameter.new(k.to_s, v) }
31
+
32
+ self << ValueParameter.new('api_key', Fleakr.api_key)
33
+ self << ValueParameter.new('auth_token', Fleakr.token.value) if authenticate?
34
+ end
35
+
36
+ # Add a new parameter (ValueParameter / FileParameter) to the list
37
+ #
38
+ def <<(parameter)
39
+ @list.merge!(parameter.name => parameter)
40
+ end
41
+
42
+ # Should this parameter list be signed?
43
+ #
44
+ def sign?
45
+ !Fleakr.shared_secret.blank?
46
+ end
47
+
48
+ # Should we send the auth_token with the request?
49
+ #
50
+ def authenticate?
51
+ @api_options.has_key?(:authenticate?) ? @api_options[:authenticate?] : !Fleakr.token.blank?
52
+ end
53
+
54
+ # Access an individual parameter by key (symbol or string)
55
+ #
56
+ def [](key)
57
+ list[key.to_s]
58
+ end
59
+
60
+ def boundary # :nodoc:
61
+ @boundary ||= Digest::MD5.hexdigest(rand.to_s)
62
+ end
63
+
64
+ # Generate the query string representation of this parameter
65
+ # list - e.g. <tt>foo=bar&blee=baz</tt>
66
+ #
67
+ def to_query
68
+ list.values.map {|element| element.to_query }.join('&')
69
+ end
70
+
71
+ # Generate the form representation of this parameter list including the
72
+ # boundary
73
+ #
74
+ def to_form
75
+ form = list.values.map {|p| "--#{self.boundary}\r\n#{p.to_form}" }.join
76
+ form << "--#{self.boundary}--"
77
+
78
+ form
79
+ end
80
+
81
+ def signature # :nodoc:
82
+ parameters_to_sign = @list.values.reject {|p| !p.include_in_signature? }
83
+ signature_text = parameters_to_sign.sort.map {|p| "#{p.name}#{p.value}" }.join
84
+
85
+ Digest::MD5.hexdigest("#{Fleakr.shared_secret}#{signature_text}")
86
+ end
87
+
88
+ private
89
+ def list
90
+ list = @list
91
+ list.merge!('api_sig' => ValueParameter.new('api_sig', signature, false)) if self.sign?
92
+
93
+ list
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,35 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = Response
5
+ #
6
+ # Response objects contain Hpricot documents that are traversed and parsed by
7
+ # the model objects. This class is never called directly but is instantiated
8
+ # during the request cycle (see: Fleakr::Api::MethodRequest.with_response!)
9
+ #
10
+ class Response
11
+
12
+ # Creates a new response from a raw XML string returned from a Request
13
+ def initialize(response_xml)
14
+ @response_xml = response_xml
15
+ end
16
+
17
+ # Return a document-based representation of the XML contained in the
18
+ # API response. This is an Hpricot document object
19
+ def body
20
+ @body ||= Hpricot.XML(@response_xml)
21
+ end
22
+
23
+ # Did the response from the API contain errors?
24
+ def error?
25
+ (self.body/'rsp').attr('stat') != 'ok'
26
+ end
27
+
28
+ # Access the API error if one exists
29
+ def error
30
+ Fleakr::Objects::Error.new(self.body) if self.error?
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,75 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = UploadRequest
5
+ #
6
+ # This implements the upload functionality of the Flickr API which is needed
7
+ # to create new photos and replace the photo content of existing photos
8
+ #
9
+ class UploadRequest
10
+
11
+ ENDPOINT_URIS = {
12
+ :create => 'http://api.flickr.com/services/upload/',
13
+ :update => 'http://api.flickr.com/services/replace/'
14
+ }
15
+
16
+ attr_reader :parameters, :type
17
+
18
+ # Send a request and return a Response object. If an API error occurs, this raises
19
+ # a Fleakr::ApiError with the reason for the error. See UploadRequest#new for more
20
+ # details.
21
+ #
22
+ def self.with_response!(filename, type = :create, options = {})
23
+ request = self.new(filename, type, options)
24
+ response = request.send
25
+
26
+ raise(Fleakr::ApiError, "Code: #{response.error.code} - #{response.error.message}") if response.error?
27
+
28
+ response
29
+ end
30
+
31
+ # Create a new UploadRequest with the specified filename, type, and options. Type
32
+ # is one of <tt>:create</tt> or <tt>:update</tt> to specify whether we are saving a new
33
+ # image or replacing an existing one.
34
+ #
35
+ # For a list of available options, see the documentation in Fleakr::Objects::Photo
36
+ #
37
+ def initialize(filename, type = :create, options = {})
38
+ @type = type
39
+ @options = options
40
+
41
+ @parameters = ParameterList.new(upload_options)
42
+ @parameters << FileParameter.new('photo', filename)
43
+ end
44
+
45
+ # A list of upload options for this upload request (see Fleakr::Api::Option)
46
+ #
47
+ def upload_options
48
+ option_list = @options.map {|key, value| Option.for(key, value) }
49
+ option_list.inject({}) {|hash, option| hash.merge(option.to_hash)}
50
+ end
51
+
52
+ def headers # :nodoc:
53
+ {'Content-Type' => "multipart/form-data; boundary=#{self.parameters.boundary}"}
54
+ end
55
+
56
+ def send # :nodoc:
57
+ response = Net::HTTP.start(endpoint_uri.host, endpoint_uri.port) do |http|
58
+ logger.info("Sending upload request to: #{endpoint_uri}")
59
+ logger.debug("Request data:\n#{self.parameters.to_form}")
60
+ logger.debug("Request headers:\n#{self.headers.inspect}")
61
+
62
+ http.post(endpoint_uri.path, self.parameters.to_form, self.headers)
63
+ end
64
+ logger.debug("Response data:\n#{response.body}")
65
+ Response.new(response.body)
66
+ end
67
+
68
+ private
69
+ def endpoint_uri
70
+ @endpoint_uri ||= URI.parse(ENDPOINT_URIS[self.type])
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,36 @@
1
+ module Fleakr
2
+ module Api # :nodoc:
3
+
4
+ # = ValueParameter
5
+ #
6
+ # A simple name / value parameter for use in API calls
7
+ #
8
+ class ValueParameter < Parameter
9
+
10
+ attr_reader :value
11
+
12
+ # Create a new parameter with the specified name / value pair.
13
+ #
14
+ def initialize(name, value, include_in_signature = true)
15
+ @value = value
16
+ super(name, include_in_signature)
17
+ end
18
+
19
+ # Generate the query string representation of this parameter.
20
+ #
21
+ def to_query
22
+ "#{self.name}=#{CGI.escape(self.value.to_s)}"
23
+ end
24
+
25
+ # Generate the form representation of this parameter.
26
+ #
27
+ def to_form
28
+ "Content-Disposition: form-data; name=\"#{self.name}\"\r\n" +
29
+ "\r\n" +
30
+ "#{self.value}\r\n"
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end