little 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,28 @@
1
1
  module Little
2
+ # log and retrieve login attempts
2
3
  class Attempt
4
+ # adds a login attempt
5
+ # @param [String] user the user who attempted to login
6
+ # @param [String] ip the user's ip address (use request.remote_ip in rails)
7
+ # @param [bool] ok whether or not the login was successful
8
+ # @return [Hash] the number of failed attempts in the last 0.5, 1, 3 and 5 minutes
3
9
  def self.add(user, ip, ok)
4
10
  Little.post(:attempts, {:user => user, :ip => ip, :ok => ok}, [:user, :ip, :ok])
5
11
  end
12
+
13
+ # gets the previous (2nd last) successful login attempt
14
+ def self.get_previous_successful(user)
15
+ Little.get(:attempts, {:user => user}, [:user])
16
+ end
17
+
18
+ # gets the login attempts for theu ser
19
+ # @param [String] user the user who attempted to login
20
+ # @param [int] count the number of past attempts to retrieve
6
21
  def self.get(user, count = 1)
7
22
  Little.get(:attempts, {:user => user, :count => count}, [:user])
8
23
  end
24
+
25
+ # generates a signature for getting attempts(useful when using the javascript library)
9
26
  def self.sign_get(user)
10
27
  Little.sign(:attempts, {:user => user})
11
28
  end
data/lib/little/like.rb CHANGED
@@ -1,29 +1,64 @@
1
1
  module Little
2
+ # add, delete and retrieve likes by user or assets
2
3
  class Like
4
+ # adds a like for a user to an asset
5
+ # @param [String] user the user who likes the asset
6
+ # @param [String] asset the asset being liked
7
+ # @param [int] type the type of asset
3
8
  def self.add(user, asset, type)
4
9
  Little.post(:likes, {:user => user, :asset => asset, :type => type}, [:user])
5
10
  end
11
+
12
+ # generates a signature for adding a like (useful when using the javascript library)
6
13
  def self.sign_add(user)
7
14
  Little.sign(:likes, {:user => user})
8
15
  end
16
+
17
+ # removes a like for a user to an asset
18
+ # @param [String] user the user who no longer likes the asset
19
+ # @param [String] asset the asset being unliked
20
+ # @param [int] type the type of asset
21
+ def self.delete(user, asset, type)
22
+ Little.delete(:likes, {:user => user, :asset => asset, :type => type}, [:user, :asset, :type])
23
+ end
24
+
25
+ # generates a signature for deleting a like (useful when using the javascript library)
26
+ # @param [String] user the user who no longr likes the asset
27
+ def self.sign_delete(user, asset, type)
28
+ Little.sign(:likes, {:user => user, :asset => asset, :type => type})
29
+ end
30
+
31
+ # does the user like the asset
9
32
  def self.user_likes_asset(user, asset, type)
10
33
  Little.get(:likes, {:user => user, :asset => asset, :type => type})
11
34
  end
35
+
36
+ # gets all the assets liked by the user
12
37
  def self.user_likes(user, page, records)
13
38
  Little.get(:likes, {:user => user, :page => page, :records => records})
14
39
  end
40
+
41
+ # gets the number of assets the user likes
15
42
  def self.user_like_count(user)
16
43
  Little.get(:likes, {:user => user, :count => true})
17
44
  end
45
+
46
+ # gets all the users who like an asset
18
47
  def self.asset_liked_by(asset, type, page, records)
19
48
  Little.get(:likes, {:asset => asset, :type => type, :page => page, :records => records})
20
49
  end
50
+
51
+ # gets the number of users who like an asset
21
52
  def self.asset_like_count(asset, type)
22
53
  Little.get(:likes, {:asset => asset, :type => type, :count => true})
23
54
  end
55
+
56
+ # gets the assets liked by type, ordered by number of likes (desc)
24
57
  def self.by_type(type, page, records)
25
58
  Little.get(:likes, {:type => type, :page => page, :records => records})
26
59
  end
60
+
61
+ # gets the number of assets for a type
27
62
  def self.by_type_count(type)
28
63
  Little.get(:likes, {:type => type, :count => true})
