syoboi_calendar 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +0 -6
  4. data/lib/syoboi_calendar/client.rb +31 -3
  5. data/lib/syoboi_calendar/queries/base_query.rb +77 -0
  6. data/lib/syoboi_calendar/queries/channel_query.rb +19 -0
  7. data/lib/syoboi_calendar/queries/program_query.rb +51 -0
  8. data/lib/syoboi_calendar/queries/title_query.rb +19 -0
  9. data/lib/syoboi_calendar/resources/{base.rb → base_resource.rb} +1 -1
  10. data/lib/syoboi_calendar/resources/{channel.rb → channel_resource.rb} +1 -1
  11. data/lib/syoboi_calendar/resources/{program.rb → program_resource.rb} +1 -1
  12. data/lib/syoboi_calendar/resources/{title.rb → title_resource.rb} +1 -1
  13. data/lib/syoboi_calendar/responses/base_response.rb +31 -0
  14. data/lib/syoboi_calendar/responses/channels_response.rb +12 -0
  15. data/lib/syoboi_calendar/responses/programs_response.rb +12 -0
  16. data/lib/syoboi_calendar/responses/titles_response.rb +12 -0
  17. data/lib/syoboi_calendar/version.rb +1 -1
  18. data/lib/syoboi_calendar.rb +12 -21
  19. data/spec/syoboi_calendar/client_spec.rb +46 -56
  20. metadata +13 -19
  21. data/lib/syoboi_calendar/connector.rb +0 -27
  22. data/lib/syoboi_calendar/parsers/base.rb +0 -25
  23. data/lib/syoboi_calendar/parsers/channel.rb +0 -15
  24. data/lib/syoboi_calendar/parsers/program.rb +0 -15
  25. data/lib/syoboi_calendar/parsers/title.rb +0 -15
  26. data/lib/syoboi_calendar/processors/base.rb +0 -43
  27. data/lib/syoboi_calendar/processors/channel.rb +0 -15
  28. data/lib/syoboi_calendar/processors/program.rb +0 -81
  29. data/lib/syoboi_calendar/processors/title.rb +0 -15
  30. data/lib/syoboi_calendar/query_builders/base.rb +0 -81
  31. data/lib/syoboi_calendar/query_builders/channel.rb +0 -13
  32. data/lib/syoboi_calendar/query_builders/channel_id_queriable.rb +0 -15
  33. data/lib/syoboi_calendar/query_builders/program.rb +0 -49
  34. data/lib/syoboi_calendar/query_builders/title.rb +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5261efe9fb199a761c6212fce48a35eb2057f929
4
- data.tar.gz: b28ef3da5e1afebbf208fa112793657bd491e537
3
+ metadata.gz: 6198d0d820f2c6005636a48403f17b363a04fb17
4
+ data.tar.gz: 0fd21d40332d20febf1fc15403770d35f90ac5d4
5
5
  SHA512:
6
- metadata.gz: 540c45673ee1e2f82ee96e91ec1ff747b1ac3febc276e500582b30b2c75ec66cc880026b4ac75fee20179d69eccca37999091e4af787bbc60b261df21cd50d3c
7
- data.tar.gz: de6d0d097673a97ba601e4cb83c6a8325986aec5cc5aa7c3c7d2ec82e2d9fbe9db60323c29861cf5ff9b345c5d729125de7a045d07b986a00a594355c8e5b016
6
+ metadata.gz: 36f9c42cd64d720b59110a884a194c4fbe823e85bd3323e186923e003f65fca566ddfbc1e426f6da758944b99f01cb29b0b4e0b7eaebe981348196b831f52bed
7
+ data.tar.gz: 6f6ff38c9e8e939cf0885f43d40ae0522ecf08deb314516ca749364b95221e592e53d36ab31c1559c823f7bfe186e3eb1fff95bc1346059750557ed1a8e33a4f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.5.0
2
+
3
+ - Refactor internal implementation
4
+ - Add `TID=*` query if no `:title_id` option given on `#list_titles`
5
+
1
6
  ## 0.4.0
2
7
 
3
8
  - Change client method names (e..g `#programs` to `#list_programs`)
