easy_manage_client 0.1.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
+ SHA256:
3
+ metadata.gz: 516e8348314da4e3265d1d59ec548ea634a756aa3e5d957dcf5c7e067e206522
4
+ data.tar.gz: e5b3eb4b2559746393c38d4847de3d57bec07cde5278966e571d44d0c38116ba
5
+ SHA512:
6
+ metadata.gz: ce4eea5e6b968b44c528fe12096bd64ddca6238c347576768ce177679efe596bb5e5f7989dfad10cfc6ca12726cad1a5f35c7414246b463500825a75dbfc8533
7
+ data.tar.gz: 5ec2aad214355b7e032760edf9533d5f3d10116a6a0dbc06098d09b36d35f9f02be31b0eb87a0a32db23d40eb29a13fda00ad910ca61a7cf9d6c989217cce49d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/*[.yml, .json]
9
+ Gemfile.lock
10
+ .env
11
+ .byebug_history
12
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ before_install: gem install bundler -v 2.1.2
3
+ rvm:
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
9
+ install:
10
+ - bundle install --retry=3
11
+ script:
12
+ - bundle exec rake test
13
+ - bundle exec rake rubocop
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # EasyManageClient [![Build Status](https://travis-ci.org/sertangulveren/easy_manage_client.svg?branch=master)](https://travis-ci.org/sertangulveren/easy_manage_client)
2
+
3
+ This client allows you to download compiled dumps in JSON or YAML format, such as translation, configuration etc. on EasyManage server to your project easly.
4
+
5
+ It is recommended to use at the projects initial stage.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'easy_manage_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install easy_manage_client
22
+
23
+ ## Configuration
24
+
25
+ Client must be configured before use. Configuration fields are as:
26
+
27
+ | Field | Description |
28
+ | --------------- | ------------------------------------------------------------------------------------------------------- |
29
+ | root_url | Root url of the EasyManage server. |
30
+ | auth_token | Your 2.0 authentication token string without the 'Bearer'. |
31
+ | extension | Dump format. Available values: 'json', 'yml' |
32
+ | download_folder | Information to which folder the dump will be downloaded. |
33
+ | compile_id | The id of the compiled version. If not passed, the client always downloads the latest compiled dump. |
34
+
35
+ ### Configuration on Rails Application:
36
+ Create a file in the `config/initializers` directory and configure client in this file as below:
37
+ `# config/initializers/easy_manage_client.rb`
38
+ ```ruby
39
+ EasyManageClient.configure do |config|
40
+ config.root_url = 'https://easymanage.example.com'
41
+ config.auth_token = 'YOUR_SECRET_AUTH_TOKEN'
42
+ config.extension = 'yml'
43
+ config.download_folder = File.join(Rails.root, 'config', 'translations')
44
+ config.compile_id = 'SPECIAL_COMPILED_VERSION_ID'
45
+ end
46
+ ```
47
+ ### Multiple Configuration
48
+ You can define multiple configurations separated by profile. To do this, you must pass the profile parameter to the `configure` definition.
49
+ If the profile name is not pass in the configuration, it defaults to use `:default`.
50
+ ##### Example:
51
+ In this example, to download dynamicly application settings, profile name specified as `settings`.
52
+ `# config/initializers/dynamic_settings.rb`
53
+ ```ruby
54
+ EasyManageClient.configure(:dynamic_settings) do |config|
55
+ config.root_url = 'https://easymanage.example.com'
56
+ config.auth_token = 'YOUR_SECRET_AUTH_TOKEN'
57
+ config.extension = 'json'
58
+ config.download_folder = File.join(Rails.root, 'config', 'settings')
59
+ end
60
+ ```
61
+ ## Usage
62
+ Initialize a client object before starting process.
63
+ ```ruby
64
+ client = EasyManageClient::Downloader.new
65
+ ```
66
+ To download compiled dump, call the method `perform`.
67
+ The client downloads the compiled version to the directory specified in the configuration, in the specified format.
68
+ In this case, client uses the folder: `File.join(Rails.root, 'config', 'translations')`.
69
+ ```ruby
70
+ client.perform
71
+ ```
72
+
73
+ > In case of an unsuccessful download; `perform!` can be used instead of `perform` for error exception. This method creates an `EasyManageClient::DownloadProcessFailed` exception.
74
+
75
+ You can pass the profile argument on client initialization to use different configuration.
76
+ ```ruby
77
+ client = EasyManageClient::Downloader.new(profile: :dynamic_settings)
78
+ client.perform
79
+ ```
80
+
81
+ If the download process will be performed only in the Rails initialization, the following method can be used:
82
+ `# config/initializers/easy_manage_client.rb`
83
+ ```ruby
84
+ EasyManageClient.configure(:dummy_profile) do |config|
85
+ config.root_url = 'https://easymanage.example.com'
86
+ config.auth_token = 'YOUR_SECRET_AUTH_TOKEN'
87
+ config.extension = 'yml'
88
+ config.download_folder = File.join(Rails.root, 'config', 'settings')
89
+ end
90
+ client = EasyManageClient::Downloader.new(profile: :dummy_profile)
91
+ client.perform!
92
+ ```
93
+
94
+ ### Using Rake Tasks
95
+ The client has its own rake tasks.
96
+ This feature can be used in configurations such as CI configurations, Dockerfile etc.
97
+
98
+ Use rake task to download your compiled dump as below:
99
+
100
+ `rake easy_manage_client:download`
101
+
102
+ Pass profile to rake task:
103
+
104
+ `easy_manage_client:download PROFILE=dummy_settings`
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sertangulveren/easy_manage_client.
108
+
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'rubocop/rake_task'
6
+
7
+ import('lib/tasks/easy_manage_client.rake')
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'test'
11
+ t.libs << 'lib'
12
+ t.test_files = FileList['test/**/*_test.rb']
13
+ t.warning = false
14
+ end
15
+
16
+ task default: :test
17
+
18
+ RuboCop::RakeTask.new
19
+
20
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'easy_manage_client'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/easy_manage_client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'easy_manage_client'
7
+ spec.version = EasyManageClient::VERSION
8
+ spec.authors = ['Sertan Gülveren', 'Yiğit Sadıç']
9
+ spec.email = ['sertangulveren@gmail.com', 'yigitsadic@gmail.com']
10
+
11
+ spec.summary = 'EasyManage API client for ruby.'
12
+ spec.description = 'It is a simple ruby client for EasyManage API.'
13
+ spec.homepage = 'https://github.com/sertangulveren/easy_manage_client'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem
21
+ # that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ f.match(%r{^(test|spec|features)/})
25
+ end
26
+ end
27
+
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency('dotenv', '~> 2.7')
31
+ spec.add_development_dependency('minitest', '~> 5.0')
32
+ spec.add_development_dependency('rake', '~> 12.0')
33
+ spec.add_development_dependency('rubocop', '~> 0.79.0')
34
+ spec.add_development_dependency('simplecov', '~> 0.17.1')
35
+ spec.add_development_dependency('webmock', '~> 3.8')
36
+
37
+ spec.add_dependency('faraday', '~> 0.11.0')
38
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # main module
4
+ module EasyManageClient
5
+ class << self
6
+ attr_accessor :configurations
7
+
8
+ def configuration(profile = :default)
9
+ raise NoProfilesWereFound if configurations.nil? || configurations.empty?
10
+
11
+ configurations.select do |conf|
12
+ conf.profile == profile
13
+ end.first || raise(ProfileNotFound, "Undefined profile: #{profile}")
14
+ end
15
+
16
+ private
17
+
18
+ def reject_profile(profile)
19
+ configurations.reject! { |conf| conf.profile == profile }
20
+ end
21
+
22
+ def initialize_configuration
23
+ self.configurations = [] if configurations.nil?
24
+ Configuration.new
25
+ end
26
+
27
+ def after_configuration_events(conf, profile)
28
+ conf.profile = profile # FORCE!
29
+ conf.check!
30
+ reject_profile(profile)
31
+ configurations << conf
32
+ end
33
+ end
34
+
35
+ def self.configure(profile = :default)
36
+ conf = initialize_configuration
37
+ yield(conf)
38
+ after_configuration_events(conf, profile)
39
+ end
40
+
41
+ # Configuration class
42
+ class Configuration
43
+ EXTENSION_TYPES = %w[yml json].freeze
44
+
45
+ attr_accessor :profile, :root_url, :auth_token, :extension, :compile_id,
46
+ :download_folder
47
+
48
+ def initialize
49
+ self.profile = :default
50
+ self.extension = 'yml'
51
+ end
52
+
53
+ def check!
54
+ raise InvalidExtension unless EXTENSION_TYPES.include?(extension)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyManageClient
4
+ # Core
5
+ class Core
6
+ attr_accessor :connection, :profile, :response, :success
7
+ def initialize(profile: :default)
8
+ self.profile = profile
9
+ # Firstly, create a connection object.
10
+ self.connection = Faraday.new(
11
+ url: ::EasyManageClient.configuration(profile).root_url,
12
+ headers: headers, params: params
13
+ )
14
+ end
15
+
16
+ # Call the api and process the response.
17
+ def perform
18
+ resp = connection.get(request_uri)
19
+ handle_response(resp)
20
+ # If server returns 200, everything is OK.
21
+ self.success = resp.status == 200
22
+ rescue Faraday::Error => e
23
+ self.response = { message: e.message }
24
+ self.success = false
25
+ rescue JSON::ParserError, ::EasyManageClient::InvalidResponseContent
26
+ # Response cannot be handled.
27
+ self.response = { message: 'Invalid response from the server.' }
28
+ self.success = false
29
+ end
30
+
31
+ # Response is parsed and checked here.
32
+ def handle_response(resp)
33
+ self.response = JSON.parse(resp.body, symbolize_names: true)
34
+
35
+ # :content and :reference must be included in response.
36
+ if response.keys.include?(:content) && response.keys.include?(:reference)
37
+ return
38
+ end
39
+
40
+ # :content or :reference not included.
41
+ raise ::EasyManageClient::InvalidResponseContent
42
+ end
43
+
44
+ # Headers prepared here.
45
+ def headers
46
+ { 'Authorization' => generate_bearer_token }
47
+ end
48
+
49
+ # Generate Bearer token from configuration.
50
+ def generate_bearer_token
51
+ "Bearer #{::EasyManageClient.configuration(profile).auth_token}"
52
+ end
53
+
54
+ # Parameters are prepared here.
55
+ def params
56
+ { version: ::EasyManageClient.configuration(profile).extension }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyManageClient
4
+ # Downloader
5
+ class Downloader < Core
6
+ def request_uri
7
+ # if version not specified, use latest uri
8
+ if ::EasyManageClient.configuration(profile).compile_id.nil?
9
+ return latest_version_uri
10
+ end
11
+
12
+ # generate compiled_version_uri
13
+ compiled_version_uri
14
+ end
15
+
16
+ def perform
17
+ # make api call and handle response
18
+ super
19
+ # if success is not true
20
+ # stop operation and return false
21
+ return false unless success
22
+
23
+ # download data to the relevant location
24
+ write_to_folder
25
+ true
26
+ end
27
+
28
+ def perform!
29
+ perform
30
+ unless success
31
+ raise ::EasyManageClient::DownloadProcessFailed, response[:message]
32
+ end
33
+
34
+ true
35
+ end
36
+
37
+ def compiled_version_uri
38
+ '/api/projects/by_reference/' +
39
+ ::EasyManageClient.configuration(profile).compile_id.to_s
40
+ end
41
+
42
+ def latest_version_uri
43
+ '/api/projects/latest_compiled_version'
44
+ end
45
+
46
+ def write_to_folder
47
+ File.open(prepare_file_path_to_download, 'w') do |file|
48
+ file.write(response[:content])
49
+ end
50
+ end
51
+
52
+ def prepare_file_path_to_download
53
+ folder = ::EasyManageClient.configuration(profile).download_folder
54
+ extension = ::EasyManageClient.configuration(profile).extension
55
+ File.join(folder, "#{response[:reference]}.#{extension}")
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'easy_manage_client'
4
+ require 'rails'
5
+
6
+ module EasyManageClient
7
+ # Railtie
8
+ class Railtie < Rails::Railtie
9
+ railtie_name :easy_manage_client
10
+
11
+ rake_tasks do
12
+ load 'tasks/easy_manage_client.rake'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyManageClient
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'easy_manage_client/version'
5
+ require 'easy_manage_client/configuration'
6
+ require 'easy_manage_client/core'
7
+ require 'easy_manage_client/downloader'
8
+ require 'easy_manage_client/railtie' if defined?(Rails)
9
+
10
+ module EasyManageClient
11
+ class Error < StandardError; end
12
+ class InvalidResponseContent < Error; end
13
+ class ProfileNotFound < Error; end
14
+ class InvalidExtension < Error; end
15
+ class NoProfilesWereFound < Error; end
16
+ class DownloadProcessFailed < Error; end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'easy_manage_client'
4
+ namespace :easy_manage_client do
5
+ desc 'Download the dump.'
6
+ task :download do
7
+ profile = ENV.fetch('PROFILE', :default).to_sym
8
+ puts "EasyManageClient running for: #{profile}"
9
+ downloader = ::EasyManageClient::Downloader.new(profile: profile)
10
+ puts "EasyManageClient processing for: #{profile}"
11
+ downloader.perform!
12
+ puts "EasyManageClient process successfully completed for: #{profile}"
13
+ end
14
+ end
data/tmp/.keep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_manage_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sertan Gülveren
8
+ - Yiğit Sadıç
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dotenv
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '5.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '5.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '12.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '12.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.79.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.79.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.17.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 0.17.1
84
+ - !ruby/object:Gem::Dependency
85
+ name: webmock
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.8'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.8'
98
+ - !ruby/object:Gem::Dependency
99
+ name: faraday
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 0.11.0
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 0.11.0
112
+ description: It is a simple ruby client for EasyManage API.
113
+ email:
114
+ - sertangulveren@gmail.com
115
+ - yigitsadic@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - easy_manage_client.gemspec
129
+ - lib/easy_manage_client.rb
130
+ - lib/easy_manage_client/configuration.rb
131
+ - lib/easy_manage_client/core.rb
132
+ - lib/easy_manage_client/downloader.rb
133
+ - lib/easy_manage_client/railtie.rb
134
+ - lib/easy_manage_client/version.rb
135
+ - lib/tasks/easy_manage_client.rake
136
+ - tmp/.keep
137
+ homepage: https://github.com/sertangulveren/easy_manage_client
138
+ licenses: []
139
+ metadata:
140
+ homepage_uri: https://github.com/sertangulveren/easy_manage_client
141
+ source_code_uri: https://github.com/sertangulveren/easy_manage_client
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 2.3.0
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.1.2
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: EasyManage API client for ruby.
161
+ test_files: []