29
64
  end
@@ -1,11 +1,23 @@
1
1
  module Little
2
+ # retrieve and repond to notifications
2
3
  class Notification
4
+ # gets the notification for a user and type
5
+ # @param [String] user the user to get the notification for
6
+ # @param [int] type the type of notifiation to get
7
+ # @return [Hash] containing the notification id and body, nil if the type is valid or if the user has already responded
3
8
  def self.get(user, type)
4
9
  Little.get(:notifications, {:user => user, :type => type})
5
10
  end
11
+
12
+ # responds to a particular notification
13
+ # @param [String] user the user responding to the notificaiton
14
+ # @param [String] notificaiton_id the id of the notificaiton
15
+ # @param [int] response the user's response
6
16
  def self.respond(user, notification_id, response)
7
17
  Little.post(:notifications, {:user => user, :notification => notification_id, :response => response}, [:user, :notification])
8
18
  end
19
+
20
+ # generates a signature for sending a response(useful when using the javascript library)
9
21
  def self.sign_respond(user, notification_id)
10
22
  Little.sign(:notification, {:user => user, :notification => notification_id})
11
23
  end
data/lib/little/sender.rb CHANGED
@@ -12,25 +12,19 @@ module Little
12
12
  end
13
13
 
14
14
  def get_request(resource, data, signature_keys)
15
- response = nil
16
- begin
17
- http = create_http(resource, data, signature_keys)
18
- response = http.get("#{BASE_URL}#{resource}?#{url_encode(data)}", HEADERS)
19
- rescue => e
20
- raise Little::Error.new(-1, 0)
21
- end
22
- handle_response(response) unless response.nil?
15
+ query_request(:get, resource, data, signature_keys)
16
+ end
17
+
18
+ def delete_request(resource, data, signature_keys)
19
+ query_request(:delete, resource, data, signature_keys)
23
20
  end
24
21
 
25
22
  def post_request(resource, data, signature_keys)
26
- response = nil
27
- begin
28
- http = create_http(resource, data, signature_keys)
29
- response = http.post(BASE_URL + resource.to_s, url_encode(data), HEADERS)
30
- rescue => e
31
- raise Little::Error.new(-2, e)
32
- end
33
- handle_response(response) unless response.nil?
23
+ form_request(:post, resource, data, signature_keys)
24
+ end
25
+
26
+ def put_request(resource, data, signature_keys)
27
+ form_request(:put, resource, data, signature_keys)
34
28
  end
35
29
 
36
30
  private
@@ -42,14 +36,39 @@ module Little
42
36
  complete_data(resource, data, signature_keys)
43
37
  http
44
38
  end
39
+
45
40
  def complete_data(resource, data, signature_keys)
46
41
  data[:key] = @configuration.api_key
47
42
  return if signature_keys.nil?
48
43
  data[:sig] = Little.sign(resource, data.select{|k,v| signature_keys.include?(k)})
49
44
  end
45
+
50
46
  def url_encode(data)
51
47
  URI.encode_www_form(data)
52
48
  end
49
+
50
+ def query_request(method, resource, data, signature_keys)
51
+ response = nil
52
+ begin
53
+ http = create_http(resource, data, signature_keys)
54
+ response = http.send(method, "#{BASE_URL}#{resource}?#{url_encode(data)}", HEADERS)
55
+ rescue => e
56
+ raise Little::Error.new(-1, e)
57
+ end
58
+ handle_response(response) unless response.nil?
59
+ end
60
+
61
+ def form_request(method, resource, data, signature_keys)
62
+ response = nil
63
+ begin
64
+ http = create_http(resource, data, signature_keys)
65
+ response = http.send(method, BASE_URL + resource.to_s, url_encode(data), HEADERS)
66
+ rescue => e
67
+ raise Little::Error.new(-2, e)
68
+ end
69
+ handle_response(response) unless response.nil?
70
+ end
71
+
53
72
  def handle_response(response)
54
73
  body = response.body
55
74
  if body.length < 2
data/lib/little/tag.rb CHANGED
@@ -1,51 +1,88 @@
1
1
  module Little