data/README.md CHANGED
@@ -153,12 +153,6 @@ Sends an HTTP request to `http://cal.syoboi.jp/db.php?Command=ProgLookup&JOIN=Su
153
153
  client.list_programs(played_from: Time.new(2000, 1, 1), played_to: Time.new(2000, 2, 1))
154
154
  ```
155
155
 
156
- Eage-loads related resources.
157
-
158
- ```ruby
159
- client.list_programs(include: [:channel, :title])
160
- ```
161
-
162
156
  ### SyoboiCalendar::Client#list_titles
163
157
 
164
158
  Sends an HTTP request to `http://cal.syoboi.jp/db.php?Command=TitleLookup`.
@@ -1,18 +1,46 @@
1
1
  module SyoboiCalendar
2
2
  class Client
3
+ ENDPOINT_BASE_URL = "http://cal.syoboi.jp"
4
+ ENDPOINT_PATH = "/db.php"
5
+
6
+ # @return [Faraday::Connection]
7
+ def connection
8
+ @connection ||= ::Faraday::Connection.new(url: ENDPOINT_BASE_URL) do |connection|
9
+ connection.adapter :net_http
10
+ connection.response :mashify
11
+ connection.response :xml
12
+ connection.response :raise_error
13
+ end
14
+ end
15
+
3
16
  # @return [Array<SyoboiCalendar::Resources::Channel>]
4
17
  def list_channels(options = {})
5
- Processors::Channel.process(options)
18
+ query = ::SyoboiCalendar::Queries::ChannelQuery.new(options)
19
+ faraday_response = get(query.to_hash)
20
+ ::SyoboiCalendar::Responses::ChannelsResponse.new(faraday_response)
6
21
  end
7
22
 
8
23
  # @return [Array<SyoboiCalendar::Resources::Program>]
9
24
  def list_programs(options = {})
10
- Processors::Program.process(options)
25
+ query = ::SyoboiCalendar::Queries::ProgramQuery.new(options)
26
+ faraday_response = get(query.to_hash)
27
+ ::SyoboiCalendar::Responses::ProgramsResponse.new(faraday_response)
11
28
  end
12
29
 
13
30
  # @return [Array<SyoboiCalendar::Resources::Title>]
14
31
  def list_titles(options = {})
15
- Processors::Title.process(options)
32
+ query = ::SyoboiCalendar::Queries::TitleQuery.new(options)
33
+ faraday_response = get(query.to_hash)
34
+ ::SyoboiCalendar::Responses::TitlesResponse.new(faraday_response)
35
+ end
36
+
37
+ private
38
+
39
+ # @private
40
+ # @param query [Hash]
41
+ # @return [Faraday::Response]
42
+ def get(query)
43
+ connection.get(ENDPOINT_PATH, query)
16
44
  end
17
45
  end
18
46
  end
