tilda-simple-api 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d296dba5753bd820993d7796198cfc47553dcfbd
4
- data.tar.gz: f0dcda9e4013c58344cc0e14364943cd36aa1b93
3
+ metadata.gz: 1c08f2b15626300a8c3ca48e18ece75485affdad
4
+ data.tar.gz: 2f64a02a11f40b8365bcaa4f172d57b1fdab541c
5
5
  SHA512:
6
- metadata.gz: cd2d4df301ded5090304734e39758efc3743a00709d02f076f7236c80988d593b383c6600241127d4b995d4096da3bbbcdbce112941fd9a2def78a510bbae6a0
7
- data.tar.gz: 33169015ae087a89461fa91f66d0cd1961bae3b522385e869190a875ea67c0e300dc9bf6815c84a8f46eb48a87d7a6958fc2a0e07f5c4b13006f6e52245845c3
6
+ metadata.gz: ec265f4edfed9ccf4c7cd47aeae0a7ae35572c97efb5a8239c74241189a9bbf6ed7040ab6ee65814760293ebfc7d01fe7a2cf977cb0ebfe0a529defaef9cd563
7
+ data.tar.gz: e72884ae0d2b43d12e838c8e1bd77f6b9f019c69d7a257d7272ac699cdb055333794fc1b10909f16301d96b60565525f96e207e54a63b6d2270c72b26bdf1a52
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Tilda::Simple::Api
2
+ [![Gem Version](https://badge.fury.io/rb/tilda-simple-api.svg)](https://badge.fury.io/rb/tilda-simple-api)
2
3
  [![Build Status](https://travis-ci.org/gururuby/tilda-simple-api.svg?branch=master)](https://travis-ci.org/gururuby/tilda-simple-api)
3
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/f9b19b95e9b50be1a52f/maintainability)](https://codeclimate.com/github/gururuby/tilda-simple-api/maintainability)
4
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/f9b19b95e9b50be1a52f/test_coverage)](https://codeclimate.com/github/gururuby/tilda-simple-api/test_coverage)
@@ -31,6 +32,7 @@ Configuration api:
31
32
  config.secret_key = "your-secret-key"
32
33
  # config.api_version = "v1" # by default'
33
34
  # config.api_host = "http://api.tildacdn.info" # by default'
35
+ # config.raise_api_errors = false # by_default
34
36
  end
35
37
  ```
36
38
 
@@ -55,6 +57,17 @@ API requests example
55
57
  | **page info for export(only body)** | `tilda_api.page_export(page_id)` | http://help.tilda.ws/api#getpageexport |
56
58
  | **full page info for export** | `tilda_api.page_full_export(page_id)` | http://help.tilda.ws/api#getpagefullexport |
57
59
 
60
+ ### API Errors
61
+ When switch `config.raise_api_errors` to `true`. API raise errors. Here is available API Errors:
62
+
63
+ | API Error | Error case |
64
+ | --------------------------------------------------|-------------------------------------------------|
65
+ | **Tilda::Simple::Api::Errors::InvalidCredentials**| invalid public_key or secret_key passed |
66
+ | **Tilda::Simple::Api::Errors::ProjectNotFound** | tilda api not found project by passed project_id|
67
+ | **Tilda::Simple::Api::Errors::PageNotFound** | tilda api not found page by passed page_id |
68
+ | **Tilda::Simple::Api::Errors::Error** | in all other cases |
69
+
70
+
58
71
  ## Contributing
59
72
 
60
73
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tilda-simple-api. 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.
@@ -1,6 +1,7 @@
1
1
  require 'httparty'
2
2
  require 'tilda/simple/api/config'
3
3
  require 'tilda/simple/api/errors'
4
+ require 'tilda/simple/api/errors_handler'
4
5
  require 'tilda/simple/api/request'
5
6
  require 'tilda/simple/api/version'
6
7
 
@@ -2,11 +2,12 @@ module Tilda
2
2
  module Simple
3
3
  module Api
4
4
  class Config
5
- attr_accessor :api_host, :api_version, :public_key, :secret_key
5
+ attr_accessor :api_host, :api_version, :public_key, :secret_key, :raise_api_errors
6
6
 
7
7
  def initialize
8
8
  @api_version = 'v1'
9
9
  @api_host = 'http://api.tildacdn.info'
10
+ @raise_api_errors = false
10
11
  end
11
12
  end
12
13
  end
@@ -3,15 +3,9 @@ module Tilda
3
3
  module Api
4
4
  module Errors
5
5
  class Error < StandardError; end
6
-
7
- class ResponseError < Error
8
- attr_reader :status_code
9
-
10
- def initialize(response)
11
- @message = response.message
12
- @status_code = response.code
13
- end
14
- end
6
+ class InvalidCredentials < Error; end
7
+ class ProjectNotFound < Error; end
8
+ class PageNotFound < Error; end
15
9
  end
16
10
  end
17
11
  end
@@ -0,0 +1,36 @@
1
+ module Tilda
2
+ module Simple
3
+ module Api
4
+ module ErrorsHandler
5
+ def get(path, params = {})
6
+ handle_errors { super(path, params) }
7
+ end
8
+
9
+ private
10
+
11
+ def handle_errors(&block)
12
+ response = block.call
13
+ check_response(response) if Api.config.raise_api_errors
14
+ response
15
+ end
16
+
17
+ def check_response(response)
18
+ raise api_error_class(response), response['message'] if response['status'] == 'ERROR'
19
+ end
20
+
21
+ def api_error_class(response)
22
+ case response['message']
23
+ when /Wrong Secret Key length|Wrong Public Key length/
24
+ Errors::InvalidCredentials
25
+ when /Can't find project with this 'projectid' parameter/
26
+ Errors::ProjectNotFound
27
+ when /Can't find page with this 'pageid' parameter/
28
+ Errors::PageNotFound
29
+ else
30
+ Errors::Error
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -2,52 +2,49 @@ module Tilda
2
2
  module Simple
3
3
  module Api
4
4
  class Request
5
- class << self
6
- def new(public_key:nil, secret_key:nil)
7
- Class.new(AbstractRequest){|klass|
8
- config = Api.config
9
- public_key ||= config.public_key
10
- secret_key ||= config.secret_key
11
- klass.base_uri "#{config.api_host}/#{config.api_version}"
12
- klass.default_params publickey: public_key, secretkey: secret_key
13
- }.new
14
- end
15
- end
16
-
17
- class AbstractRequest
18
- include HTTParty
19
-
20
- def projects_list
21
- self.class.get('/getprojectslist')
22
- end
23
-
24
- def project(project_id)
25
- self.class.get('/getproject', query: { projectid: project_id })
26
- end
27
-
28
- def project_export(project_id)
29
- self.class.get('/getprojectexport', query: { projectid: project_id })
30
- end
31
-
32
- def pages_list(project_id)
33
- self.class.get('/getpageslist', query: { projectid: project_id })
34
- end
35
-
36
- def page(page_id)
37
- self.class.get('/getpage', query: { pageid: page_id })
38
- end
39
-
40
- def page_full(page_id)
41
- self.class.get('/getpagefull', query: { pageid: page_id })
42
- end
43
-
44
- def page_export(page_id)
45
- self.class.get('/getpageexport', query: { pageid: page_id })
46
- end
47
-
48
- def page_full_export(page_id)
49
- self.class.get('/getpagefullexport', query: { pageid: page_id })
50
- end
5
+ include HTTParty
6
+ extend ErrorsHandler
7
+
8
+ def initialize(public_key: nil, secret_key: nil)
9
+ config = Api.config
10
+
11
+ public_key ||= config.public_key
12
+ secret_key ||= config.secret_key
13
+
14
+ self.class.base_uri "#{config.api_host}/#{config.api_version}"
15
+ self.class.default_params publickey: public_key, secretkey: secret_key
16
+ end
17
+
18
+ def projects_list
19
+ self.class.get('/getprojectslist')
20
+ end
21
+
22
+ def project(project_id)
23
+ self.class.get('/getproject', query: { projectid: project_id })
24
+ end
25
+
26
+ def project_export(project_id)
27
+ self.class.get('/getprojectexport', query: { projectid: project_id })
28
+ end
29
+
30
+ def pages_list(project_id)
31
+ self.class.get('/getpageslist', query: { projectid: project_id })
32
+ end
33
+
34
+ def page(page_id)
35
+ self.class.get('/getpage', query: { pageid: page_id })
36
+ end
37
+
38
+ def page_full(page_id)
39
+ self.class.get('/getpagefull', query: { pageid: page_id })
40
+ end
41
+
42
+ def page_export(page_id)
43
+ self.class.get('/getpageexport', query: { pageid: page_id })
44
+ end
45
+
46
+ def page_full_export(page_id)
47
+ self.class.get('/getpagefullexport', query: { pageid: page_id })
51
48
  end
52
49
  end
53
50
  end
@@ -1,7 +1,7 @@
1
1
  module Tilda
2
2
  module Simple
3
3
  module Api
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.bindir = "exe"
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
+ spec.required_ruby_version = '>= 1.9.3'
31
32
 
32
33
  spec.add_development_dependency "bundler", "~> 1.16"
33
34
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tilda-simple-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - V. Miroshnichenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-12 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,7 @@ files:
114
114
  - lib/tilda/simple/api.rb
115
115
  - lib/tilda/simple/api/config.rb
116
116
  - lib/tilda/simple/api/errors.rb
117
+ - lib/tilda/simple/api/errors_handler.rb
117
118
  - lib/tilda/simple/api/request.rb
118
119
  - lib/tilda/simple/api/version.rb
119
120
  - tilda-simple-api.gemspec
@@ -130,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
132
  - - ">="
132
133
  - !ruby/object:Gem::Version
133
- version: '0'
134
+ version: 1.9.3
134
135
  required_rubygems_version: !ruby/object:Gem::Requirement
135
136
  requirements:
136
137
  - - ">="