2
+ # add, delete and retrieve tags by user or assets
2
3
  class Tag
4
+ # adds a tag for a user to an asset
5
+ # @param [String] user the user who likes the asset
6
+ # @param [String] asset the asset being liked
7
+ # @param [int] type the type of asset
8
+ # @param [bool] share whether to share this tag or not
9
+ # @param [String] arbitrary data to save with the tag
3
10
  def self.add(user, asset, type, share, data = nil)
4
11
  d = {:user => user, :asset => asset, :share => share, :type => type}
5
12
  d[:data] = data if data
6
13
  Little.post(:tags, d, [:user])
7
14
  end
15
+
16
+ # generates a signature for adding a tag (useful when using the javascript library)
8
17
  def self.sign_add(user)
9
18
  Little.sign(:tags, {:user => user})
10
19
  end
20
+
21
+ # removes a tag for a user to an asset
22
+ # @param [String] id the tag's id
23
+ def self.delete(id)
24
+ Little.delete(:tags, {:id => id}, [:id])
25
+ end
26
+
27
+ # generates a signature for deleting a tag (useful when using the javascript library)
28
+ def self.sign_delete(id)
29
+ Little.sign(:tags, {:id => id})
30
+ end
31
+
32
+ # gets a tag by id
33
+ # @param [String] id the tag's id
34
+ # @param [bool] shared_only only get the tag if it's shared
11
35
  def self.get_by_id(id, shared_only = true)
12
36
  Little.get(:tags, {:id => id}, shared_only ? nil : [:id])
13
37
  end
14
- def self.user_tags(user, page, records, shared_only = true, asset = nil, type = 0)
38
+
39
+ # generates a signature for getting a tag by id (useful when using the javascript library)
40
+ def self.sign_get_by_id(id)
41
+ Little.sign(:tags, {:id => id})
42
+ end
43
+
44
+ # gets all the tags for a user
45
+ def self.user_tags(user, page, records, shared_only = true)
15
46
  data = {:user => user, :page => page, :records => records}
16
- if asset
17
- data[:asset] = asset
18
- data[:type] = type
19
- end
20
- Little.get(:tags, data, keys_to_sign_with(!asset.nil?, shared_only))
47
+ Little.get(:tags, data, shared_only ? nil : [:user])
48
+ end
49
+
50
+ # gets all the assets liked for a user for a specific asset
51
+ def self.user_tags_for_asset(user, asset, type, page, records, shared_only = true)
52
+ data = {:user => user, :asset => asset, :type => type, :page => page, :records => records}
53
+ Little.get(:tags, data, shared_only ? nil : [:user, :asset, :type])
21
54
  end
22
- def self.user_tag_count(user, shared_only = true, asset = nil, type = 0)
55
+
56
+ # gets the number of tags for a user
57
+ def self.user_tag_count(user, shared_only = true)
23
58
  data = {:user => user, :count => true}
24
- if asset
25
- data[:asset] = asset
26
- data[:type] = type
27
- end
28
- Little.get(:tags, data, keys_to_sign_with(!asset.nil?, shared_only))
59
+ Little.get(:tags, data, shared_only ? nil : [:user])
60
+ end
61
+
62
+ # gets all the of tags for a user for a specific asset
63
+ def self.user_tag_for_asset_count(user, asset, type, shared_only = true)
64
+ data = {:user => user, :asset => asset, :type => type, :count => true}
65
+ Little.get(:tags, data, shared_only ? nil : [:user, :asset, :type])
29
66
  end
30
- def self.sign_user_tags(user, asset = nil, type = nil)
31
- asset.nil? ? Little.sign(:tags, {:user => user}) : Little.sign(:tags, {:user => user, :asset => asset, :type => type})
67
+
68
+ # generates a signature for getting a tags for a user (useful when using the javascript library)
69
+ def self.sign_user_tags(user)
70
+ Little.sign(:tags, {:user => user})
32
71
  end
