visupedia 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/visupedia.rb +8 -0
- data/lib/visupedia/api/visu.rb +35 -0
- data/lib/visupedia/client.rb +23 -0
- data/lib/visupedia/error.rb +9 -0
- data/lib/visupedia/error/client_error.rb +18 -0
- data/lib/visupedia/http_client.rb +118 -0
- data/lib/visupedia/http_client/auth_handler.rb +53 -0
- data/lib/visupedia/http_client/error_handler.rb +42 -0
- data/lib/visupedia/http_client/request_handler.rb +24 -0
- data/lib/visupedia/http_client/response.rb +20 -0
- data/lib/visupedia/http_client/response_handler.rb +19 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0ace392474dc229b91478077cad68dd65b7aea58
|
4
|
+
data.tar.gz: 4d940104f7869d1bba0ff19231df510861b8d4d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7f03a9064fbb90ea3ac5f07941a279c25689cb800c722082d0a07889f66c1c845915ffc6bac0aba31b3b93cecbbf9a67b265ed5e6af1c9a55095f7156d672da
|
7
|
+
data.tar.gz: ed453eab8c26067c2c4eddcbd04d881b61197112eaa809c73a278be325e8fdda045607fd196106465ec98e4566784b2541462ab7b7fcbdb4bc43fbf911d56b34
|
data/lib/visupedia.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Visupedia
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns an MyVisu api instance
|
6
|
+
#
|
7
|
+
# key - The api key provided by Visupedia
|
8
|
+
class Visu
|
9
|
+
|
10
|
+
def initialize(key, client)
|
11
|
+
@key = key
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns all information about the wanted Visu
|
16
|
+
#
|
17
|
+
# '/api?key=:key&id=:id&lang=:lang&version=:version' GET
|
18
|
+
#
|
19
|
+
# id - The unique ID of the Visu
|
20
|
+
# lang - The language code wanted for the Visu
|
21
|
+
# version - Use a specific version of our API
|
22
|
+
def get(id, lang, version, options = {})
|
23
|
+
body = options.fetch(:query, {})
|
24
|
+
body[:id] = id
|
25
|
+
body[:lang] = lang
|
26
|
+
body[:version] = version
|
27
|
+
|
28
|
+
@client.get("/api?key=#{@key}&id=#{id}&lang=#{lang}&version=#{version}", body, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
require "visupedia/api/visu"
|
5
|
+
|
6
|
+
module Visupedia
|
7
|
+
|
8
|
+
class Client
|
9
|
+
|
10
|
+
def initialize(auth = {}, options = {})
|
11
|
+
@http_client = Visupedia::HttpClient::HttpClient.new(auth, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns an MyVisu api instance
|
15
|
+
#
|
16
|
+
# key - The api key provided by Visupedia
|
17
|
+
def visu(key)
|
18
|
+
Visupedia::Api::Visu.new(key, @http_client)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require "visupedia/http_client/auth_handler"
|
2
|
+
require "visupedia/http_client/error_handler"
|
3
|
+
require "visupedia/http_client/request_handler"
|
4
|
+
require "visupedia/http_client/response"
|
5
|
+
require "visupedia/http_client/response_handler"
|
6
|
+
|
7
|
+
module Visupedia
|
8
|
+
|
9
|
+
module HttpClient
|
10
|
+
|
11
|
+
# Main HttpClient which is used by Api classes
|
12
|
+
class HttpClient
|
13
|
+
|
14
|
+
attr_accessor :options, :headers
|
15
|
+
|
16
|
+
def initialize(auth = {}, options = {})
|
17
|
+
|
18
|
+
@options = {
|
19
|
+
:base => "https://dev.visupedia.net",
|
20
|
+
:user_agent => "alpaca/0.2.1 (https://github.com/pksunkara/alpaca)"
|
21
|
+
}
|
22
|
+
|
23
|
+
@options.update(options)
|
24
|
+
|
25
|
+
@headers = {
|
26
|
+
"user-agent" => @options[:user_agent]
|
27
|
+
}
|
28
|
+
|
29
|
+
if @options.has_key?(:headers)
|
30
|
+
@headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
|
31
|
+
@options.delete(:headers)
|
32
|
+
end
|
33
|
+
|
34
|
+
@client = Faraday.new(@options[:base]) do |conn|
|
35
|
+
conn.use(Visupedia::HttpClient::AuthHandler, auth)
|
36
|
+
conn.use(Visupedia::HttpClient::ErrorHandler)
|
37
|
+
|
38
|
+
conn.adapter(Faraday.default_adapter)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get(path, params = {}, options = {})
|
43
|
+
request(path, nil, "get", options.merge({ :query => params }))
|
44
|
+
end
|
45
|
+
|
46
|
+
def post(path, body = {}, options = {})
|
47
|
+
request(path, body, "post", options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def patch(path, body = {}, options = {})
|
51
|
+
request(path, body, "patch", options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete(path, body = {}, options = {})
|
55
|
+
request(path, body, "delete", options)
|
56
|
+
end
|
57
|
+
|
58
|
+
def put(path, body = {}, options = {})
|
59
|
+
request(path, body, "put", options)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Intermediate function which does three main things
|
63
|
+
#
|
64
|
+
# - Transforms the body of request into correct format
|
65
|
+
# - Creates the requests with give parameters
|
66
|
+
# - Returns response body after parsing it into correct format
|
67
|
+
def request(path, body, method, options)
|
68
|
+
options = @options.merge(options)
|
69
|
+
|
70
|
+
options[:headers] = options[:headers] || {}
|
71
|
+
options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])
|
72
|
+
|
73
|
+
options[:body] = body
|
74
|
+
|
75
|
+
if method != "get"
|
76
|
+
options[:body] = options[:body] || {}
|
77
|
+
options = set_body(options)
|
78
|
+
end
|
79
|
+
|
80
|
+
response = create_request(method, path, options)
|
81
|
+
|
82
|
+
body = get_body(response)
|
83
|
+
|
84
|
+
Visupedia::HttpClient::Response.new(body, response.status, response.headers)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Creating a request with the given arguments
|
88
|
+
#
|
89
|
+
# If api_version is set, appends it immediately after host
|
90
|
+
def create_request(method, path, options)
|
91
|
+
version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
|
92
|
+
|
93
|
+
path = "#{version}#{path}"
|
94
|
+
|
95
|
+
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
96
|
+
@client.#{method}(path) do |req|
|
97
|
+
req.body = options[:body]
|
98
|
+
req.headers.update(options[:headers])
|
99
|
+
req.params.update(options[:query]) if options[:query]
|
100
|
+
end
|
101
|
+
RUBY
|
102
|
+
end
|
103
|
+
|
104
|
+
# Get response body in correct format
|
105
|
+
def get_body(response)
|
106
|
+
Visupedia::HttpClient::ResponseHandler.get_body(response)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Set request body in correct format
|
110
|
+
def set_body(options)
|
111
|
+
Visupedia::HttpClient::RequestHandler.set_body(options)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
module Visupedia
|
4
|
+
|
5
|
+
module HttpClient
|
6
|
+
|
7
|
+
# AuthHandler takes care of devising the auth type and using it
|
8
|
+
class AuthHandler < Faraday::Middleware
|
9
|
+
|
10
|
+
def initialize(app, auth = {}, options = {})
|
11
|
+
@auth = auth
|
12
|
+
super(app)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
if !@auth.empty?
|
17
|
+
auth = get_auth_type
|
18
|
+
flag = false
|
19
|
+
|
20
|
+
if !flag
|
21
|
+
raise StandardError.new "Unable to calculate authorization method. Please check"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Calculating the Authentication Type
|
29
|
+
def get_auth_type()
|
30
|
+
|
31
|
+
return -1
|
32
|
+
end
|
33
|
+
|
34
|
+
def query_params(url)
|
35
|
+
if url.query.nil? or url.query.empty?
|
36
|
+
{}
|
37
|
+
else
|
38
|
+
Faraday::Utils.parse_query(url.query)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def merge_query(env, query)
|
43
|
+
query = query.update query_params(env[:url])
|
44
|
+
|
45
|
+
env[:url].query = Faraday::Utils.build_query(query)
|
46
|
+
|
47
|
+
return env
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Visupedia
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# ErrorHanlder takes care of selecting the error message from response body
|
6
|
+
class ErrorHandler < Faraday::Middleware
|
7
|
+
|
8
|
+
def initialize(app)
|
9
|
+
super(app)
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
@app.call(env).on_complete do |env|
|
14
|
+
code = env[:response].status
|
15
|
+
type = env[:response].headers["content-type"]
|
16
|
+
|
17
|
+
case code
|
18
|
+
when 500...599
|
19
|
+
raise Visupedia::Error::ClientError.new("Error #{code}", code)
|
20
|
+
when 400...499
|
21
|
+
body = Visupedia::HttpClient::ResponseHandler.get_body(env[:response])
|
22
|
+
message = ""
|
23
|
+
|
24
|
+
# If HTML, whole body is taken
|
25
|
+
if body.is_a?(String)
|
26
|
+
message = body
|
27
|
+
end
|
28
|
+
|
29
|
+
if message == ""
|
30
|
+
message = "Unable to understand the content type of response returned by request responsible for error"
|
31
|
+
end
|
32
|
+
|
33
|
+
raise Visupedia::Error::ClientError.new message, code
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Visupedia
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# RequestHandler takes care of encoding the request body into format given by options
|
6
|
+
class RequestHandler
|
7
|
+
|
8
|
+
def self.set_body(options)
|
9
|
+
type = options.fetch(:request_type, "form")
|
10
|
+
|
11
|
+
# Raw body
|
12
|
+
if type == "raw"
|
13
|
+
options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
|
14
|
+
options[:headers].delete("content-type")
|
15
|
+
end
|
16
|
+
|
17
|
+
return options
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Visupedia
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# Response object contains the response returned by the client
|
6
|
+
class Response
|
7
|
+
|
8
|
+
attr_accessor :body, :code, :headers
|
9
|
+
|
10
|
+
def initialize(body, code, headers)
|
11
|
+
@body = body
|
12
|
+
@code = code
|
13
|
+
@headers = headers
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Visupedia
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# ResponseHandler takes care of decoding the response body into suitable type
|
6
|
+
class ResponseHandler
|
7
|
+
|
8
|
+
def self.get_body(response)
|
9
|
+
type = response.headers["content-type"]
|
10
|
+
body = response.body
|
11
|
+
|
12
|
+
return body
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: visupedia
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gaël Gillard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.8
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.7.7
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.7.7
|
41
|
+
description: Official Visupedia API library client for ruby
|
42
|
+
email: dev@visupedia.net
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/visupedia.rb
|
48
|
+
- lib/visupedia/api/visu.rb
|
49
|
+
- lib/visupedia/client.rb
|
50
|
+
- lib/visupedia/error.rb
|
51
|
+
- lib/visupedia/error/client_error.rb
|
52
|
+
- lib/visupedia/http_client.rb
|
53
|
+
- lib/visupedia/http_client/auth_handler.rb
|
54
|
+
- lib/visupedia/http_client/error_handler.rb
|
55
|
+
- lib/visupedia/http_client/request_handler.rb
|
56
|
+
- lib/visupedia/http_client/response.rb
|
57
|
+
- lib/visupedia/http_client/response_handler.rb
|
58
|
+
homepage: http://visupedia.net
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Official Visupedia API library client for ruby
|
82
|
+
test_files: []
|