ulule 0.0.5 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aebe9db8e423042495679d2c3683d6bf47b89ffb
4
- data.tar.gz: 543f6099a86b8eb562c37a37175ed5259862a31d
3
+ metadata.gz: 8a764261a56105fbe3b0a534570a09cdca7f8a52
4
+ data.tar.gz: bd06b1c97fffe8bd6bfa9c6b3ddbcb6baf4923a8
5
5
  SHA512:
6
- metadata.gz: 673f065a2f402815c603235369244f5cdc420ef41b0d71b57d6c0ff8fde7f84875ec35d381121736f6c5ed6a5a43fc7a995036f74da3a9b2a94d7f10838dfd35
7
- data.tar.gz: c0de5f299bb3a79e1393320ef49ff64cca0ac4e3801ebf9c05872471e18c0e0d8995a5d8a344b755d34812526857d2d4a2b096b026422e5d311a102d4f12226e
6
+ metadata.gz: c8cc98cab6dd08de1c1414662b04ac5d530748bb10bccb7f9e7b900bdd46025a8df2257cfc8fa3c3c01d80af6cd87fd901c0748958ca1c2ffcc82c1fc08dcb92
7
+ data.tar.gz: 54468cde2c895c953657017135a61a1478edb149c979fec1e949907ecfa1af763ee54601117336868509f707d9eb3f21c044bbb2ee64d81b0b0811df9fafe802
@@ -1,33 +1,11 @@
1
1
  require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
2
4
  require 'json'
5
+ require 'ulule/error'
6
+ require 'ulule/authentication_error'
7
+ require 'ulule/client'
3
8
 
4
- class UluleError < Exception
5
- end
9
+ module Ulule
6
10
 
7
- class Ulule
8
- attr_accessor :username
9
- attr_accessor :apikey
10
-
11
- def initialize(username, apikey)
12
- @username, @apikey = username, apikey
13
- end
14
-
15
- def project url
16
- raise UluleError, "Wrong url" if (name = url.match(/.*\/(.*)\/$/)).nil?
17
- output = send_request name[1]
18
- raise UluleError "Wrong username/apikey or url" if output.empty?
19
- JSON.parse output
20
- end
21
-
22
- private
23
-
24
- def send_request name
25
- uri = URI "https://api.ulule.com/v1/projects/#{name}"
26
- Net::HTTP.start uri.host, uri.port, use_ssl: uri.scheme == 'https' do |http|
27
- request = Net::HTTP::Get.new uri
28
- request.add_field 'Authorization', "ApiKey #{@username}:#{@apikey}"
29
- response = http.request request
30
- response.body
31
- end
32
- end
33
11
  end
@@ -0,0 +1,5 @@
1
+ module Ulule
2
+
3
+ class AuthenticationError < Error ; end
4
+
5
+ end
@@ -0,0 +1,86 @@
1
+ module Ulule
2
+
3
+ class Client
4
+
5
+ attr_accessor :username, :key
6
+
7
+ def initialize(options = {})
8
+
9
+ @key = options.fetch(:key) { ENV.fetch('ULULE_API_KEY') }
10
+ @username = options.fetch(:username) { ENV.fetch('ULULE_USERNAME') }
11
+ @host = options.fetch(:host) { 'https://api.ulule.com/v1/' }
12
+ @projects = 'projects'
13
+ @comments = 'comments'
14
+ @supporters = 'supporters'
15
+ @news = 'news'
16
+ @users = 'users'
17
+
18
+ end
19
+
20
+ def me
21
+ get("#{@users}/#{@username}")
22
+ end
23
+
24
+ def my_projects(options = {})
25
+ get("#{@users}/#{@username}/#{@projects}", options)
26
+ end
27
+
28
+ def projects(user, options = {})
29
+ get("#{@users}/#{user}/#{@projects}", options)
30
+ end
31
+
32
+ def project slug
33
+ get("#{@projects}/#{slug}")
34
+ end
35
+
36
+ def project_comments(slug, options = {})
37
+ get("#{@projects}/#{slug}/#{@comments}", options)
38
+ end
39
+
40
+ def project_news slug
41
+ get("#{@projects}/#{slug}/#{@news}")
42
+ end
43
+
44
+ def news id
45
+ get("#{@news}/#{id}")
46
+ end
47
+
48
+ def news_comments(id, options = {})
49
+ get("#{@news}/#{id}/#{@comments}", options)
50
+ end
51
+
52
+ def supporters slug
53
+ get ("#{@projects}/#{slug}/#{@supporters}")
54
+ end
55
+
56
+ private
57
+
58
+ def get(path, params = {})
59
+ uri = URI "#{@host}#{path}#{build_params params}"
60
+ req = Net::HTTP::Get.new uri
61
+ req['Authorization'] = "ApiKey #{username}:#{key}"
62
+ parse Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
63
+ end
64
+
65
+ def parse http_response
66
+ case http_response
67
+ when Net::HTTPSuccess
68
+ JSON.parse http_response.body
69
+ when Net::HTTPUnauthorized
70
+ raise AuthenticationError
71
+ else
72
+ raise Error, "Unexpected HTTP response, code #{http_response.code}"
73
+ end
74
+ end
75
+
76
+ def build_params params
77
+ str = nil
78
+ params.each_key do |key|
79
+ str = "#{str}#{CGI::escape(key.to_s)}=#{CGI::escape(params[key])}&" unless params[key].empty?
80
+ end
81
+ str.nil? ? '' : "?#{str[0...-1]}"
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,5 @@
1
+ module Ulule
2
+
3
+ class Error < Exception ; end
4
+
5
+ end
metadata CHANGED
@@ -1,22 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ulule
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Puyet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple gem to fetch datas projects from Ulule's API
13
+ description: A simple Ruby wrapper for the Ulule API
14
14
  email: sebastienpuyet@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/ulule.rb
20
+ - lib/ulule/authentication_error.rb
21
+ - lib/ulule/client.rb
22
+ - lib/ulule/error.rb
20
23
  homepage: https://github.com/spuyet/gem-ulule
21
24
  licenses:
22
25
  - MIT
@@ -37,8 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
40
  version: '0'
38
41
  requirements: []
39
42
  rubyforge_project:
40
- rubygems_version: 2.2.2
43
+ rubygems_version: 2.4.4
41
44
  signing_key:
42
45
  specification_version: 4
43
- summary: Ulule API for Rails
46
+ summary: A gem for ulule API
44
47
  test_files: []