72
+
73
+ # generates a signature for getting a for a user for a specific asset (useful when using the javascript library)
74
+ def self.sign_user_tags_for_asset(user, asset, type)
75
+ Little.sign(:tags, {:user => user, :asset => asset, :type => type})
76
+ end
77
+
78
+ # gets all the shared tags for a specific asset
33
79
  def self.asset_tags(asset, type, page, records)
34
80
  Little.get(:tags, {:asset => asset, :type => type, :page => page, :records => records})
35
81
  end
82
+
83
+ # gets the number of shared tags for a specific asset
36
84
  def self.asset_tag_count(asset, type)
37
85
  Little.get(:tags, {:asset => asset, :type => type, :count => true})
38
86
  end
39
-
40
- private
41
- def self.keys_to_sign_with(has_asset, shared_only)
42
- return nil if shared_only
43
- keys = [:user]
44
- if has_asset
45
- keys << :asset
46
- keys << :type
47
- end
48
- keys
49
- end
50
87
  end
51
88
  end
@@ -1,3 +1,3 @@
1
1
  module Little
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
data/lib/little.rb CHANGED
@@ -7,32 +7,51 @@ require 'little/attempt'
7
7
  require 'little/notification'
8
8
  require 'little/sender'
9
9
 
10
- module Little
10
+ module Little #:nodoc
11
11
  class << self
12
12
  attr_accessor :sender
13
13
  attr_writer :configuration
14
14
 
15
- def configuration
16
- @configuration ||= Configuration.new
17
- end
18
-
15
+ # Sets the configuration options. Best used by passing a block, for example:
16
+ #
17
+ # Little.configure do |config|
18
+ # config.api_key = 'KEY'
19
+ # config.api_secret = 'SECRET'
20
+ # end
19
21
  def configure
20
22
  yield(configuration)
21
23
  self.sender = Sender.new(configuration)
22
24
  end
23
25
 
26
+ # Can be used to generate a signature. Best to use specific signature methods such as Little::Like.sign_add(user)
27
+ def sign(resource, params)
28
+ params[:key] = configuration.api_key
29
+ raw = params.sort{|a, b| a[0] <=> b[0]}.join('|') + '|' + configuration.api_secret + '|' + resource.to_s
30
+ Digest::SHA1.hexdigest(raw)
31
+ end
32
+
33
+ # Gets the current configuration
34
+ def configuration
35
+ @configuration ||= Configuration.new
36
+ end
37
+
38
+ # Issues a POST request to the little.io service
24
39
  def post(resource, data, signature_keys)
25
40
  sender.post_request(resource, data, signature_keys)
26
41
  end
42
+ # Issues a DELETE request to the little.io service
43
+ # @param [String, Symbol] resource name of the resource (:tags, :likes, ...)
44
+ # @param [Hash] data data to send to the service
45
+ # @param [Array] signature_keys keys of the data used for signing (optional)
46
+ def delete(resource, data, signature_keys)
47
+ sender.delete_request(resource, data, signature_keys)
48
+ end
49
+ # Issues a GET request to the little.io service
50
+ # @param [String, Symbol] resource name of the resource (:tags, :likes, ...)
51
+ # @param [Hash] data data to send to the service
52
+ # @param [Array] signature_keys keys of the data used for signing (optional)
27
53
  def get(resource, data, signature_keys = nil)
28
54
  sender.get_request(resource, data, signature_keys)
29
55
  end
30
-
31
- def sign(resource, params)
32
- params[:key] = configuration.api_key
33
- raw = params.sort{|a, b| a[0] <=> b[0]}.join('|') + '|' + configuration.api_secret + '|' + resource.to_s
34
- Digest::SHA1.hexdigest(raw)
35
- end
36
-
37
56
  end
38
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: little
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-29 00:00:00.000000000 Z
12
+ date: 2011-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
16
- requirement: &70285946445680 !ruby/object:Gem::Requirement
16
+ requirement: &70251397271620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.2.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70285946445680
24
+ version_requirements: *70251397271620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70285946444780 !ruby/object:Gem::Requirement
27
+ requirement: &70251397269580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70285946444780
35
+ version_requirements: *70251397269580
36
36
  description:
37
37
  email:
38
38
  - help@little.io