transparent_data-rb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f258721c6c110c19f04edf8539c83c69848db272da6198e0c64938cb5d14caf5
4
- data.tar.gz: 7b43d915c95b584c2d0e2b79956bf0f390dd509b800208a57f1c070ef4cd98aa
3
+ metadata.gz: 5a46655327640a1c8d2bf6c73418692c2f962add03aab036551c89aeb966d972
4
+ data.tar.gz: fc85f512554cea23a73e27ceff5855c4d78f7f93280ea5b48277a8e93ace15f3
5
5
  SHA512:
6
- metadata.gz: 5d3c2a4abfaceb3fad5f1f9430ecf3172459cab72fe7f5ff1711706a7d7f848341f9eebe133eb3033a92ca6361bc4205b2b5f8ac0649da2b49f9b5c04333a87f
7
- data.tar.gz: 24fe0d76d0e496fba62722ac6ac65bb9f9898c047df4edb3143f0897ffe4740426ecb4c8c4f90f00d66b9e6d9833e1c46d2c080cf4b120d65282ddf1eaeb4d2b
6
+ metadata.gz: 6f74412f2aef37f3af6c2ce6fb38798239ee6fd7aa706752f68d1770ee9e34844e1249877f5dca1935fe687ed51931165dd522de2d05c528a71613694d972313
7
+ data.tar.gz: d9b697e8c885558e79dc95ace299695bab5314af77de0849314248d8eb9a6d9191d1b9a9375f1077d5dcc901d20fa0805773aff41c1a8d1d6c2a861debb12c4c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- transparent_data-rb (0.0.1)
4
+ transparent_data-rb (0.1.4)
5
5
  bundler (~> 2.1)
6
6
  faraday
7
7
  rake (~> 13.0)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # PG Inheritance
