vidalo 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec108eae52b5747eee1c9274e0f12b622db0b375
4
+ data.tar.gz: 5ef829bdc878d526837c77f57cdb4a30e32d2294
5
+ SHA512:
6
+ metadata.gz: e5af7fb67661b82946e421a62a545eacc6b2863f939c0a0189f6af5ae104f4a147da3851ad191e1359260cb9d4ce1a0e0858296ea52b5fbf7d3732d68d4ea936
7
+ data.tar.gz: b5ace2570953e1c71a2759dc65ed891904446a70d406be6f342e0d7e63bb975db6b135a59d6084fd51eaaf4a02a6ca99c99c44242c17f1cbdb0ae1636abfe4ac
@@ -0,0 +1,37 @@
1
+ module Vidalo
2
+ module API
3
+ module Get
4
+ # Return Nokogiri object
5
+ def send_api_request(keyword, params = {})
6
+ request = @conn.get do |req|
7
+ req.url(keyword)
8
+ unless params.empty?
9
+ params.each do |param_key, param_value|
10
+ req.params[param_key] = param_value
11
+ end
12
+ end
13
+ end
14
+
15
+ unless request.status == 200
16
+ raise %{
17
+
18
+ Seems there is a problem when connect to Vidal server.
19
+ The HTTP request status code is: #{request.status}.
20
+
21
+ You may want to test the connection with the URL below in your browser:
22
+ #{request.env.url}
23
+ }
24
+ else
25
+ Nokogiri::XML(request.body).errors.empty? ?
26
+ Nokogiri::XML(request.body).remove_namespaces!.at('feed') :
27
+ nil
28
+ end
29
+ end
30
+
31
+ # Return number of total products
32
+ def get_number_of_products
33
+ send_api_request('products', { 'start-page' => 1, 'page-size' => 1 }).at('totalResults').text.to_i
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,49 @@
1
+ module Vidalo
2
+ module API
3
+ module Search
4
+ # Get result of a set of Nokogiri Objects
5
+ #
6
+ # @params type [String], query [String], start_page [Fixnum], page_size [Fixnum]
7
+ # @return [Array]
8
+ def search(type: nil, query: nil, start_page: 1, page_size: 10)
9
+ if type.nil?
10
+ raise ArgumentError.new 'Error: You need to define "type" at least (eg. "products")'
11
+ end
12
+ @res = send_api_request(type, {'q' => query, 'start-page' => start_page, 'page-size' => page_size })
13
+ @res.search('entry').to_a if @res
14
+ end
15
+
16
+
17
+ # Get result of a product's information
18
+ #
19
+ # @params id[Fixnum], aggregate[Array[String]], all_info[TrueClass]
20
+ # @return [Nokogiri]
21
+ #
22
+ # Example:
23
+ # api.search_product(id: 45, aggregate: ['UCD'])
24
+ # api.search_product(id: 45, all_info: true)
25
+ #
26
+ def search_product(id: nil, aggregate: [], all_info: nil)
27
+ raise %{
28
+
29
+ Need a Vidal Product ID.
30
+
31
+ } unless id.is_a? Fixnum || id.to_i.to_s == id
32
+ aggregate = %w(
33
+ molecules
34
+ indications
35
+ ald_detail
36
+ smr_asmr
37
+ atc_classification
38
+ VIDAL_CLASSIFICATION
39
+ product_range
40
+ document
41
+ reco
42
+ UCDS
43
+ UNITS
44
+ ) if all_info == true
45
+ @res = send_api_request("product/#{id}", { 'aggregate' => aggregate })
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require 'vidalo/parser'
2
+ require 'vidalo/api/get'
3
+ require 'vidalo/api/search'
4
+ VIDAL_API_URL = 'http://apirest-dev.vidal.fr/rest/api/'
5
+
6
+ module Vidalo
7
+ class Connection
8
+ include Vidalo::API::Get
9
+ include Vidalo::API::Search
10
+ include Vidalo::Parser
11
+ attr_reader :app_id, :app_key
12
+
13
+ def initialize(app_id, app_key)
14
+ @app_id = app_id
15
+ @app_key = app_key
16
+ @conn = Faraday.new(url: VIDAL_API_URL,
17
+ request: { params_encoder: Faraday::FlatParamsEncoder },
18
+ params: { app_id: app_id, app_key: app_key }) do |faraday|
19
+ faraday.adapter Faraday.default_adapter
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Vidalo
2
+ module Parser
3
+ # Return string
4
+ def get_inner_text(type: nil, id: nil, node: nil)
5
+ if type.nil? || id.nil? || node.nil?
6
+ raise ArgumentError.new 'Error: You need to define "type", "id" and node name - eg. get_inner_text(type: "package", id: 5355, node: "title")'
7
+ end
8
+ result = send_api_request("#{type}/#{id}")
9
+ result ?
10
+ get_node_value(result.at('entry').at(node)) :
11
+ ""
12
+ end
13
+
14
+
15
+ # Return string
16
+ def get_node_value(node)
17
+ node ?
18
+ node.text :
19
+ ""
20
+ end
21
+ end
22
+ end
data/lib/vidalo.rb ADDED
@@ -0,0 +1 @@
1
+ require 'vidalo/connection'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vidalo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Xiaofan Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for using Vidal API easier
14
+ email: xfanwu@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/vidalo.rb
20
+ - lib/vidalo/api/get.rb
21
+ - lib/vidalo/api/search.rb
22
+ - lib/vidalo/connection.rb
23
+ - lib/vidalo/parser.rb
24
+ homepage: http://rubygems.org/gems/vidalo
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A gem for using Vidal API easier
48
+ test_files: []