transparent_data-rb 0.1.2 → 0.1.7

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: fdb82eceff80959973b79ac2ac2eb2f70c3f304cc871eea6afec0ca68f052923
4
- data.tar.gz: 3bd9b51316d4793062bdb42a9aa21b00874d1fab976d45d1114b0687d66d88ec
3
+ metadata.gz: a4a8f55fa7433e0e315d2bbb74b17105bdbb26f547e68cd3e4e869d81a98b836
4
+ data.tar.gz: 710a59a70647472ac85a443af84a6f5a1fd21b5e9d657a562625c2f5e9d82f14
5
5
  SHA512:
6
- metadata.gz: eae69543829933e695001af6c663a8a6e6a6ebfacd770c145e2d1cad87ffb36b81cc4e07ad86aaa74bfc930e4cd00f1d39db646f64237cb704eb2720d0e41584
7
- data.tar.gz: 689417247a6e2d4cf130b713257b7672865a11334345c2b7c51e20acbd6f061253b5ed888164517a5ca156cf8c0e99e11ab4cca78aa0eb534046fad97b4eb3f6
6
+ metadata.gz: 8a93674569fbb5dddd85e95635beff0fbda36e8a3ccd380f89d27eda11c6e368bbf7b4c9fed0fab7335565c806ed1da198310860522e10fda9cde47e81a48af3
7
+ data.tar.gz: f6390304fb55dcfb2bccfe77a2f979606b3bd60d9e2efea44da569e3967192b9761498164d403c63222d933f4fde4d0d769eb4d2272ab7eb61f571d3b59c4b1f
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ transparent_data-rb-*.gem
@@ -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.7)
5
5
  bundler (~> 2.1)
6
6
  faraday
7
7
  rake (~> 13.0)
