syoboi_calendar 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,6 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
1
4
  module SyoboiCalendar
2
5
  class Client
3
6
  ENDPOINT_BASE_URL = "http://cal.syoboi.jp"
@@ -7,47 +10,48 @@ module SyoboiCalendar
7
10
  def connection
8
11
  @connection ||= ::Faraday::Connection.new(url: ENDPOINT_BASE_URL) do |connection|
9
12
  connection.adapter :net_http
13
+ connection.response :xml
10
14
  connection.response :raise_error
11
15
  end
12
16
  end
13
17
 
14
- # @return [Array<SyoboiCalendar::Resources::ChannelGroup>]
18
+ # @param options [Hash]
19
+ # @return [SyoboiCalendar::Response]
15
20
  def list_channel_groups(options = {})
16
- query = ::SyoboiCalendar::Queries::ChannelGroupQuery.new(options)
17
- faraday_response = get(query.to_hash)
18
- ::SyoboiCalendar::Response.new(
19
- faraday_response: faraday_response,
20
- response_parser_class: ::SyoboiCalendar::ResponseParsers::ChannelGroupsResponseParser,
21
+ list(
22
+ options: options,
23
+ query_class: ::SyoboiCalendar::Queries::ListChannelGroupsQuery,
24
+ response_class: ::SyoboiCalendar::Responses::ListChannelGroupsResponse,
21
25
  )
22
26
  end
23
27
 
24
- # @return [Array<SyoboiCalendar::Resources::Channel>]
28
+ # @param options [Hash]
29
+ # @return [SyoboiCalendar::Response]
25
30
  def list_channels(options = {})
26
- query = ::SyoboiCalendar::Queries::ChannelQuery.new(options)
27
- faraday_response = get(query.to_hash)
28
- ::SyoboiCalendar::Response.new(
29
- faraday_response: faraday_response,
30
- response_parser_class: ::SyoboiCalendar::ResponseParsers::ChannelsResponseParser,
31
+ list(
32
+ options: options,
33
+ query_class: ::SyoboiCalendar::Queries::ListChannelsQuery,
34
+ response_class: ::SyoboiCalendar::Responses::ListChannelsResponse,
31
35
  )
32
36
  end
33
37
 
34
- # @return [Array<SyoboiCalendar::Resources::Program>]
38
+ # @param options [Hash]
39
+ # @return [SyoboiCalendar::Response]
35
40
  def list_programs(options = {})
36
- query = ::SyoboiCalendar::Queries::ProgramQuery.new(options)
37
- faraday_response = get(query.to_hash)
38
- ::SyoboiCalendar::Response.new(
39
- faraday_response: faraday_response,
40
- response_parser_class: ::SyoboiCalendar::ResponseParsers::ProgramsResponseParser,
41
+ list(
42
+ options: options,
43
+ query_class: ::SyoboiCalendar::Queries::ListProgramsQuery,
44
+ response_class: ::SyoboiCalendar::Responses::ListProgramsResponse,
41
45
  )
42
46
  end
43
47
 
44
- # @return [Array<SyoboiCalendar::Resources::Title>]
48
+ # @param options [Hash]
49
+ # @return [SyoboiCalendar::Response]
45
50
  def list_titles(options = {})
46
- query = ::SyoboiCalendar::Queries::TitleQuery.new(options)
47
- faraday_response = get(query.to_hash)
48
- ::SyoboiCalendar::Response.new(
49
- faraday_response: faraday_response,
50
- response_parser_class: ::SyoboiCalendar::ResponseParsers::TitlesResponseParser,
51
+ list(
52
+ options: options,
53
+ query_class: ::SyoboiCalendar::Queries::ListTitlesQuery,
54
+ response_class: ::SyoboiCalendar::Responses::ListTitlesResponse,
51
55
  )
52
56
  end
53
57
 
@@ -59,5 +63,16 @@ module SyoboiCalendar
59
63
  def get(query)
60
64
  connection.get(ENDPOINT_PATH, query)
61
65
  end
66
+
67
+ # @private
68
+ # @param options [Hash]
69
+ # @param query_class [Class]
70
+ # @param response_class [Class]
71
+ # @return [SyoboiCalendar::Responses::ListBaseResponse]
72
+ def list(options:, query_class:, response_class:)
73
+ query = query_class.new(options)
74
+ faraday_response = get(query.to_hash)
75
+ response_class.new(faraday_response)
76
+ end
62
77
  end
63
78
  end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Queries
3
- class ChannelGroupQuery < BaseQuery
3
+ class ListChannelGroupsQuery < BaseQuery
4
4
  COMMAND = "ChGroupLookup"
5
5
 
6
6
  property :ChGID
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Queries
3
- class ChannelQuery < BaseQuery
3
+ class ListChannelsQuery < BaseQuery
4
4
  COMMAND = "ChLookup"
5
5
 
6
6
  property :ChID
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Queries
3
- class ProgramQuery < BaseQuery
3
+ class ListProgramsQuery < BaseQuery
4
4
  COMMAND = "ProgLookup"
5
5
 
6
6
  JOIN = "SubTitles"
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
2
  module Queries
3
- class TitleQuery < BaseQuery
3
+ class ListTitlesQuery < BaseQuery
4
4
  COMMAND = "TitleLookup"
5
5
 
6
6
  property :Fields
@@ -1,3 +1,5 @@
1
+ require "time"
2
+
1
3
  module SyoboiCalendar
2
4
  module Resources
3
5
  class BaseResource
@@ -1,3 +1,5 @@
1
+ require "time"
2
+
1
3
  module SyoboiCalendar
2
4
  module Resources
3
5
  class ProgramResource < BaseResource
@@ -0,0 +1,93 @@
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
+ # @return [String]
12
+ def body
13
+ faraday_response.body
14
+ end
15
+
16
+ # @return [Integer]
17
+ def code
18
+ body[response_key]["Result"]["Code"].to_i
19
+ end
20
+
21
+ # @note Implementation for Enumerable
22
+ def each(&block)
23
+ resources.each(&block)
24
+ end
25
+
26
+ # @return [Boolean]
27
+ def has_error?
28
+ code != 200
29
+ end
30
+
31
+ # @return [Faraday::Utils::Headers]
32
+ def headers
33
+ faraday_response.headers
34
+ end
35
+
36
+ # @return [String, nil]
37
+ def message
38
+ body[response_key]["Result"]["Message"]
39
+ end
40
+
41
+ # @return [Array<SyoboiCalendar::Resources::BaseResource>]
42
+ def resources
43
+ sources.map do |source|
44
+ resource_class.new(source)
45
+ end
46
+ end
47
+
48
+ # @return [Integer]
49
+ def status
50
+ faraday_response.status
51
+ end
52
+
53
+ private
54
+
55
+ # @return [Faraday::Response]
56
+ def faraday_response
57
+ @faraday_response
58
+ end
59
+
60
+ # @return [Class]
61
+ def resource_class
62
+ raise ::NotImplementedError
63
+ end
64
+
65
+ # @return [String]
66
+ def response_key
67
+ raise ::NotImplementedError
68
+ end
69
+
70
+ # @return [Array<Hash>, Hash]
71
+ def source_or_sources
72
+ raise ::NotImplementedError
73
+ end
74
+
75
+ # @return [Array<Hash>]
76
+ def sources
77
+ if has_error?
78
+ []
79
+ else
80
+ object = source_or_sources
81
+ case
82
+ when object.nil?
83
+ []
84
+ when object.respond_to?(:to_ary)
85
+ object.to_ary || [object]
86
+ else
87
+ [object]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
- module ResponseParsers
3
- class ChannelGroupsResponseParser < BaseResponseParser
2
+ module Responses
3
+ class ListChannelGroupsResponse < BaseResponse
4
4
  private
5
5
 
6
6
  # @note Override
@@ -8,9 +8,14 @@ module SyoboiCalendar
8
8
  ::SyoboiCalendar::Resources::ChannelGroupResource
9
9
  end
10
10
 
11
+ # @note Override
12
+ def response_key
13
+ "ChGroupLookupResponse"
14
+ end
15
+
11
16
  # @note Override
12
17
  def source_or_sources
13
- parsed_body["ChGroupLookupResponse"]["ChGroupItems"]["ChGroupItem"]
18
+ body[response_key]["ChGroupItems"]["ChGroupItem"]
14
19
  end
15
20
  end
16
21
  end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
- module ResponseParsers
3
- class ChannelsResponseParser < BaseResponseParser
2
+ module Responses
3
+ class ListChannelsResponse < BaseResponse
4
4
  private
5
5
 
6
6
  # @note Override
@@ -8,9 +8,14 @@ module SyoboiCalendar
8
8
  ::SyoboiCalendar::Resources::ChannelResource
9
9
  end
10
10
 
11
+ # @note Override
12
+ def response_key
13
+ "ChLookupResponse"
14
+ end
15
+
11
16
  # @note Override
12
17
  def source_or_sources
13
- parsed_body["ChLookupResponse"]["ChItems"]["ChItem"]
18
+ body[response_key]["ChItems"]["ChItem"]
14
19
  end
15
20
  end
16
21
  end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
- module ResponseParsers
3
- class ProgramsResponseParser < BaseResponseParser
2
+ module Responses
3
+ class ListProgramsResponse < BaseResponse
4
4
  private
5
5
 
6
6
  # @note Override
@@ -8,9 +8,14 @@ module SyoboiCalendar
8
8
  ::SyoboiCalendar::Resources::ProgramResource
9
9
  end
10
10
 
11
+ # @note Override
12
+ def response_key
13
+ "ProgLookupResponse"
14
+ end
15
+
11
16
  # @note Override
12
17
  def source_or_sources
13
- parsed_body["ProgLookupResponse"]["ProgItems"]["ProgItem"]
18
+ body[response_key]["ProgItems"]["ProgItem"]
14
19
  end
15
20
  end
16
21
  end
@@ -1,6 +1,6 @@
1
1
  module SyoboiCalendar
2
- module ResponseParsers
3
- class TitlesResponseParser < BaseResponseParser
2
+ module Responses
3
+ class ListTitlesResponse < BaseResponse
4
4
  private
5
5
 
6
6
  # @note Override
@@ -8,9 +8,14 @@ module SyoboiCalendar
8
8
  ::SyoboiCalendar::Resources::TitleResource
9
9
  end
10
10
 
11
+ # @note Override
12
+ def response_key
13
+ "TitleLookupResponse"
14
+ end
15
+
11
16
  # @note Override
12
17
  def source_or_sources
13
- parsed_body["TitleLookupResponse"]["TitleItems"]["TitleItem"]
18
+ body[response_key]["TitleItems"]["TitleItem"]
14
19
  end
15
20
  end
16
21
  end
@@ -1,3 +1,3 @@
1
1
  module SyoboiCalendar
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -1,21 +1,16 @@
1
- require "faraday"
2
- require "faraday_middleware"
3
- require "multi_xml"
4
-
5
1
  require "syoboi_calendar/client"
6
2
  require "syoboi_calendar/queries/base_query"
7
- require "syoboi_calendar/queries/channel_group_query"
8
- require "syoboi_calendar/queries/channel_query"
9
- require "syoboi_calendar/queries/program_query"
10
- require "syoboi_calendar/queries/title_query"
3
+ require "syoboi_calendar/queries/list_channel_groups_query"
4
+ require "syoboi_calendar/queries/list_channels_query"
5
+ require "syoboi_calendar/queries/list_programs_query"
6
+ require "syoboi_calendar/queries/list_titles_query"
11
7
  require "syoboi_calendar/resources/base_resource"
12
8
  require "syoboi_calendar/resources/channel_group_resource"
13
9
  require "syoboi_calendar/resources/channel_resource"
14
10
  require "syoboi_calendar/resources/program_resource"
15
11
  require "syoboi_calendar/resources/title_resource"
16
- require "syoboi_calendar/response"
17
- require "syoboi_calendar/response_parsers/base_response_parser"
18
- require "syoboi_calendar/response_parsers/channel_groups_response_parser"
19
- require "syoboi_calendar/response_parsers/channels_response_parser"
20
- require "syoboi_calendar/response_parsers/programs_response_parser"
21
- require "syoboi_calendar/response_parsers/titles_response_parser"
12
+ require "syoboi_calendar/responses/base_response"
13
+ require "syoboi_calendar/responses/list_channel_groups_response"
14
+ require "syoboi_calendar/responses/list_channels_response"
15
+ require "syoboi_calendar/responses/list_programs_response"
16
+ require "syoboi_calendar/responses/list_titles_response"
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "active_support/core_ext/object/to_query"
2
2
  require "active_support/core_ext/string/strip"
3
- require "rack"
4
3
  require "syoboi_calendar"
5
4
  require "webmock/rspec"
6
5