bookable_cloud 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5a54c363b733effe51c189dd32d1cebada7c196c961ec970786da5e79126e242
4
+ data.tar.gz: 0b963df79596c504be9895f3e9aa4d93f0e94d00bfa21e1b9da4b7460e53598d
5
+ SHA512:
6
+ metadata.gz: e125320716aa2b652fede7ed4d2186b6613034107bac70c1643cd11e48e4ad30d51e485bb4c439e0da25c47818a451a5433e7aa76d7af68972d93af6589a47f1
7
+ data.tar.gz: 2ffbfc5850ccacf80a0f8b63125600de040f0121019b781539c2dacd6ef99b814b8d92e317d5dd1859a61c213122822f73408151890568df54bfa0af22c7f767
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ example/
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bookable_cloud'
4
+
5
+ BookableCloud::Cli.start(ARGV)
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bookable_cloud/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bookable_cloud"
7
+ s.version = BookableCloud::VERSION
8
+ s.authors = ["Adam Beynon"]
9
+ s.email = ["adam.beynon@gmail.com"]
10
+ s.summary = %q{Bookable Cloud Theme Editor}
11
+ s.description = %q{Bookable Cloud editor}
12
+ s.license = 'MIT'
13
+
14
+ s.add_dependency('thor', '>= 0.14.4')
15
+ s.add_dependency('httparty', '~> 0.13.0')
16
+ s.add_dependency('json', '~> 1.8.0')
17
+ s.add_dependency('mimemagic')
18
+ s.add_dependency('listen', '~> 3.0')
19
+ s.add_dependency('launchy')
20
+
21
+ s.add_development_dependency 'rake'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ['lib']
27
+ end
@@ -0,0 +1,7 @@
1
+ require 'bookable_cloud/cli'
2
+ require 'bookable_cloud/api_client'
3
+ require 'bookable_cloud/version'
4
+
5
+ module BookableCloud
6
+
7
+ end
@@ -0,0 +1,95 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module BookableCloud
5
+ class ApiClient
6
+ include Thor::Actions
7
+
8
+ # class Response
9
+ # attr_reader :json, :success
10
+ #
11
+ # def initialize(success: true, json: nil)
12
+ # @success = success
13
+ # @json = json
14
+ # end
15
+ # end
16
+
17
+ def initialize(config)
18
+ @config = config
19
+ end
20
+
21
+ def tenant_url
22
+ @config["tenant_url"]
23
+ end
24
+
25
+ def base_url
26
+ tenant_url.to_s + "/admin"
27
+ end
28
+
29
+ def get_theme
30
+ uri = URI.parse(base_url + "/themes/#{@config["theme_id"]}.json")
31
+
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ http.use_ssl = true
34
+
35
+ request = Net::HTTP::Get.new(uri.request_uri)
36
+ request["Authorization"] = "Token token=#{@config["access_token"]}"
37
+
38
+ http.request(request)
39
+ end
40
+
41
+ def list_assets
42
+ json = get_json "/themes/#{@config["theme_id"]}/assets.json"
43
+
44
+ if assets = json["assets"]
45
+ assets.collect { |asset| asset["key"] }
46
+ end
47
+ end
48
+
49
+ def get_asset(asset)
50
+ json = get_json "/themes/#{@config["theme_id"]}/assets.json", params: { key: asset }
51
+
52
+ json["asset"]
53
+ end
54
+
55
+ def update_asset(key, body)
56
+ json = put_json "/themes/#{@config["theme_id"]}/assets.json", params: {
57
+ "asset[key]" => key,
58
+ "asset[value]" => body
59
+ }
60
+ end
61
+
62
+ private
63
+
64
+ def get_json(url, params: nil)
65
+ uri = URI.parse(base_url + url)
66
+ uri.query = URI.encode_www_form(params) if params
67
+
68
+ http = Net::HTTP.new(uri.host, uri.port)
69
+ http.use_ssl = true
70
+
71
+ request = Net::HTTP::Get.new(uri.request_uri)
72
+ request["Authorization"] = "Token token=#{@config["access_token"]}"
73
+
74
+ response = http.request(request)
75
+
76
+ JSON.parse(response.body)
77
+ end
78
+
79
+ def put_json(url, params: nil)
80
+ uri = URI.parse(base_url + url)
81
+
82
+ http = Net::HTTP.new(uri.host, uri.port)
83
+ http.use_ssl = true
84
+
85
+ request = Net::HTTP::Put.new(uri.request_uri)
86
+ request.set_form_data(params) if params
87
+ request["Authorization"] = "Token token=#{@config["access_token"]}"
88
+
89
+ response = http.request(request)
90
+
91
+ # p response.body
92
+ JSON.parse(response.body)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,119 @@
1
+ require 'thor'
2
+ require 'yaml'
3
+ require 'base64'
4
+ require 'fileutils'
5
+ require 'json'
6
+ require 'listen'
7
+ require 'launchy'
8
+
9
+ module BookableCloud
10
+ class Cli < Thor
11
+ include Thor::Actions
12
+
13
+ desc "check", "check configuration"
14
+ def check
15
+ check_configuration!
16
+ end
17
+
18
+ desc "fetch", "download current assets for theme"
19
+ def fetch
20
+ check_configuration!
21
+
22
+ assets = api_client.list_assets
23
+
24
+ assets.each do |key|
25
+ fetch_asset(key)
26
+ log "Fetched: #{key}"
27
+ end
28
+ # say("Done.", :green) unless options['quiet']
29
+ end
30
+
31
+ desc "watch", "watch and upload changes"
32
+ def watch
33
+ check_configuration!
34
+
35
+ log "Watching directory: #{Dir.pwd}"
36
+ Listen.to(Dir.pwd) do |modified, added, removed|
37
+ modified.each do |filename|
38
+ filename = filename.gsub("#{Dir.pwd}/", '')
39
+
40
+ next if File.directory?(filename)
41
+
42
+ upload_asset filename
43
+ end
44
+ end.start
45
+ sleep
46
+ end
47
+
48
+ private
49
+
50
+ def check_configuration!
51
+ is_success = api_client.get_theme.is_a?(Net::HTTPSuccess)
52
+
53
+ if is_success
54
+ log "Theme works", color: :green
55
+ else
56
+ log "Theme doesnt work", color: :red
57
+ exit(1)
58
+ end
59
+ end
60
+
61
+ def upload_asset(key)
62
+ log "Uploading #{key}", color: :green
63
+ content = File.read(key)
64
+
65
+ api_client.update_asset key, content
66
+ end
67
+
68
+ def fetch_asset(key)
69
+ asset = api_client.get_asset(key)
70
+ # puts asset
71
+ # puts asset
72
+
73
+ if asset['value']
74
+ # # For CRLF line endings
75
+ content = asset['value'].gsub("\r", "")
76
+ format = "w"
77
+ elsif asset['attachment']
78
+ content = Base64.decode64(asset['attachment'])
79
+ format = "w+b"
80
+ end
81
+
82
+ FileUtils.mkdir_p(File.dirname(key))
83
+ File.open(key, format) {|f| f.write content} if content
84
+ end
85
+
86
+ def log(text, color: nil)
87
+ text = case color
88
+ when :black
89
+ "\033[30m#{text}\033[0m"
90
+ when :red
91
+ "\033[31m#{text}\033[0m"
92
+ when :green
93
+ "\033[32m#{text}\033[0m"
94
+ when :brown
95
+ "\033[33m#{text}\033[0m"
96
+ when :brown
97
+ "\033[34m#{text}\033[0m"
98
+ when :magenta
99
+ "\033[35m#{text}\033[0m"
100
+ when :cyan
101
+ "\033[36m#{text}\033[0m"
102
+ when :gray
103
+ "\033[37m#{text}\033[0m"
104
+ else
105
+ text
106
+ end
107
+
108
+ puts text
109
+ end
110
+
111
+ def api_client
112
+ @api_client ||= ApiClient.new(config)
113
+ end
114
+
115
+ def config
116
+ @config ||= YAML.load_file 'config.yml'
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module BookableCloud
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bookable_cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Beynon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.13.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mimemagic
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: listen
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: launchy
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Bookable Cloud editor
112
+ email:
113
+ - adam.beynon@gmail.com
114
+ executables:
115
+ - bookable_cloud
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - bin/bookable_cloud
122
+ - bookable_cloud.gemspec
123
+ - lib/bookable_cloud.rb
124
+ - lib/bookable_cloud/api_client.rb
125
+ - lib/bookable_cloud/cli.rb
126
+ - lib/bookable_cloud/version.rb
127
+ homepage:
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.3
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Bookable Cloud Theme Editor
151
+ test_files: []