kokkai 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a0327d198e22e0b915b21cdf213d2b5ccf542c9a5450740f35b761a581a4721
4
- data.tar.gz: c2f47122de057d6d40cd16516fc96ad0c87d333aa05a97d11fa150b5a9e01385
3
+ metadata.gz: 2d55e93920f786bbb3978bf6c43f974781720a7a7db843d88dde7d91a31eca4d
4
+ data.tar.gz: 6a3a546d9db2361c68e096bd13553390ed52b030846369afe3ce7ed75a92a21c
5
5
  SHA512:
6
- metadata.gz: abeecebd58ed9512cb2b12e9c34990c1baf78f7089bb6ffddab06f8407a8c1e119073a516d5c416b02a4e33f5d9d5aeba33fa09e69967e354ab1e98496341205
7
- data.tar.gz: 816dd7b690972a4bb1759eb00738859b7329d7c5c74c31dacbdbbf470de0a45f9ea60c3e7c556b05b9655cf0bd8953d243fa924de3064b56c57cd2a4bf12f05a
6
+ metadata.gz: 9b7fc77b81c04801e973a73fdcfeb1b4d2de3c8a7da77f03d49e73aa05361135fa825fe445c567c51e58a015aaadc9cb03355505c9abf9895b7b9674eaac4178
7
+ data.tar.gz: 2d84485521d8ce4334ce8253c4f332776b1ab6382623695a896c6c449aee967ec4231e34b1e668de30ad9a0f02d1198fed55565026d280c4b2b961703be52959
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.7
4
+ - 2.4.4
5
+ - 2.5.1
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Kokkai
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kokkai`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Build Status](https://travis-ci.org/kimihito/kokkai.svg?branch=master)](https://travis-ci.org/kimihito/kokkai)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ http://kokkai.ndl.go.jp/api.html client library, written in Ruby
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,23 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ```
26
+ # Initialization
27
+ client = Kokkai::Client.new
28
+
29
+ # Call speech data
30
+ client.speech(any: "アベノミクス", speaker: "安倍晋三", start_record: 1, maximum_records: 5).body.data
31
+
32
+
33
+ # call meeting API
34
+
35
+ client.meeting(name_of_house: "両院", name_of_meeting: "国家基本政策委員会合同審査会").body.data
36
+
37
+
38
+ ```
39
+
40
+ More information about parameters and attributes is [here](http://kokkai.ndl.go.jp/api.html#specification) (Japanese).
41
+
26
42
 
27
43
  ## Development
28
44
 
@@ -32,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
48
 
33
49
  ## Contributing
34
50
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kokkai.
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kimihito/kokkai.
36
52
 
37
53
  ## License
38
54
 
@@ -3,5 +3,9 @@ module Kokkai
3
3
  def speech(params = nil)
4
4
  send_get("/api/1.0/speech", params)
5
5
  end
6
+
7
+ def meeting(params = nil)
8
+ send_get("/api/1.0/meeting", params)
9
+ end
6
10
  end
7
11
  end
@@ -1,12 +1,11 @@
1
1
  require 'erb'
2
- require 'kokkai/errors'
3
2
  require 'kokkai/api_methods'
4
3
  require 'kokkai/response'
4
+ require 'kokkai/utils/string_formatter'
5
5
 
6
6
  module Kokkai
7
7
  class Client
8
8
  include ApiMethods
9
-
10
9
  def initialize
11
10
  end
12
11
 
@@ -54,10 +53,14 @@ module Kokkai
54
53
  end
55
54
 
56
55
  class ParamsEncoder
56
+ using Utils::StringFormatter
57
+
57
58
  def self.encode(params)
58
- str = params.map {|(key, value)|
59
+ camelized_params = Hash[params.map {|k,v| [k.to_s.to_camel.to_sym, v]}]
60
+ str = camelized_params.map {|(key, value)|
59
61
  "#{key}=#{value}"
60
62
  }.join('&')
63
+
61
64
  return ERB::Util.url_encode(str)
62
65
  end
63
66
 
@@ -1,6 +1,8 @@
1
1
  require 'memoist'
2
2
  require 'kokkai/utils/string_formatter.rb'
3
3
  require 'kokkai/speech'
4
+ require 'kokkai/meeting'
5
+
4
6
  module Kokkai
5
7
  class Data
6
8
  extend Memoist
@@ -0,0 +1,44 @@
1
+ module Kokkai
2
+ class Error < StandardError
3
+ attr_reader :code
4
+
5
+ ClientError = Class.new(self)
6
+ BadRequest = Class.new(ClientError)
7
+
8
+ ERRORS = {
9
+ 400 => Kokkai::Error::BadRequest
10
+ }.freeze
11
+
12
+ ATTRIBUTES = %w(data diagnostics diagnostic)
13
+
14
+ class << self
15
+ def from_response(body)
16
+ message, code = parse_error(body)
17
+ new(message, code)
18
+ end
19
+
20
+ private
21
+
22
+ def parse_error(body)
23
+ if body.nil? || body.empty?
24
+ ['', nil]
25
+ elsif body.dig("data", "diagnostics")
26
+ extract_message_from_error(body.dig(*ATTRIBUTES))
27
+ end
28
+ end
29
+
30
+ def extract_message_from_error(body)
31
+ message = body.dig("details").nil? ? body.dig("message") : body.dig("details")
32
+ message = message.first if message.is_a?(Array)
33
+ match = message.match(/\([0-9]{5}\)/)
34
+ [match.post_match, match[0].gsub(/\(|\)/, '').to_i]
35
+ end
36
+
37
+ end
38
+
39
+ def initialize(message, code)
40
+ super(message)
41
+ @code = code
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ require 'kokkai/record'
2
+ require 'kokkai/speech'
3
+
4
+ module Kokkai
5
+ class Meeting < Record
6
+
7
+ def initialize(raw)
8
+ super
9
+ @raw_record = @raw_record.dig("meetingRecord")
10
+ end
11
+
12
+ def speeches
13
+ @raw_record.dig("speechRecord").map do |record|
14
+ recordData = { "recordData" => { "speechRecord" => record } }
15
+ Kokkai::Speech.new(recordData)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,10 +1,23 @@
1
1
  require "kokkai/data"
2
+ require "kokkai/error"
2
3
  module Kokkai
3
4
  class Parser
4
5
  attr_reader :raw_data, :data
5
6
 
6
- def initialize(raw_data)
7
- @data = Data.new(raw_data.dig("data"))
7
+ def initialize(raw_data, raw_status)
8
+ if raw_status == 200
9
+ @data = Data.new(raw_data.dig("data"))
10
+ else
11
+ error = error(raw_data, raw_status)
12
+ raise(error)
13
+ end
8
14
  end
15
+
16
+ private
17
+
18
+ def error(raw_data, raw_status)
19
+ klass = Kokkai::Error::ERRORS[raw_status]
20
+ klass.from_response(raw_data)
21
+ end
9
22
  end
10
23
  end
@@ -9,7 +9,7 @@ module Kokkai
9
9
  end
10
10
 
11
11
  def body
12
- Parser.new(@raw_body)
12
+ Parser.new(@raw_body, @raw_status)
13
13
  end
14
14
 
15
15
  def headers
@@ -1,3 +1,3 @@
1
1
  module Kokkai
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kokkai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kimihito
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-18 00:00:00.000000000 Z
11
+ date: 2018-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -200,6 +200,7 @@ extensions: []
200
200
  extra_rdoc_files: []
201
201
  files:
202
202
  - ".gitignore"
203
+ - ".travis.yml"
203
204
  - Gemfile
204
205
  - Guardfile
205
206
  - MIT-LICENSE
@@ -212,7 +213,8 @@ files:
212
213
  - lib/kokkai/api_methods.rb
213
214
  - lib/kokkai/client.rb
214
215
  - lib/kokkai/data.rb
215
- - lib/kokkai/errors.rb
216
+ - lib/kokkai/error.rb
217
+ - lib/kokkai/meeting.rb
216
218
  - lib/kokkai/parser.rb
217
219
  - lib/kokkai/record.rb
218
220
  - lib/kokkai/response.rb
@@ -1,4 +0,0 @@
1
- module Kokkai
2
- class KokkaiError < StandardError; end
3
- class ServiceUnavailableError < KokkaiError; end
4
- end