@@ -0,0 +1,77 @@
1
+ module SyoboiCalendar
2
+ module Queries
3
+ class BaseQuery
4
+ class << self
5
+ attr_writer :property_names
6
+
7
+ # @note Override
8
+ def inherited(child_class)
9
+ child_class.property_names = property_names.clone
10
+ end
11
+
12
+ # @return [Array<Symbol>]
13
+ def property_names
14
+ @property_names ||= []
15
+ end
16
+
17
+ # @param property_name [Symbol]
18
+ def property(property_name)
19
+ property_names << property_name
20
+ end
21
+ end
22
+
23
+ property :Command
24
+ property :LastUpdate
25
+
26
+ def initialize(options = {})
27
+ @options = options
28
+ end
29
+
30
+ # @return [String]
31
+ def Command
32
+ raise ::NotImplementedError
33
+ end
34
+
35
+ # @return [String, nil]
36
+ def LastUpdate
37
+ format_time_range(options[:updated_from], options[:updated_to])
38
+ end
39
+
40
+ # @return [Hash]
41
+ def to_hash
42
+ self.class.property_names.each_with_object({}) do |property_name, result|
43
+ value = send(property_name)
44
+ unless value.nil?
45
+ result[property_name.to_s] = value
46
+ end
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # @param [Time, nil]
53
+ # @return [String]
54
+ def format_time(time)
55
+ if time
56
+ time.strftime("%Y%m%d_%H%M%S")
57
+ end
58
+ end
59
+
60
+ # @param started_at [Time, nil]
61
+ # @param finished_at [Time, nil]
62
+ def format_time_range(started_at, finished_at)
63
+ if started_at || finished_at
64
+ [
65
+ format_time(started_at),
66
+ format_time(finished_at),
67
+ ].join("-")
68
+ end
69
+ end
70
+
71
+ # @return [Hash]
72
+ def options
73
+ @options
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,19 @@
1
+ module SyoboiCalendar
2
+ module Queries
3
+ class ChannelQuery < BaseQuery
4
+ COMMAND = "ChLookup"
5
+
6
+ property :ChID
7
+
8
+ # @return [String, nil]
9
+ def ChID
10
+ options[:channel_id]
11
+ end
12
+
13
+ # @note Override
14
+ def Command
15
+ COMMAND
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ module SyoboiCalendar
2
+ module Queries
3
+ class ProgramQuery < BaseQuery
4
+ COMMAND = "ProgLookup"
5
+
6
+ JOIN = "SubTitles"
7
+
8
+ property :ChID
9
+ property :Count
10
+ property :JOIN
11
+ property :PID
12
+ property :Range
13
+ property :StTime
14
+
15
+ # @return [Integer, nil]
16
+ def ChID
17
+ options[:channel_id]
18
+ end
19
+
20
+ # @note Override
21
+ def Command
22
+ COMMAND
23
+ end
24
+
25
+ # @return [Integer, nil]
26
+ def Count
27
+ options[:count]
28
+ end
29
+
30
+ # @return [String]
31
+ def JOIN
32
+ JOIN
33
+ end
34
+
35
+ # @return [Integer, nil]
36
+ def PID
37
+ options[:program_id]
38
+ end
39
+
40
+ # @return [String, nil]
41
+ def Range
42
+ format_time_range(options[:played_from], options[:played_to])
43
+ end
44
+
45
+ # @return [String, nil]
46
+ def StTime
47
+ format_time_range(options[:started_from], options[:started_to])
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ module SyoboiCalendar
2
+ module Queries
3
+ class TitleQuery < BaseQuery
4
+ COMMAND = "TitleLookup"
5
+
6
+ property :TID
7
+
8
+ # @note Override
9
+ def Command
10
+ COMMAND
11
+ end
12
+
13
+ # @return [Integer, String]
14
+ def TID
15
+ options[:title_id] || "*"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Resources
3
- class Base
3
+ class BaseResource
4
4
  # @return [Hashie::Mash]
5
5
  attr_reader :response
6
6
 
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Resources
3
- class Channel < Base
3
+ class ChannelResource < BaseResource
4
4
  # @return [String, nil]
5
5
  def comment
6
6
  response.ChComment
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Resources
3
- class Program < Base
3
+ class ProgramResource < BaseResource
4
4
  # @return [SyoboiCalendar::Resources::Channel, nil]
5
5
  attr_accessor :channel
6
6
 
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Resources
3
- class Title < Base
3
+ class TitleResource < BaseResource
4
4
  # @return [Integer, nil]
5
5
  def category_id
6
6
  response.Cat.try(:to_i)
@@ -0,0 +1,31 @@
1
+ module SyoboiCalendar
2
+ module Responses
3
+ class BaseResponse
4
+ include ::Enumerable
5
+
6
+ # @param faraday_response [Faraday::Response]
7
+ def initialize(faraday_response)
8
+ @faraday_response = faraday_response
9
+ end
10
+
11
+ # @note Override
12
+ def each
13
+ resources.map do |resource|
14
+ yield resource
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ # @return [Array<SyoboiCalendar::Resources::BaseResource>]
21
+ def resources
22
+ raise ::NotImplementedError
23
+ end
24
+
25
+ # @return [Faraday::Response]
26
+ def faraday_response
27
+ @faraday_response
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module SyoboiCalendar
2
+ module Responses
3
+ class ChannelsResponse < BaseResponse
4
+ # @note Override
5
+ def resources
6
+ ::Array.wrap(faraday_response.body.ChLookupResponse.ChItems.ChItem).map do |element|
7
+ ::SyoboiCalendar::Resources::ChannelResource.new(element)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module SyoboiCalendar
2
+ module Responses
3
+ class ProgramsResponse < BaseResponse
4
+ # @note Override
5
+ def resources
6
+ ::Array.wrap(faraday_response.body.ProgLookupResponse.ProgItems.ProgItem).map do |element|
7
+ ::SyoboiCalendar::Resources::ProgramResource.new(element)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module SyoboiCalendar
2
+ module Responses
3
+ class TitlesResponse < BaseResponse
4
+ # @note Override
5
+ def resources
6
+ ::Array.wrap(faraday_response.body.TitleLookupResponse.TitleItems.TitleItem).map do |element|
7
+ ::SyoboiCalendar::Resources::TitleResource.new(element)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module SyoboiCalendar
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,28 +1,19 @@
1
1
  require "active_support/concern"
