zesty 0.1.0

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: 456ce147e22198fe7a14ecffa2d69d896d893e5080bcc4e759781f72674a8f1d
4
+ data.tar.gz: 4b4f730f678a6cb8394f229efe26910541520ed221f37c25848aa50297c0ea17
5
+ SHA512:
6
+ metadata.gz: 89fbe963ddbf3e554941444c0bc02db2910cad6cddf2da38ce4f85c78a3108d97e3a95712a26b3daa91591331270e3a7143cede9ea47eb942a2bcbe1d738da00
7
+ data.tar.gz: 360193b4dcd1b449c36db27209f227ba31f23f470f9e92b19eb73e7cdf93eb0c797c2975701d66250841fcbe902dbd9173b63bdf492e915310fca0ec3fc7612a
data/.env.sample ADDED
@@ -0,0 +1,4 @@
1
+ EMAIL="YOUR_EMAIL"
2
+ PASSWORD="YOUR_PASSWORD"
3
+ INSTANCE_ZUID=""
4
+ MODEL_ZUID=""
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .env
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ # - 2.4.7 # TODO: separate gemfiles to account for ActiveSupport
5
+ - 2.5.6
6
+ - 2.6.4
7
+ before_install:
8
+ - gem update --system
9
+ - gem install bundler
10
+ cache: bundler
11
+ env:
12
+ - EMAIL="email" PASSWORD="password" INSTANCE_ZUID="1-abc-def" MODEL_ZUID="6-d08392dbc9-9gl4bk"
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ Changelog
2
+ =========
3
+
4
+ ## 0.1.0 (2019-09-24)
5
+
6
+ * Initial release to RubyGems
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jjfutbol@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in zesty.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zesty (0.1.0)
5
+ http (~> 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.0.0)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.1, >= 2.1.8)
16
+ addressable (2.7.0)
17
+ public_suffix (>= 2.0.2, < 5.0)
18
+ concurrent-ruby (1.1.5)
19
+ crack (0.4.3)
20
+ safe_yaml (~> 1.0.0)
21
+ diff-lcs (1.3)
22
+ domain_name (0.5.20190701)
23
+ unf (>= 0.0.5, < 1.0.0)
24
+ dotenv (2.7.5)
25
+ hashdiff (1.0.0)
26
+ http (4.1.1)
27
+ addressable (~> 2.3)
28
+ http-cookie (~> 1.0)
29
+ http-form_data (~> 2.0)
30
+ http_parser.rb (~> 0.6.0)
31
+ http-cookie (1.0.3)
32
+ domain_name (~> 0.5)
33
+ http-form_data (2.1.1)
34
+ http_parser.rb (0.6.0)
35
+ i18n (1.6.0)
36
+ concurrent-ruby (~> 1.0)
37
+ minitest (5.12.0)
38
+ public_suffix (4.0.1)
39
+ rake (12.3.3)
40
+ rspec (3.8.0)
41
+ rspec-core (~> 3.8.0)
42
+ rspec-expectations (~> 3.8.0)
43
+ rspec-mocks (~> 3.8.0)
44
+ rspec-core (3.8.2)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-expectations (3.8.4)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.8.0)
49
+ rspec-json_expectations (2.2.0)
50
+ rspec-mocks (3.8.1)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.8.0)
53
+ rspec-support (3.8.2)
54
+ safe_yaml (1.0.5)
55
+ thread_safe (0.3.6)
56
+ tzinfo (1.2.5)
57
+ thread_safe (~> 0.1)
58
+ unf (0.1.4)
59
+ unf_ext
60
+ unf_ext (0.0.7.6)
61
+ vcr (5.0.0)
62
+ webmock (3.7.5)
63
+ addressable (>= 2.3.6)
64
+ crack (>= 0.3.2)
65
+ hashdiff (>= 0.4.0, < 2.0.0)
66
+ zeitwerk (2.1.10)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ activesupport
73
+ bundler (~> 2.0)
74
+ dotenv
75
+ rake (~> 12.0)
76
+ rspec (~> 3.0)
77
+ rspec-json_expectations
78
+ vcr
79
+ webmock
80
+ zesty!
81
+
82
+ BUNDLED WITH
83
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Javier Julio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # The Zesty.io Ruby Gem
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/zesty.svg)][gem]
4
+ [![Build Status](https://travis-ci.org/javierjulio/zesty-rb.svg?branch=master)][travis]
5
+
6
+ A Ruby interface to the Zesty REST API. Requires Ruby 2.4 and up. Not all API actions are supported yet. Since the Zesty API uses mostly camelCase, this gem will handle converting to and from snake_case for you.
7
+
8
+ ## Installation
9
+
10
+ Add the following to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'zesty'
14
+ ```
15
+
16
+ Or install directly with `gem install zesty`.
17
+
18
+ To try out the gem, just follow the Development section instructions as the `bin/setup` script will direct you on how to provide the necessary API info to get started.
19
+
20
+ ## Usage
21
+
22
+ ### Client Configuration
23
+
24
+ Request a valid token by logging in.
25
+
26
+ ```ruby
27
+ token = Zesty::Auth.get_token("email", "password")
28
+ ```
29
+
30
+ Then initialize a client with that token and an instance zuid.
31
+
32
+ ```ruby
33
+ client = Zesty::Client.new(token, "instance_zuid")
34
+ ```
35
+
36
+ ### Making Requests
37
+
38
+ Make an API request with a payload in the structure documented by the Zesty REST API but using snake_case. The request payload will be converted to camelCase for you.
39
+
40
+ ```ruby
41
+ head_tag = client.create_head_tag(
42
+ resource_zuid: "instance_zuid",
43
+ type: "link",
44
+ attributes: {
45
+ rel: :icon,
46
+ href: "favicon.ico",
47
+ "data-manual": true
48
+ },
49
+ sort: 1
50
+ )
51
+ ```
52
+
53
+ So in the example above, `resource_zuid` gets converted to `resourceZUID` in the request payload.
54
+
55
+ ### Handling Responses
56
+
57
+ Response data is in snake_case form as it converted automatically from camelCase. This includes nested objects as shown below:
58
+
59
+ ```ruby
60
+ head_tag = client.get_head_tag("zuid")
61
+ pp head_tag
62
+ # {:_meta=>
63
+ # {:timestamp=>"2019-09-24T16:39:30.571076035Z",
64
+ # :total_results=>1,
65
+ # :start=>0,
66
+ # :offset=>0,
67
+ # :limit=>1},
68
+ # :data=>
69
+ # {:zuid=>"zuid",
70
+ # :type=>"link",
71
+ # :attributes=>
72
+ # {:"data-manual"=>true,
73
+ # :href=>"favicon-64.png",
74
+ # :rel=>"icon",
75
+ # :type=>"image/png"},
76
+ # :resource_zuid=>"resource_zuid",
77
+ # :sort=>1,
78
+ # :created_by_user_zuid=>"created_by_user_zuid",
79
+ # :updated_by_user_zuid=>"created_by_user_zuid",
80
+ # :created_at=>"2019-09-24T16:39:30.561203655Z",
81
+ # :updated_at=>"2019-09-24T16:39:30.561210119Z"}}
82
+ ```
83
+
84
+ ## Development
85
+
86
+ 1. `git clone https://github.com/javierjulio/zesty-rb.git`
87
+ 2. Run `./bin/setup` to install dependencies and fill out API info
88
+ 3. Run `./bin/console` for an interactive prompt with an authenticated client for you to experiment:
89
+
90
+ ```ruby
91
+ instance = client.get_instance
92
+ instance[:data][:name]
93
+ # => My Test Instance
94
+ ```
95
+
96
+ All code is written in snake_case since requests and responses are converted to and from camelCase for you.
97
+
98
+ ### Tests
99
+
100
+ Run `rspec` or to re-record VCR cassettes use `RECORD_MODE=all rspec`.
101
+
102
+ ### Releasing
103
+
104
+ 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).
105
+
106
+ ## Contributing
107
+
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/javierjulio/zesty-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
113
+
114
+ ## Code of Conduct
115
+
116
+ Everyone interacting in the Zesty project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/javierjulio/zesty-rb/blob/master/CODE_OF_CONDUCT.md).
117
+
118
+ [gem]: https://rubygems.org/gems/zesty
119
+ [travis]: https://travis-ci.org/javierjulio/zesty-rb
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dotenv/load"
5
+ require "zesty"
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
+ def client
11
+ @client ||= begin
12
+ puts "Signing in..."
13
+ token = Zesty::Auth.get_token(ENV["EMAIL"], ENV["PASSWORD"])
14
+ puts "Client authenticated."
15
+ Zesty::Client.new(token, ENV["INSTANCE_ZUID"])
16
+ end
17
+ end
18
+
19
+ # (If you use this, don't forget to add pry to your Gemfile!)
20
+ # require "pry"
21
+ # Pry.start
22
+
23
+ require "irb"
24
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ echo "Enter your Zesty account credentials below."
8
+ read -p 'Email: ' email
9
+ read -p 'Password: ' password
10
+
11
+ cp .env.sample .env
12
+ sed -i '' -e "s/YOUR_EMAIL/$email/g" .env
13
+ sed -i '' -e "s/YOUR_PASSWORD/$password/g" .env
data/lib/zesty/auth.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Zesty
2
+ class Auth
3
+
4
+ AUTH_URL = "https://svc.zesty.io/auth"
5
+
6
+ using Refinements::SnakeCase
7
+
8
+ def self.get_token(email, password)
9
+ self.new.login(email, password).dig(:meta, :token)
10
+ end
11
+
12
+ def initialize(**options)
13
+ @options = options
14
+ end
15
+
16
+ def login(email, password)
17
+ Request.post_form(
18
+ "#{AUTH_URL}/login",
19
+ params: {
20
+ email: email,
21
+ password: password
22
+ }
23
+ )
24
+ end
25
+
26
+ def verify_token(token)
27
+ Request.get("#{AUTH_URL}/verify", headers: { Authorization: "Bearer #{token}" })
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,106 @@
1
+ module Zesty
2
+ class Client
3
+
4
+ API_URLS = {
5
+ get_instance: "https://accounts.api.zesty.io/v1/instances/%{instance_zuid}",
6
+ get_models: "https://%{instance_zuid}.api.zesty.io/v1/content/models",
7
+ get_model: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}",
8
+ get_items: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items",
9
+ get_item: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items/%{item_zuid}",
10
+ update_item: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items/%{item_zuid}",
11
+ get_head_tags: "https://%{instance_zuid}.api.zesty.io/v1/web/headtags",
12
+ create_head_tag: "https://%{instance_zuid}.api.zesty.io/v1/web/headtags",
13
+ delete_head_tag: "https://%{instance_zuid}.api.zesty.io/v1/web/headtags/%{head_tag_zuid}"
14
+ }
15
+
16
+ using Refinements::CamelCase
17
+
18
+ attr_reader :instance_zuid
19
+
20
+ def initialize(token, instance_zuid, **options)
21
+ @token = token
22
+ @instance_zuid = instance_zuid
23
+ @options = options
24
+ end
25
+
26
+ def get_instance
27
+ Request.get(url_for(:get_instance), headers: { authorization: "Bearer #{@token}" })
28
+ end
29
+
30
+ def get_models
31
+ Request.get(url_for(:get_models), headers: { authorization: "Bearer #{@token}" })
32
+ end
33
+
34
+ def get_model(model_zuid)
35
+ Request.get(url_for(:get_model, model_zuid: model_zuid), headers: { authorization: "Bearer #{@token}" })
36
+ end
37
+
38
+ def get_items(model_zuid)
39
+ Request.get(url_for(:get_items, model_zuid: model_zuid), headers: { authorization: "Bearer #{@token}" })
40
+ end
41
+
42
+ def get_item(model_zuid, item_zuid)
43
+ Request.get(url_for(:get_item, model_zuid: model_zuid, item_zuid: item_zuid), headers: { authorization: "Bearer #{@token}" })
44
+ end
45
+
46
+ def update_item(model_zuid, item_zuid, publish: false, data:, meta:, web: {})
47
+ # TODO: include `publish: publish` in `url_for` line once API is announced
48
+ # TODO: add `?publish=%{publish}` to API_URLS `:update_item`
49
+ Request.put(
50
+ url_for(:update_item, model_zuid: model_zuid, item_zuid: item_zuid), # publish: publish),
51
+ params: {
52
+ data: data,
53
+ meta: meta.to_camel_case,
54
+ web: web.to_camel_case
55
+ },
56
+ headers: {
57
+ authorization: "Bearer #{@token}"
58
+ }
59
+ )
60
+ end
61
+
62
+ # [LEGACY] This will be replaced by `update_item(... publish: true)`
63
+ def publish_item(model_zuid, item_zuid, version_number)
64
+ Request.post(
65
+ "https://svc.zesty.io/sites-service/#{instance_zuid}/content/items/#{item_zuid}/publish-schedule",
66
+ params: {
67
+ version_num: version_number
68
+ },
69
+ headers: {
70
+ authorization: "Bearer #{@token}",
71
+ 'X-Auth': @token
72
+ }
73
+ )
74
+ end
75
+
76
+ def get_head_tags
77
+ Request.get(url_for(:get_head_tags), headers: { authorization: "Bearer #{@token}" })
78
+ end
79
+
80
+ def create_head_tag(type:, attributes:, sort:, resource_zuid:)
81
+ Request.post(
82
+ url_for(:create_head_tag),
83
+ params: {
84
+ type: type,
85
+ attributes: attributes,
86
+ sort: sort,
87
+ resource_zuid: resource_zuid
88
+ }.to_camel_case,
89
+ headers: {
90
+ authorization: "Bearer #{@token}"
91
+ }
92
+ )
93
+ end
94
+
95
+ def delete_head_tag(head_tag_zuid)
96
+ Request.delete(url_for(:delete_head_tag, head_tag_zuid: head_tag_zuid), headers: { authorization: "Bearer #{@token}" })
97
+ end
98
+
99
+ private
100
+
101
+ def url_for(name, **data)
102
+ format(API_URLS.fetch(name), data.merge(instance_zuid: instance_zuid))
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,38 @@
1
+ module Zesty
2
+ class Error < StandardError
3
+
4
+ attr_reader :code, :response_body, :response_headers
5
+
6
+ class << self
7
+ def from_response(data, response)
8
+ message = parse_error(data) || response.to_s
9
+ new(message, response.code, response.to_s, response.headers.to_h)
10
+ end
11
+
12
+ private
13
+
14
+ def parse_error(data)
15
+ if data.is_a?(Hash)
16
+ data.dig(:message) || data.dig(:error)
17
+ end
18
+ end
19
+ end
20
+
21
+ def initialize(message, code, response_body, response_headers)
22
+ super(message)
23
+ @code = code
24
+ @response_body = response_body
25
+ @response_headers = response_headers
26
+ end
27
+
28
+ def to_h
29
+ {
30
+ code: code,
31
+ message: message,
32
+ response_body: response_body,
33
+ response_headers: response_headers
34
+ }
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ module Zesty
2
+ module Refinements
3
+ module CamelCase
4
+ refine Hash do
5
+
6
+ def to_camel_case(data = self)
7
+ case data
8
+ when Array
9
+ data.map { |value| to_camel_case(value) }
10
+ when Hash
11
+ data.map { |key, value| [camel_case_key(key), to_camel_case(value)] }.to_h
12
+ else
13
+ data
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def camel_case_key(key)
20
+ case key
21
+ when Symbol
22
+ camel_case(key.to_s).to_sym
23
+ when String
24
+ camel_case(key).to_sym
25
+ else
26
+ key
27
+ end
28
+ end
29
+
30
+ def camel_case(string)
31
+ @acronyms ||= { 'id' => 'ID', 'zuid' => 'ZUID' }
32
+ @acronym_regex ||= /#{@acronyms.values.join("|")}/
33
+ @acronyms_camelize_regex ||= /^(?:#{@acronym_regex}(?=\b|[A-Z_])|\w)/
34
+ @acronyms_underscore_regex ||= /(?:(?<=([A-Za-z\d]))|\b)(#{@acronym_regex})(?=\b|[^a-z])/
35
+
36
+ result = string.sub(/^[a-z\d]*/) { |match| @acronyms[match] || match }
37
+
38
+ if !result.start_with?("_") # e.g. ignore "_meta", "_meta_title", etc.
39
+ result = result.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{@acronyms[$2] || $2.capitalize}" }
40
+ end
41
+
42
+ result
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ module Zesty
2
+ module Refinements
3
+ module SnakeCase
4
+ refine Hash do
5
+
6
+ def to_snake_case(data = self)
7
+ case data
8
+ when Array
9
+ data.map { |value| to_snake_case(value) }
10
+ when Hash
11
+ data.map { |key, value| [underscore_key(key), to_snake_case(value)] }.to_h
12
+ else
13
+ data
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def underscore_key(key)
20
+ case key
21
+ when Symbol
22
+ underscore(key.to_s).to_sym
23
+ when String
24
+ underscore(key).to_sym
25
+ else
26
+ key
27
+ end
28
+ end
29
+
30
+ def underscore(string)
31
+ @__memoize_underscore ||= {}
32
+ return @__memoize_underscore[string] if @__memoize_underscore[string]
33
+ @__memoize_underscore[string] =
34
+ string.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
35
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
36
+ .downcase
37
+ @__memoize_underscore[string]
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,85 @@
1
+ module Zesty
2
+ class Request
3
+
4
+ using Refinements::SnakeCase
5
+
6
+ attr_reader :request_method, :url, :params, :headers
7
+
8
+ def self.get(url, params: nil, headers: {})
9
+ Request.new(:get, url, params, headers).perform
10
+ end
11
+
12
+ def self.post_form(url, params: {}, headers: {})
13
+ Request.new(:post_form, url, params, headers).perform
14
+ end
15
+
16
+ def self.post(url, params: {}, headers: {})
17
+ Request.new(:post, url, params, headers).perform
18
+ end
19
+
20
+ def self.put(url, params: {}, headers: {})
21
+ Request.new(:put, url, params, headers).perform
22
+ end
23
+
24
+ def self.delete(url, params: {}, headers: {})
25
+ Request.new(:delete, url, params, headers).perform
26
+ end
27
+
28
+ def initialize(request_method, url, params, headers)
29
+ @request_method = request_method
30
+ @url = url
31
+ @params = params
32
+ @headers = headers
33
+ end
34
+
35
+ def perform
36
+ response = send(@request_method)
37
+ process_response(response)
38
+ end
39
+
40
+ private
41
+
42
+ def delete
43
+ http_client.delete(url, params: params)
44
+ end
45
+
46
+ def get
47
+ http_client.get(url, body: params)
48
+ end
49
+
50
+ def post_form
51
+ http_client.post(url, form: params.transform_values { |v| HTTP::FormData::Part.new(v) })
52
+ end
53
+
54
+ def post
55
+ http_client.post(url, json: params)
56
+ end
57
+
58
+ def put
59
+ http_client.put(url, json: params)
60
+ end
61
+
62
+ def http_client
63
+ HTTP.headers({ accept: "application/json" }.merge(headers))
64
+ end
65
+
66
+ def process_response(response)
67
+ data = parse_response_body(response.to_s)
68
+
69
+ if response.status.success?
70
+ data
71
+ else
72
+ raise Error.from_response(data, response)
73
+ end
74
+ end
75
+
76
+ def parse_response_body(body)
77
+ return nil if body.strip.empty?
78
+ json = JSON.parse(body, symbolize_names: true)
79
+ #pp json
80
+ json&.to_snake_case
81
+ rescue JSON::ParserError
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Zesty
2
+ VERSION = "0.1.0"
3
+ end
data/lib/zesty.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "http"
2
+ require "zesty/version"
3
+ require "zesty/refinements/camel_case"
4
+ require "zesty/refinements/snake_case"
5
+ require "zesty/error"
6
+ require "zesty/request"
7
+ require "zesty/auth"
8
+ require "zesty/client"
9
+
10
+ module Zesty
11
+ end
data/zesty.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "zesty/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "zesty"
7
+ spec.version = Zesty::VERSION
8
+ spec.authors = ["Javier Julio"]
9
+ spec.email = ["jjfutbol@gmail.com"]
10
+
11
+ spec.summary = %q{A Ruby interface to the Zesty.io API}
12
+ spec.description = %q{A Ruby interface to the Zesty.io API}
13
+ spec.homepage = "https://github.com/javierjulio/zesty-rb"
14
+ spec.license = "MIT"
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "https://github.com/javierjulio/zesty-rb/blob/master/CHANGELOG.md"
21
+ spec.metadata["bug_tracker_uri"] = "https://github.com/javierjulio/zesty-rb/issues"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.required_ruby_version = ">= 2.4.0"
33
+
34
+ spec.add_dependency "http", "~> 4.0"
35
+
36
+ spec.add_development_dependency "bundler", "~> 2.0"
37
+ spec.add_development_dependency "rake", "~> 12.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "rspec-json_expectations"
40
+ spec.add_development_dependency "dotenv"
41
+ spec.add_development_dependency "vcr"
42
+ spec.add_development_dependency "webmock"
43
+ spec.add_development_dependency "activesupport"
44
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zesty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Julio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-json_expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
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: vcr
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
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: A Ruby interface to the Zesty.io API
140
+ email:
141
+ - jjfutbol@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".env.sample"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - CHANGELOG.md
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - lib/zesty.rb
160
+ - lib/zesty/auth.rb
161
+ - lib/zesty/client.rb
162
+ - lib/zesty/error.rb
163
+ - lib/zesty/refinements/camel_case.rb
164
+ - lib/zesty/refinements/snake_case.rb
165
+ - lib/zesty/request.rb
166
+ - lib/zesty/version.rb
167
+ - zesty.gemspec
168
+ homepage: https://github.com/javierjulio/zesty-rb
169
+ licenses:
170
+ - MIT
171
+ metadata:
172
+ homepage_uri: https://github.com/javierjulio/zesty-rb
173
+ source_code_uri: https://github.com/javierjulio/zesty-rb
174
+ changelog_uri: https://github.com/javierjulio/zesty-rb/blob/master/CHANGELOG.md
175
+ bug_tracker_uri: https://github.com/javierjulio/zesty-rb/issues
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: 2.4.0
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubygems_version: 3.0.4
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: A Ruby interface to the Zesty.io API
195
+ test_files: []