1
+ # TransparentData RB
2
2
  [![Gem Version](https://badge.fury.io/rb/transparent_data-rb.svg)](https://badge.fury.io/rb/transparent_data-rb) [![Build Status](https://travis-ci.com/sigmen/transparent_data-rb.svg?branch=master)](https://travis-ci.com/sigmen/transparent_data-rb)
3
3
 
4
- # UNDER DEVELOPMENT
5
-
6
4
  ## Supports
7
5
 
8
6
  Ruby `>= 2.4`.
@@ -1,8 +1,10 @@
1
1
  require 'faraday'
2
+ require 'json'
2
3
 
3
4
  require 'transparent_data/version'
4
5
  require 'transparent_data/exceptions'
5
6
  require 'transparent_data/configurable'
7
+ require 'transparent_data/request'
6
8
  require 'transparent_data/module_functions'
7
9
 
8
10
  module TransparentData
@@ -0,0 +1,28 @@
1
+ module TransparentData
2
+ module Actions
3
+ class Add
4
+ attr_reader :client, :source, :method, :parameters
5
+
6
+ def initialize(client, source, method, parameters)
7
+ @client = client
8
+ @source = source
9
+ @method = method
10
+ @parameters = parameters
11
+ end
12
+
13
+ def call
14
+ TransparentData::Request.call(client, 'add', json: build_json)
15
+ end
16
+
17
+ private
18
+
19
+ def build_json
20
+ {
21
+ source: source,
22
+ method: method,
23
+ parameters: parameters
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module TransparentData
2
+ module Actions
3
+ class Result
4
+ attr_reader :client, :ident
5
+
6
+ def initialize(client, ident)
7
+ @client = client
8
+ @ident = ident
9
+ end
10
+
11
+ def call
12
+ TransparentData::Request.call(client, 'result', query: build_query(ident))
13
+ end
14
+
15
+ private
16
+
17
+ def build_query
18
+ { ident: ident }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ module TransparentData
2
+ module Actions
3
+ class Search
4
+ COUNTRY_TO_KEY_MAP = {
5
+ finland: :fi,
6
+ czech: :cz,
7
+ denmark: :dk,
8
+ norway: :no,
9
+ slovakia: :sk,
10
+ uk: :uk
11
+ }.freeze
12
+
13
+ attr_reader :client, :country, :query_str
14
+
15
+ def initialize(client, country, query_str)
16
+ @client = client
17
+ @country = country
18
+ @query_str = query_str
19
+ end
20
+
21
+ def call
22
+ search_method = "#{fetch_country_id(country)}Search"
23
+
24
+ TransparentData::Request.call(client, search_method, query: build_query(query_str))
25
+ end
26
+
27
+ private
28
+
29
+ def build_query(query_str)
30
+ { q: query_str }
31
+ end
32
+
33
+ def fetch_country_id(country)
34
+ COUNTRY_TO_KEY_MAP.fetch(country.to_s.downcase.to_sym) do
35
+ raise TransparentData::UnknownCountryError, "Unknown country: #{country.inspect}"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,8 +2,16 @@ require 'transparent_data/request'
2
2
 
3
3
  module TransparentData
4
4
  module ModuleFunctions
5
- def search(country, query)
6
- TransparentData::Request.call(client, country, query)
5
+ def search(country, query_str)
6
+ TransparentData::Actions::Search.new(client, country, query_str).call
7
+ end
8
+
9
+ def add(source, method, parameters)
10
+ TransparentData::Actions::Add.new(client, source, method, parameters).call
11
+ end
12
+
13
+ def result(ident)
14
+ TransparentData::Actions::Result.new(client, ident).call
7
15
  end
8
16
 
9
17
  private
@@ -2,33 +2,24 @@ require 'transparent_data/response/struct'
2
2
 
3
3
  module TransparentData
4
4
  class Request
5
- QUERY_PARAM = :q
6
-
7
- COUNTRY_TO_KEY_MAP = {
8
- finland: :fi,
9
- czech: :cz,
10
- denmark: :dk,
11
- norway: :no,
12
- slovakia: :sk,
13
- uk: :uk
14
- }.freeze
15
-
16
- def self.call(client, country, query)
17
- response = client.post(build_path(country, query))
5
+ def self.call(client, method, query: {}, json: {})
6
+ response = client.post(build_path(method, query), json)
18
7
 
19
8
  TransparentData::Response::Struct.new(response)
20
9
  end
21
10
 
22
11
  private
23
12
 
24
- def build_path(country, query)
25
- "/#{TransparentData.key}/#{fetch_country_id(country)}Search?#{QUERY_PARAM}=#{query}"
13
+ def build_path(method, query)
14
+ base_path = "/#{TransparentData.key}/#{method}"
15
+
16
+ return base_path unless query&.any?
17
+
18
+ base_path.concat("?#{convert_params_to_query(query)}")
26
19
  end
27
20
 
28
- def fetch_country_id(country)
29
- COUNTRY_TO_KEY_MAP.fetch(country.to_s.downcase.to_sym) do
30
- raise TransparentData::UnknownCountryError, "Unknown country: #{country.inspect}"
31
- end
21
+ def convert_params_to_query(params)
22
+ params.map { |pair| pair.join('=') }.join('&')
32
23
  end
33
24
  end
34
25
  end
@@ -3,11 +3,17 @@ module TransparentData
3
3
  class Struct
4
4
  extend Forwardable
5
5
 
6
- def_delegators :@response, :body, :headers, :status
6
+ def_delegators :@response, :headers, :status, :success?
7
7
 
8
8
  def initialize(response)
9
9
  @response = response
10
10
  end
11
+
12
+ def body
13
+ JSON.parse(@response.body)
14
+ rescue JSON::ParserError
15
+ @response.body
16
+ end
11
17
  end
12
18
  end
13
19
  end
@@ -1,3 +1,3 @@
1
1
  module TransparentData
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transparent_data-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kakorin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-21 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,9 @@ files:
114
114
  - bin/console
115
115
  - bin/setup
116
116
  - lib/transparent_data.rb
117
+ - lib/transparent_data/actions/add.rb
118
+ - lib/transparent_data/actions/result.rb
119
+ - lib/transparent_data/actions/search.rb
117
120
  - lib/transparent_data/configurable.rb
118
121
  - lib/transparent_data/exceptions.rb
119
122
  - lib/transparent_data/module_functions.rb