2
2
  require "active_support/core_ext/array/wrap"
3
- require "active_support/core_ext/class/attribute"
4
- require "active_support/core_ext/enumerable"
5
3
  require "active_support/core_ext/object/try"
6
- require "active_support/core_ext/string/inflections"
7
4
  require "faraday"
8
5
  require "faraday_middleware"
9
6
 
10
7
  require "syoboi_calendar/client"
11
- require "syoboi_calendar/connector"
12
- require "syoboi_calendar/parsers/base"
13
- require "syoboi_calendar/parsers/channel"
14
- require "syoboi_calendar/parsers/program"
15
- require "syoboi_calendar/parsers/title"
16
- require "syoboi_calendar/processors/base"
17
- require "syoboi_calendar/processors/channel"
18
- require "syoboi_calendar/processors/program"
19
- require "syoboi_calendar/processors/title"
20
- require "syoboi_calendar/query_builders/base"
21
- require "syoboi_calendar/query_builders/channel_id_queriable"
22
- require "syoboi_calendar/query_builders/channel"
23
- require "syoboi_calendar/query_builders/program"
24
- require "syoboi_calendar/query_builders/title"
25
- require "syoboi_calendar/resources/base"
26
- require "syoboi_calendar/resources/channel"
27
- require "syoboi_calendar/resources/program"
28
- require "syoboi_calendar/resources/title"
8
+ require "syoboi_calendar/queries/base_query"
9
+ require "syoboi_calendar/queries/channel_query"
10
+ require "syoboi_calendar/queries/program_query"
11
+ require "syoboi_calendar/queries/title_query"
12
+ require "syoboi_calendar/resources/base_resource"
13
+ require "syoboi_calendar/resources/channel_resource"
14
+ require "syoboi_calendar/resources/program_resource"
15
+ require "syoboi_calendar/resources/title_resource"
16
+ require "syoboi_calendar/responses/base_response"
17
+ require "syoboi_calendar/responses/channels_response"
18
+ require "syoboi_calendar/responses/programs_response"
19
+ require "syoboi_calendar/responses/titles_response"
@@ -192,17 +192,16 @@ describe ::SyoboiCalendar::Client do
192
192
  stub_request(:get, //).to_rack(app)
193
193
  end
194
194
 
195
- it "returns an Array of ::SyoboiCalendar::Resources::Channel" do
195
+ it "returns channels" do
196
196
  channels = subject
197
- channels[0].should be_a ::SyoboiCalendar::Resources::Channel
198
- channels[0].comment.should == "DummyComment"
199
- channels[0].epg_url.should == "http://example.com/epg-url"
200
- channels[0].group_id.should == 1
201
- channels[0].id.should == 3
202
- channels[0].iepg_name.should == "DummyiEPGName"
203
- channels[0].name.should == "DummyChannelName"
204
- channels[0].number.should == 3
205
- channels[0].url.should == "http://example.com/"
197
+ channels.first.comment.should == "DummyComment"
198
+ channels.first.epg_url.should == "http://example.com/epg-url"
199
+ channels.first.group_id.should == 1
200
+ channels.first.id.should == 3
201
+ channels.first.iepg_name.should == "DummyiEPGName"
202
+ channels.first.name.should == "DummyChannelName"
203
+ channels.first.number.should == 3
204
+ channels.first.url.should == "http://example.com/"
206
205
  end
207
206
  end
208
207
  end
@@ -368,34 +367,21 @@ describe ::SyoboiCalendar::Client do
368
367
  stub_request(:get, //).to_rack(app)
369
368
  end
370
369
 
371
- it "returns an Array of ::SyoboiCalendar::Resources::Program" do
370
+ it "returns programs" do
372
371
  programs = subject
373
- programs[0].should be_a ::SyoboiCalendar::Resources::Program
374
- programs[0].channel_id.should == 3
375
- programs[0].comment.should == "DummyComment"
376
- programs[0].count.should == 1
377
- programs[0].deleted?.should == false
378
- programs[0].finished_at.should == Time.parse("2000-01-01 00:00:00")
379
- programs[0].flag.should == 9
380
- programs[0].id.should == 1
381
- programs[0].revision.should == 2
382
- programs[0].started_at.should == Time.parse("2000-01-01 00:00:00")
383
- programs[0].sub_title.should == "DummySubTitle"
384
- programs[0].title_id.should == 2
385
- programs[0].updated_at.should == Time.parse("2000-01-01 00:00:00")
386
- programs[0].warn.should == 1
387
- end
388
- end
389
-
390
- context "with :include option" do
391
- let!(:request) do
392
- stub_request(:get, //).to_rack(app)
393
- end
394
-
395
- it "eager loads related resources" do
396
- programs = client.list_programs(includes: [:channel, :title])
397
- programs[0].channel.should be_a ::SyoboiCalendar::Resources::Channel
398
- programs[0].title.should be_a ::SyoboiCalendar::Resources::Title
372
+ programs.first.channel_id.should == 3
373
+ programs.first.comment.should == "DummyComment"
374
+ programs.first.count.should == 1
375
+ programs.first.deleted?.should == false
376
+ programs.first.finished_at.should == Time.parse("2000-01-01 00:00:00")
377
+ programs.first.flag.should == 9
378
+ programs.first.id.should == 1
379
+ programs.first.revision.should == 2
380
+ programs.first.started_at.should == Time.parse("2000-01-01 00:00:00")
381
+ programs.first.sub_title.should == "DummySubTitle"
382
+ programs.first.title_id.should == 2
383
+ programs.first.updated_at.should == Time.parse("2000-01-01 00:00:00")
384
+ programs.first.warn.should == 1
399
385
  end
400
386
  end
401
387
  end
@@ -414,6 +400,7 @@ describe ::SyoboiCalendar::Client do
414
400
  options: {},
415
401
  query: {
416
402
  Command: "TitleLookup",
403
+ TID: "*",
417
404
  },
418
405
  },
419
406
  {
@@ -432,6 +419,7 @@ describe ::SyoboiCalendar::Client do
432
419
  query: {
433
420
  Command: "TitleLookup",
434
421
  LastUpdate: "20000101_000000-",
422
+ TID: "*",
435
423
  },
436
424
  },
437
425
  {
@@ -441,16 +429,19 @@ describe ::SyoboiCalendar::Client do
441
429
  query: {
442
430
  Command: "TitleLookup",
443
431
  LastUpdate: "-20000101_000000",
432
+ TID: "*",
444
433
  },
445
434
  },
446
435
  {
447
436
  options: {
448
437
  updated_from: ::Time.new(2000, 1, 1),
449
438
  updated_to: ::Time.new(2000, 1, 1),
439
+ TID: "*",
450
440
  },
451
441
  query: {
452
442
  Command: "TitleLookup",
453
443
  LastUpdate: "20000101_000000-20000101_000000",
444
+ TID: "*",
454
445
  },
455
446
  },
456
447
  ].each do |example|
@@ -472,26 +463,25 @@ describe ::SyoboiCalendar::Client do
472
463
  stub_request(:get, //).to_rack(app)
473
464
  end
474
465
 
475
- it "returns an Array of ::SyoboiCalendar::Resources::Title" do
466
+ it "returns titles" do
476
467
  titles = subject
477
- titles[0].should be_a ::SyoboiCalendar::Resources::Title
478
- titles[0].category_id.should == 4
479
- titles[0].comment.should == "DummyComment"
480
- titles[0].first_channel.should == "DummyChannel"
481
- titles[0].first_end_month.should == 1
482
- titles[0].first_end_year.should == 2000
483
- titles[0].first_month.should == 1
484
- titles[0].first_year.should == 2000
485
- titles[0].keywords.should == "DummyKeywords"
486
- titles[0].short_title.should == "DummyShortTitle"
487
- titles[0].sub_titles.should == "DummySubTitles"
488
- titles[0].id.should == 2
489
- titles[0].name.should == "DummyTitle"
490
- titles[0].english_name.should == "DummyEnglishTitle"
491
- titles[0].flag.should == 0
492
- titles[0].kana.should == "ダミータイトル"
493
- titles[0].point.should == 6
494
- titles[0].rank.should == 1
468
+ titles.first.category_id.should == 4
469
+ titles.first.comment.should == "DummyComment"
470
+ titles.first.first_channel.should == "DummyChannel"
471
+ titles.first.first_end_month.should == 1
472
+ titles.first.first_end_year.should == 2000
473
+ titles.first.first_month.should == 1
474
+ titles.first.first_year.should == 2000
475
+ titles.first.keywords.should == "DummyKeywords"
476
+ titles.first.short_title.should == "DummyShortTitle"
477
+ titles.first.sub_titles.should == "DummySubTitles"
478
+ titles.first.id.should == 2
479
+ titles.first.name.should == "DummyTitle"
480
+ titles.first.english_name.should == "DummyEnglishTitle"
481
+ titles.first.flag.should == 0
482
+ titles.first.kana.should == "ダミータイトル"
483
+ titles.first.point.should == 6
484
+ titles.first.rank.should == 1
495
485
  end
496
486
  end
497
487
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syoboi_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -166,24 +166,18 @@ files:
166
166
  - Rakefile
167
167
  - lib/syoboi_calendar.rb
168
168
  - lib/syoboi_calendar/client.rb
169
- - lib/syoboi_calendar/connector.rb
170
- - lib/syoboi_calendar/parsers/base.rb
171
- - lib/syoboi_calendar/parsers/channel.rb
172
- - lib/syoboi_calendar/parsers/program.rb
173
- - lib/syoboi_calendar/parsers/title.rb
174
- - lib/syoboi_calendar/processors/base.rb
175
- - lib/syoboi_calendar/processors/channel.rb
176
- - lib/syoboi_calendar/processors/program.rb
177
- - lib/syoboi_calendar/processors/title.rb
178
- - lib/syoboi_calendar/query_builders/base.rb
179
- - lib/syoboi_calendar/query_builders/channel.rb
180
- - lib/syoboi_calendar/query_builders/channel_id_queriable.rb
181
- - lib/syoboi_calendar/query_builders/program.rb
182
- - lib/syoboi_calendar/query_builders/title.rb
183
- - lib/syoboi_calendar/resources/base.rb
184
- - lib/syoboi_calendar/resources/channel.rb
185
- - lib/syoboi_calendar/resources/program.rb
186
- - lib/syoboi_calendar/resources/title.rb
169
+ - lib/syoboi_calendar/queries/base_query.rb
170
+ - lib/syoboi_calendar/queries/channel_query.rb
171
+ - lib/syoboi_calendar/queries/program_query.rb
172
+ - lib/syoboi_calendar/queries/title_query.rb
173
+ - lib/syoboi_calendar/resources/base_resource.rb
174
+ - lib/syoboi_calendar/resources/channel_resource.rb
175
+ - lib/syoboi_calendar/resources/program_resource.rb
176
+ - lib/syoboi_calendar/resources/title_resource.rb
177
+ - lib/syoboi_calendar/responses/base_response.rb
178
+ - lib/syoboi_calendar/responses/channels_response.rb
179
+ - lib/syoboi_calendar/responses/programs_response.rb
180
+ - lib/syoboi_calendar/responses/titles_response.rb
187
181
  - lib/syoboi_calendar/version.rb
188
182
  - spec/spec_helper.rb
189
183
  - spec/syoboi_calendar/client_spec.rb
@@ -1,27 +0,0 @@
1
- module SyoboiCalendar
2
- class Connector
3
- URL = "http://cal.syoboi.jp/"
4
- PATH = "/db.php"
5
-
6
- def get(query)
7
- connection.get(path, query)
8
- end
9
-
10
- def connection
11
- @connection ||= Faraday.new(url: url) do |connection|
12
- connection.adapter :net_http
13
- connection.response :mashify
14
- connection.response :xml
15
- connection.response :raise_error
16
- end
17
- end
18
-
19
- def path
20
- PATH
21
- end
22
-
23
- def url
24
- URL
25
- end
26
- end
27
- end
@@ -1,25 +0,0 @@
1
- module SyoboiCalendar
2
- module Parsers
3
- class Base
4
- class << self
5
- def parse(response)
6
- new(response).parse
7
- end
8
- end
9
-
10
- attr_reader :response
11
-
12
- def initialize(response)
13
- @response = response
14
- end
15
-
16
- def parse
17
- items.map do |item|
18
- resource_class.new(item)
19
- end
20
- rescue NoMethodError
21
- []
22
- end
23
- end
24
- end
25
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module Parsers
3
- class Channel < Base
4
- private
5
-
6
- def resource_class
7
- Resources::Channel
8
- end
9
-
10
- def items
11
- ::Array.wrap(response.ChLookupResponse.ChItems.ChItem)
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module Parsers
3
- class Program < Base
4
- private
5
-
6
- def resource_class
7
- Resources::Program
8
- end
9
-
10
- def items
11
- ::Array.wrap(response.ProgLookupResponse.ProgItems.ProgItem)
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module Parsers
3
- class Title < Base
4
- private
5
-
6
- def resource_class
7
- Resources::Title
8
- end
9
-
10
- def items
11
- ::Array.wrap(response.TitleLookupResponse.TitleItems.TitleItem)
12
- end
13
- end
14
- end
15
- end
@@ -1,43 +0,0 @@
1
- module SyoboiCalendar
2
- module Processors
3
- class Base
4
- class << self
5
- def process(*args)
6
- new(*args).process
7
- end
8
- end
9
-
10
- attr_reader :options
11
-
12
- def initialize(options)
13
- @options = options
14
- end
15
-
16
- def process
17
- resources
18
- end
19
-
20
- private
21
-
22
- def resources
23
- @resources ||= begin
24
- query = query_builder.build(options)
25
- response = get(query)
26
- parser.parse(response)
27
- end
28
- end
29
-
30
- def connector
31
- @connector ||= Connector.new
32
- end
33
-
34
- def get(query)
35
- connector.get(query).body
36
- end
37
-
38
- def includes
39
- ::Array.wrap(options[:includes])
40
- end
41
- end
42
- end
43
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module Processors
3
- class Channel < Base
4
- private
5
-
6
- def query_builder
7
- QueryBuilders::Channel
8
- end
9
-
10
- def parser
11
- Parsers::Channel
12
- end
13
- end
14
- end
15
- end
@@ -1,81 +0,0 @@
1
- module SyoboiCalendar
2
- module Processors
3
- class Program < Base
4
- def process
5
- include_channel if specified_to_include_channel?
6
- include_title if specified_to_include_title?
7
- resources
8
- end
9
-
10
- private
11
-
12
- def query_builder
13
- QueryBuilders::Program
14
- end
15
-
16
- def parser
17
- Parsers::Program
18
- end
19
-
20
- def titles_indexed_by_id
21
- @titles_indexed_by_id ||= titles.index_by(&:id)
22
- end
23
-
24
- def titles
25
- Parsers::Title.parse(titles_response)
26
- end
27
-
28
- def titles_response
29
- connector.get(titles_query).body
30
- end
31
-
32
- def titles_query
33
- QueryBuilders::Title.build(title_id: title_ids.join(","))
34
- end
35
-
36
- def title_ids
37
- resources.map(&:title_id)
38
- end
39
-
40
- def specified_to_include_title?
41
- includes.include?(:title)
42
- end
43
-
44
- def specified_to_include_channel?
45
- includes.include?(:channel)
46
- end
47
-
48
- def include_title
49
- resources.each do |program|
50
- program.title = titles_indexed_by_id[program.title_id]
51
- end
52
- end
53
-
54
- def channels_indexed_by_id
55
- @channels_indexed_by_id ||= channels.index_by(&:id)
56
- end
57
-
58
- def channels
59
- Parsers::Channel.parse(channels_response)
60
- end
61
-
62
- def channels_response
63
- connector.get(channels_query).body
64
- end
65
-
66
- def channels_query
67
- QueryBuilders::Channel.build(channel_id: channel_ids.join(","))
68
- end
69
-
70
- def channel_ids
71
- resources.map(&:channel_id)
72
- end
73
-
74
- def include_channel
75
- resources.each do |program|
76
- program.channel = channels_indexed_by_id[program.channel_id]
77
- end
78
- end
79
- end
80
- end
81
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module Processors
3
- class Title < Base
4
- private
5
-
6
- def query_builder
7
- QueryBuilders::Title
8
- end
9
-
10
- def parser
11
- Parsers::Title
12
- end
13
- end
14
- end
15
- end
@@ -1,81 +0,0 @@
1
- module SyoboiCalendar
2
- module QueryBuilders
3
- class Base
4
- class << self
5
- def build(*args)
6
- new(*args).to_hash
7
- end
8
-
9
- def property(*properties)
10
- self.properties += properties
11
- end
12
-
13
- def option(*options)
14
- options.each do |option|
15
- define_method(option) do
16
- self.options[option]
17
- end
18
- end
19
- end
20
-
21
- def time_option(*names)
22
- names.each do |name|
23
- define_method("has_#{name}_time?") do
24
- !!(send("#{name}_from") || send("#{name}_to"))
25
- end
26
-
27
- define_method("formatted_#{name}_from") do
28
- send("#{name}_from").try(:strftime, "%Y%m%d_%H%M%S")
29
- end
30
-
31
- define_method("formatted_#{name}_to") do
32
- send("#{name}_to").try(:strftime, "%Y%m%d_%H%M%S")
33
- end
34
- end
35
- end
36
- end
37
-
38
- class_attribute :properties
39
-
40
- self.properties = []
41
-
42
- option(
43
- :updated_from,
44
- :updated_to,
45
- )
46
-
47
- time_option(
48
- :updated,
49
- )
50
-
51
- property(
52
- :Command,
53
- :LastUpdate,
54
- )
55
-
56
- attr_reader :options
57
-
58
- def initialize(options = {})
59
- @options = options
60
- end
61
-
62
- def to_hash
63
- to_hash_with_nil_value.reject {|key, value| value.nil? }
64
- end
65
-
66
- def to_hash_with_nil_value
67
- properties.inject({}) do |hash, property|
68
- hash.merge(property => send(property.to_s.underscore))
69
- end
70
- end
71
-
72
- def properties
73
- self.class.properties
74
- end
75
-
76
- def last_update
77
- "#{formatted_updated_from}-#{formatted_updated_to}" if has_updated_time?
78
- end
79
- end
80
- end
81
- end
@@ -1,13 +0,0 @@
1
- module SyoboiCalendar
2
- module QueryBuilders
3
- class Channel < Base
4
- include ChannelIdQueriable
5
-
6
- private
7
-
8
- def command
9
- "ChLookup"
10
- end
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- module SyoboiCalendar
2
- module QueryBuilders
3
- module ChannelIdQueriable
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- option :channel_id
8
-
9
- property :ChID
10
-
11
- alias ch_id channel_id
12
- end
13
- end
14
- end
15
- end
@@ -1,49 +0,0 @@
1
- module SyoboiCalendar
2
- module QueryBuilders
3
- class Program < Base
4
- include ChannelIdQueriable
5
-
6
- option(
7
- :count,
8
- :program_id,
9
- :played_from,
10
- :played_to,
11
- :started_from,
12
- :started_to,
13
- )
14
-
15
- time_option(
16
- :played,
17
- :started,
18
- )
19
-
20
- property(
21
- :Count,
22
- :JOIN,
23
- :PID,
24
- :Range,
25
- :StTime,
26
- )
27
-
28
- alias pid program_id
29
-
30
- private
31
-
32
- def command
33
- "ProgLookup"
34
- end
35
-
36
- def join
37
- "SubTitles"
38
- end
39
-
40
- def range
41
- "#{formatted_played_from}-#{formatted_played_to}" if has_played_time?
42
- end
43
-
44
- def st_time
45
- "#{formatted_started_from}-#{formatted_started_to}" if has_started_time?
46
- end
47
- end
48
- end
49
- end
@@ -1,17 +0,0 @@
1
- module SyoboiCalendar
2
- module QueryBuilders
3
- class Title < Base
4
- option :title_id
5
-
6
- property :TID
7
-
8
- alias tid title_id
9
-
10
- private
11
-
12
- def command
13
- "TitleLookup"
14
- end
15
- end
16
- end
17
- end