data/README.md CHANGED
@@ -1,44 +1,56 @@
1
- # TransparentData
1
+ # TransparentData RB
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)
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/transparent_data`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+ ## Supports
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Ruby `>= 2.4`.
6
7
 
7
8
  ## Installation
8
9
 
9
10
  Add this line to your application's Gemfile:
10
11
 
11
- ```ruby
12
- gem 'transparent_data-rb'
13
- ```
12
+ gem 'transparent_data-rb'
14
13
 
15
14
  And then execute:
16
15
 
17
16
  $ bundle install
18
17
 
19
- Or install it yourself as:
18
+ Or install with:
20
19
 
21
20
  $ gem install transparent_data-rb
22
21
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+ ### Usage
30
23
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
24
+ ```ruby
25
+ response = TransparentData.search(country_name, query)
34
26
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/transparent_data-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/transparent_data-rb/blob/master/CODE_OF_CONDUCT.md).
27
+ response.body # => [{...}]
28
+ response.status # => 200
29
+ response.headers # => {...}
30
+ ```
36
31
 
32
+ #### Search options
33
+ * country_name - Any available country name (see Available countries)
34
+ * query - Search query
37
35
 
38
- ## License
36
+ #### Available countries
37
+ Gem supports all available Transparentdata.eu countries:
38
+ * finland
39
+ * uk
40
+ * denmark
41
+ * norway
42
+ * slovakia
43
+ * czech
39
44
 
40
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+ #### Configuration
41
46
 
42
- ## Code of Conduct
47
+ Before using gem you need to configure some parameters:
43
48
 
44
- Everyone interacting in the TransparentData project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/transparent_data-rb/blob/master/CODE_OF_CONDUCT.md).
49
+ ```ruby
50
+ TransparentData.configure do |config|
51
+ config.url = 'https://transparentdata.eu/' # You can using any another url for test environment
52
+ config.user = 'user' # Username for HTTP Basic
53
+ config.password = 'password' # Password for HTTP Basic
54
+ config.key = 'key' # API Key
55
+ end
56
+ ```
@@ -1,13 +1,14 @@
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'
6
- require 'transparent_data/module_functions'
7
+ require 'transparent_data/actions'
7
8
 
8
9
  module TransparentData
9
10
  extend TransparentData::Configurable
10
- extend TransparentData::ModuleFunctions
11
+ extend TransparentData::Actions
11
12
 
12
13
  initialize_config_accessors(:url, :user, :password, :key)
13
14
  end
@@ -0,0 +1,28 @@
1
+ require 'transparent_data/request'
2
+ require 'transparent_data/actions/search'
3
+ require 'transparent_data/actions/add'
4
+ require 'transparent_data/actions/result'
5
+
6
+ module TransparentData
7
+ module Actions
8
+ def search(country, query_str)
9
+ TransparentData::Actions::Search.new(client, country, query_str).call
10
+ end
11
+
12
+ def add(source, method, parameters)
13
+ TransparentData::Actions::Add.new(client, source, method, parameters).call
14
+ end
15
+
16
+ def result(ident)
17
+ TransparentData::Actions::Result.new(client, ident).call
18
+ end
19
+
20
+ private
21
+
22
+ def client
23
+ @client ||= Faraday.new(TransparentData.url) do |conn|
24
+ conn.basic_auth(TransparentData.user, TransparentData.password)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -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
@@ -14,7 +14,7 @@ module TransparentData
14
14
  end
15
15
 
16
16
  define_singleton_method("#{attr}=") do |value|
17
- public_send(attr) || instance_variable_set("@#{attr}", value)
17
+ instance_variable_get("@#{attr}") || instance_variable_set("@#{attr}", value)
18
18
  end
19
19
  end
20
20
  end
@@ -2,32 +2,25 @@ require 'transparent_data/response/struct'
2
2
 
3
3
  module TransparentData
4
4
  class Request
5
- QUERY_PARAM = :q
5
+ class << self
6
+ def call(client, method, query: {}, json: {})
7
+ response = client.post(build_path(method, query), json.to_json)
6
8
 
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
9
+ TransparentData::Response::Struct.new(response)
10
+ end
15
11
 
16
- def self.call(client, country, query)
17
- response = client.post(build_path(country, query))
12
+ private
18
13
 
19
- TransparentData::Response::Struct.new(response)
20
- end
14
+ def build_path(method, query)
15
+ base_path = "/#{TransparentData.key}/#{method}"
21
16
 
22
- private
17
+ return base_path unless query&.any?
23
18
 
24
- def build_path(country, query)
25
- "/#{TransparentData.key}/#{fetch_country_id(country)}Search?#{QUERY_PARAM}=#{query}"
26
- end
19
+ base_path.concat("?#{convert_params_to_query(query)}")
20
+ end
27
21
 
28
- def fetch_country_id(country)
29
- COUNTRY_TO_KEY_MAP.fetch(country.to_sym) do
30
- raise TransparentData::UnknownCountryError, "Unknown country: #{country.inspect}"
22
+ def convert_params_to_query(params)
23
+ params.map { |pair| pair.join('=') }.join('&')
31
24
  end
32
25
  end
33
26
  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.2'.freeze
2
+ VERSION = '0.1.7'.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.2
4
+ version: 0.1.7
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,9 +114,12 @@ files:
114
114
  - bin/console
115
115
  - bin/setup
116
116
  - lib/transparent_data.rb
117
+ - lib/transparent_data/actions.rb
118
+ - lib/transparent_data/actions/add.rb
119
+ - lib/transparent_data/actions/result.rb
120
+ - lib/transparent_data/actions/search.rb
117
121
  - lib/transparent_data/configurable.rb
118
122
  - lib/transparent_data/exceptions.rb
119
- - lib/transparent_data/module_functions.rb
120
123
  - lib/transparent_data/request.rb
121
124
  - lib/transparent_data/response/struct.rb
122
125
  - lib/transparent_data/version.rb
@@ -1,17 +0,0 @@
1
- require 'transparent_data/request'
2
-
3
- module TransparentData
4
- module ModuleFunctions
5
- def search(country, query)
6
- TransparentData::Request.call(client, country, query)
7
- end
8
-
9
- private
10
-
11
- def client
12
- @client ||= Faraday.new(TransparentData.url) do |conn|
13
- conn.basic_auth(TransparentData.user, TransparentData.password)
14
- end
15
- end
16
- end
17
- end