little 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/little/attempt.rb +13 -0
- data/lib/little/configuration.rb +28 -0
- data/lib/little/error.rb +9 -0
- data/lib/little/like.rb +31 -0
- data/lib/little/notification.rb +13 -0
- data/lib/little/sender.rb +65 -0
- data/lib/little/tag.rb +51 -0
- data/lib/little/version.rb +3 -0
- data/lib/little.rb +38 -0
- metadata +77 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
module Little
|
2
|
+
class Attempt
|
3
|
+
def self.add(user, ip, ok)
|
4
|
+
Little.post(:attempts, {:user => user, :ip => ip, :ok => ok}, [:user, :ip, :ok])
|
5
|
+
end
|
6
|
+
def self.get(user, count = 1)
|
7
|
+
Little.get(:attempts, {:user => user, :count => count}, [:user])
|
8
|
+
end
|
9
|
+
def self.sign_get(user)
|
10
|
+
Little.sign(:attempts, {:user => user})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Little
|
2
|
+
class Configuration
|
3
|
+
OPTIONS = [
|
4
|
+
:api_key, :api_secret,
|
5
|
+
:host, :port, :secure,
|
6
|
+
:http_open_timeout, :http_read_timeout,
|
7
|
+
:proxy_host, :proxy_pass, :proxy_port, :proxy_user].freeze
|
8
|
+
|
9
|
+
attr_accessor :api_key, :api_secret
|
10
|
+
attr_accessor :host, :secure
|
11
|
+
attr_accessor :http_open_timeout, :http_read_timeout
|
12
|
+
attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_pass
|
13
|
+
|
14
|
+
def port=(value)
|
15
|
+
@port = value
|
16
|
+
end
|
17
|
+
def port
|
18
|
+
defined?(@port) ? @port : (secure ? 443 : 80)
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@secure = true
|
23
|
+
@host = 'api.little.io'
|
24
|
+
@http_open_timeout = 5
|
25
|
+
@http_read_timeout = 5
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/little/error.rb
ADDED
data/lib/little/like.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Little
|
2
|
+
class Like
|
3
|
+
def self.add(user, asset, type)
|
4
|
+
Little.post(:likes, {:user => user, :asset => asset, :type => type}, [:user])
|
5
|
+
end
|
6
|
+
def self.sign_add(user)
|
7
|
+
Little.sign(:likes, {:user => user})
|
8
|
+
end
|
9
|
+
def self.user_likes_asset(user, asset, type)
|
10
|
+
Little.get(:likes, {:user => user, :asset => asset, :type => type})
|
11
|
+
end
|
12
|
+
def self.user_likes(user, page, records)
|
13
|
+
Little.get(:likes, {:user => user, :page => page, :records => records})
|
14
|
+
end
|
15
|
+
def self.user_like_count(user)
|
16
|
+
Little.get(:likes, {:user => user, :count => true})
|
17
|
+
end
|
18
|
+
def self.asset_liked_by(asset, type, page, records)
|
19
|
+
Little.get(:likes, {:asset => asset, :type => type, :page => page, :records => records})
|
20
|
+
end
|
21
|
+
def self.asset_like_count(asset, type)
|
22
|
+
Little.get(:likes, {:asset => asset, :type => type, :count => true})
|
23
|
+
end
|
24
|
+
def self.by_type(type, page, records)
|
25
|
+
Little.get(:likes, {:type => type, :page => page, :records => records})
|
26
|
+
end
|
27
|
+
def self.by_type_count(type)
|
28
|
+
Little.get(:likes, {:type => type, :count => true})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Little
|
2
|
+
class Notification
|
3
|
+
def self.get(user, type)
|
4
|
+
Little.get(:notifications, {:user => user, :type => type})
|
5
|
+
end
|
6
|
+
def self.respond(user, notification_id, response)
|
7
|
+
Little.post(:notifications, {:user => user, :notification => notification_id, :response => response}, [:user, :notification])
|
8
|
+
end
|
9
|
+
def self.sign_respond(user, notification_id)
|
10
|
+
Little.sign(:notification, {:user => user, :notification => notification_id})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
|
5
|
+
module Little
|
6
|
+
class Sender
|
7
|
+
BASE_URL = '/api/v1/'
|
8
|
+
HEADERS = {'Accept' => 'application/json'}
|
9
|
+
|
10
|
+
def initialize(configuration)
|
11
|
+
@configuration = configuration
|
12
|
+
end
|
13
|
+
|
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?
|
23
|
+
end
|
24
|
+
|
25
|
+
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?
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def create_http(resource, data, signature_keys)
|
38
|
+
http = Net::HTTP::Proxy(@configuration.proxy_host, @configuration.proxy_port, @configuration.proxy_user, @configuration.proxy_pass).new(@configuration.host, @configuration.port)
|
39
|
+
http.read_timeout = @configuration.http_read_timeout
|
40
|
+
http.open_timeout = @configuration.http_open_timeout
|
41
|
+
http.use_ssl = @configuration.secure
|
42
|
+
complete_data(resource, data, signature_keys)
|
43
|
+
http
|
44
|
+
end
|
45
|
+
def complete_data(resource, data, signature_keys)
|
46
|
+
data[:key] = @configuration.api_key
|
47
|
+
return if signature_keys.nil?
|
48
|
+
data[:sig] = Little.sign(resource, data.select{|k,v| signature_keys.include?(k)})
|
49
|
+
end
|
50
|
+
def url_encode(data)
|
51
|
+
URI.encode_www_form(data)
|
52
|
+
end
|
53
|
+
def handle_response(response)
|
54
|
+
if response['Content-Length'] == '0'
|
55
|
+
data = nil
|
56
|
+
elsif response['Content-Type'] =~ /application\/json/
|
57
|
+
data = JSON.parse(response.body)
|
58
|
+
else
|
59
|
+
response.body
|
60
|
+
end
|
61
|
+
return data if response.is_a?(Net::HTTPSuccess)
|
62
|
+
raise Little::Error.new(response.code, data)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/little/tag.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Little
|
2
|
+
class Tag
|
3
|
+
def self.add(user, asset, type, share, data = nil)
|
4
|
+
d = {:user => user, :asset => asset, :share => share, :type => type}
|
5
|
+
d[:data] = data if data
|
6
|
+
Little.post(:tags, d, [:user])
|
7
|
+
end
|
8
|
+
def self.sign_add(user)
|
9
|
+
Little.sign(:tags, {:user => user})
|
10
|
+
end
|
11
|
+
def self.get_by_id(id, shared_only = true)
|
12
|
+
Little.get(:tags, {:id => id}, shared_only ? nil : [:id])
|
13
|
+
end
|
14
|
+
def self.user_tags(user, page, records, shared_only = true, asset = nil, type = 0)
|
15
|
+
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))
|
21
|
+
end
|
22
|
+
def self.user_tag_count(user, shared_only = true, asset = nil, type = 0)
|
23
|
+
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))
|
29
|
+
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})
|
32
|
+
end
|
33
|
+
def self.asset_tags(asset, type, page, records)
|
34
|
+
Little.get(:tags, {:asset => asset, :type => type, :page => page, :records => records})
|
35
|
+
end
|
36
|
+
def self.asset_tag_count(asset, type)
|
37
|
+
Little.get(:tags, {:asset => asset, :type => type, :count => true})
|
38
|
+
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
|
+
end
|
51
|
+
end
|
data/lib/little.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'little/version'
|
2
|
+
require 'little/error'
|
3
|
+
require 'little/configuration'
|
4
|
+
require 'little/like'
|
5
|
+
require 'little/tag'
|
6
|
+
require 'little/attempt'
|
7
|
+
require 'little/notification'
|
8
|
+
require 'little/sender'
|
9
|
+
|
10
|
+
module Little
|
11
|
+
class << self
|
12
|
+
attr_accessor :sender
|
13
|
+
attr_writer :configuration
|
14
|
+
|
15
|
+
def configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(configuration)
|
21
|
+
self.sender = Sender.new(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def post(resource, data, signature_keys)
|
25
|
+
sender.post_request(resource, data, signature_keys)
|
26
|
+
end
|
27
|
+
def get(resource, data, signature_keys = nil)
|
28
|
+
sender.get_request(resource, data, signature_keys)
|
29
|
+
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
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: little
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Karl Seguin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: addressable
|
16
|
+
requirement: &70236902599800 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.2.6
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70236902599800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &70236902598900 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70236902598900
|
36
|
+
description:
|
37
|
+
email:
|
38
|
+
- help@little.io
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/little/attempt.rb
|
44
|
+
- lib/little/configuration.rb
|
45
|
+
- lib/little/error.rb
|
46
|
+
- lib/little/like.rb
|
47
|
+
- lib/little/notification.rb
|
48
|
+
- lib/little/sender.rb
|
49
|
+
- lib/little/tag.rb
|
50
|
+
- lib/little/version.rb
|
51
|
+
- lib/little.rb
|
52
|
+
homepage: http://little.io
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.10
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A ruby client for little.io services
|
76
|
+
test_files: []
|
77
|
+
has_rdoc:
|