mt-data_api-client 0.0.1

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
+ SHA1:
3
+ metadata.gz: 60f553e01389e5e637f74cced0c94bc6e94c729e
4
+ data.tar.gz: 557cdbee8d24f38513e4be8ea367a1907927da3b
5
+ SHA512:
6
+ metadata.gz: 9554835dd7dee9e5ea4f91669ac7917517817452141621c3f6a1c71d1991525b944ed804ccd153383baa1c80a84375819eb36d774f756a0e4f8ebb9b65b059df
7
+ data.tar.gz: 6a0df84562a608fdcaadb8981e55ed7a75cf807c801712ebb3ed1927647be5f152266b501a9d97148afe9021788418161cbd5bd4f3ef5b4182b6c97cb0b28599
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --warnings
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.0
5
+ - 2.1.10
6
+ - 2.0.0
7
+ before_install:
8
+ - gem update bundler
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mt-data_api.gemspec
4
+ gemspec
5
+
6
+ if Gem::Version.create(RUBY_VERSION) < Gem::Version.create('2.2.2')
7
+ gem 'activesupport', '>= 4.2.7.1', '< 5.0.0'
8
+ end
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # MT::DataAPI::Client
2
+
3
+ Movable Type Data API client for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mt-data_api-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mt-data_api-client
20
+
21
+ ## Usage
22
+
23
+ client = MT::DataAPI::Client.new(
24
+ base_url: 'http://localhost/mt/mt-data-api.cgi',
25
+ client_id: 'mt-ruby'
26
+ )
27
+ json = client.call(:list_entries, site_id: 1)
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mt-data_api.
38
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :spec
4
+
5
+ desc 'run specs'
6
+ task(:spec) { ruby '-S rspec spec' }
7
+
8
+ task :test => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mt/data_api/client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
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
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.3.1
@@ -0,0 +1,33 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+
3
+ require 'mt/data_api/client/endpoint_manager'
4
+ require 'mt/data_api/client/version'
5
+
6
+ module MT
7
+ module DataAPI
8
+ # Movable Type Data API client for Ruby.
9
+ class Client
10
+ attr_accessor :access_token
11
+
12
+ def initialize(opts)
13
+ opts = opts.symbolize_keys
14
+ @access_token = opts.delete(:access_token)
15
+ @endpoint_manager = EndpointManager.new(opts)
16
+ end
17
+
18
+ def call(id, args = {})
19
+ id = id.to_s
20
+ endpoint = @endpoint_manager.find_endpoint(id)
21
+ raise "no endpoint: #{id}" unless endpoint
22
+ res = endpoint.call(@access_token, args)
23
+ @access_token = res['accessToken'] if id == 'authenticate'
24
+ @endpoint_manager.endpoints = res['items'] if id == 'list_endpoints'
25
+ res
26
+ end
27
+
28
+ def endpoints
29
+ @endpoint_manager.endpoints
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module MT
5
+ module DataAPI
6
+ class Client
7
+ # Send request to endpoint
8
+ class APIRequest
9
+ def initialize(endpoint)
10
+ @endpoint = endpoint
11
+ end
12
+
13
+ def send(access_token, args)
14
+ uri = URI.parse(@endpoint.request_url(args))
15
+ net_http(uri).start do |http|
16
+ http.request(request(uri, access_token, args))
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def net_http(uri)
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ http.use_ssl = true if uri.scheme == 'https'
25
+ http
26
+ end
27
+
28
+ def request(uri, access_token, args)
29
+ req = http_request_class.new(uri)
30
+ if access_token
31
+ req['X-MT-Authorization'] = "MTAuth accessToken=#{access_token}"
32
+ end
33
+ req.set_form_data(args) if post_or_put?
34
+ req
35
+ end
36
+
37
+ def http_request_class
38
+ Object.const_get("Net::HTTP::#{@endpoint.verb.capitalize}")
39
+ end
40
+
41
+ def post_or_put?
42
+ %w(POST PUT).include? @endpoint.verb
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+
3
+ require 'active_support/core_ext/hash/keys'
4
+
5
+ require 'mt/data_api/client/api_request'
6
+
7
+ module MT
8
+ module DataAPI
9
+ class Client
10
+ # Send request to endpoint.
11
+ class Endpoint
12
+ class << self
13
+ attr_writer :api_url
14
+ end
15
+
16
+ attr_reader :verb
17
+
18
+ def initialize(hash)
19
+ raise ArgumentError, 'parameter should be hash' unless hash.is_a? Hash
20
+ hash = hash.symbolize_keys
21
+
22
+ @id = hash[:id]
23
+ @route = hash[:route]
24
+ @version = hash[:version]
25
+ @verb = hash[:verb]
26
+
27
+ raise ArgumentError, "invalid verb: #{@verb}" unless valid_verb?
28
+ end
29
+
30
+ def call(access_token = nil, args = {})
31
+ res = APIRequest.new(self).send(access_token, args)
32
+ raise "#{res.code}: #{res.message}" unless res.code == '200'
33
+ JSON.parse(res.body)
34
+ end
35
+
36
+ def request_url(args)
37
+ url = self.class.instance_variable_get(:@api_url) + route(args)
38
+ url += query_string(args) if @verb == 'GET'
39
+ url
40
+ end
41
+
42
+ private
43
+
44
+ def route(args = {})
45
+ route = @route.dup
46
+ route.scan(%r{:[^:/]+(?=/|$)}).each do |m|
47
+ key = m.sub(/^:/, '').to_sym
48
+ value = args.delete(key)
49
+ raise ArgumentError, %(parameter "#{key}" is required) unless value
50
+ route.sub!(/#{m}/, value.to_s)
51
+ end
52
+ route
53
+ end
54
+
55
+ def query_string(args = {})
56
+ return '' if args.empty?
57
+ query = args.keys.sort.map do |key|
58
+ [key, args[key]].join('=')
59
+ end
60
+ '?' + query.join('&')
61
+ end
62
+
63
+ def valid_verb?
64
+ %w(GET POST PUT DELETE).include? @verb
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,74 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+
3
+ require 'mt/data_api/client/endpoint'
4
+
5
+ module MT
6
+ module DataAPI
7
+ class Client
8
+ # Retrieve and find endpoints
9
+ class EndpointManager
10
+ DEFAULT_API_VERSION = 3
11
+ LIST_ENDPOINTS_HASH = {
12
+ 'id' => 'list_endpoints',
13
+ 'route' => '/endpoints',
14
+ 'version' => 1,
15
+ 'verb' => 'GET'
16
+ }.freeze
17
+
18
+ attr_accessor :endpoints
19
+ attr_reader :access_token
20
+
21
+ def initialize(opts)
22
+ raise parameter_should_be_hash unless opts.is_a? Hash
23
+ initialize_parameters(opts.symbolize_keys)
24
+ raise invalid_parameter unless @base_url && @client_id
25
+ Endpoint.api_url = api_url
26
+ end
27
+
28
+ def find_endpoint(id)
29
+ hash = find_endpoint_hash(id)
30
+ hash ? Endpoint.new(hash) : nil
31
+ end
32
+
33
+ private
34
+
35
+ def initialize_parameters(opts)
36
+ @base_url = opts[:base_url]
37
+ @client_id = opts[:client_id]
38
+ @api_version = opts[:api_version] || DEFAULT_API_VERSION
39
+ @endpoints = opts[:endpoints] if opts.key? :endpoints
40
+ @access_token = opts[:access_token] if opts.key? :access_token
41
+ end
42
+
43
+ def parameter_should_be_hash
44
+ ArgumentError.new 'parameter should be hash'
45
+ end
46
+
47
+ def invalid_parameter
48
+ ArgumentError.new 'parameter "base_url" and "client_id" are required'
49
+ end
50
+
51
+ def find_endpoint_hash(id)
52
+ @endpoints ||= retrieve_endpoints
53
+ endpoints = @endpoints.select do |ep|
54
+ ep['id'].to_s == id.to_s && @api_version.to_i >= ep['version'].to_i
55
+ end
56
+ endpoints.sort! { |a, b| b['version'].to_i <=> a['version'].to_i }
57
+ endpoints.first
58
+ end
59
+
60
+ def api_url
61
+ url = @base_url
62
+ url += '/' unless url[-1] == '/'
63
+ url + 'v' + @api_version.to_s
64
+ end
65
+
66
+ def retrieve_endpoints
67
+ response = Endpoint.new(LIST_ENDPOINTS_HASH).call
68
+ raise response['error'] if response.key? 'error'
69
+ response['items']
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ module MT
2
+ module DataAPI
3
+ class Client
4
+ VERSION = '0.0.1'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mt/data_api/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mt-data_api-client'
8
+ spec.version = MT::DataAPI::Client::VERSION
9
+ spec.authors = ['Masahiro Iuchi']
10
+ spec.email = ['masahiro.iuchi@gmail.com']
11
+
12
+ spec.summary = 'Movable Type Data API client for Ruby.'
13
+ spec.description = 'Movable Type Data API client for Ruby.'
14
+ spec.homepage = 'https://github.com/masiuchi/mt-data_api-client'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.required_ruby_version = '>= 2.0.0'
20
+
21
+ spec.add_dependency 'activesupport', '>= 4.2.7.1'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.12'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.5'
26
+ spec.add_development_dependency 'webmock', '~> 2.1'
27
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mt-data_api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masahiro Iuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.7.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ description: Movable Type Data API client for Ruby.
84
+ email:
85
+ - masahiro.iuchi@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - circle.yml
99
+ - lib/mt/data_api/client.rb
100
+ - lib/mt/data_api/client/api_request.rb
101
+ - lib/mt/data_api/client/endpoint.rb
102
+ - lib/mt/data_api/client/endpoint_manager.rb
103
+ - lib/mt/data_api/client/version.rb
104
+ - mt-data_api-client.gemspec
105
+ homepage: https://github.com/masiuchi/mt-data_api-client
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.0.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Movable Type Data API client for Ruby.
128
+ test_files: []