Class: DoorkeeperJp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/doorkeeper_jp/client.rb,
sig/doorkeeper_jp/client.rbs

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • access_token (String) (defaults to: nil)


6
7
8
# File 'lib/doorkeeper_jp/client.rb', line 6

def initialize(access_token = nil)
  @access_token = access_token
end

Instance Method Details

#connectionFaraday::Connection

Returns:

  • (Faraday::Connection)


116
117
118
119
120
121
122
123
124
# File 'lib/doorkeeper_jp/client.rb', line 116

def connection
  Faraday.new(url: "https://api.doorkeeper.jp/", headers: request_headers) do |conn|
    conn.response :mashify, mash_class: DoorkeeperJp::Response
    conn.response :json
    conn.response :raise_error

    conn.adapter Faraday.default_adapter
  end
end

#event(id, is_expand_group: false, locale: nil) ⇒ DoorkeeperJp::Response

Show a specific event

Parameters:

  • id (Integer)
  • is_expand_group (Boolean) (defaults to: false)

    Expands the group object.

  • locale (String) (defaults to: nil)

    The localized text for an event. One of en, ja. Default: ja.

  • is_expand_group: (Boolean) (defaults to: false)
  • locale: (String, nil) (defaults to: nil)

Returns:

See Also:



81
82
83
84
85
86
87
88
89
90
# File 'lib/doorkeeper_jp/client.rb', line 81

def event(id, is_expand_group: false, locale: nil)
  params = {
    locale: locale,
  }.compact #: Hash[Symbol|String, untyped]

  params["expand[]"] = "group" if is_expand_group

  res = connection.get("events/#{id}", params).body
  res["event"]
end

#event_params(page:, locale:, sort:, since_date:, until_date:, keyword:, prefecture:, is_expand_group:) ⇒ Hash

Parameters:

  • page (Integer)

    The page offset of the results.

  • locale (String)

    The localized text for an event. One of en, ja. Default: ja.

  • sort (String)

    The order of the results. One of published_at, starts_at, updated_at. Default: published_at.

  • since_date (Date)

    Only events taking place during or after this date will be included. Default: Today.

  • until_date (Date)

    Only events taking place during or before this date will be included.

  • keyword (String)

    Keyword to search for from the title or description fields.

  • prefecture (String)

    Only events with an address in prefecture.

  • is_expand_group (Boolean)

    Expands the group object.

  • page: (Integer, nil)
  • locale: (String, nil)
  • sort: (String, nil)
  • since_date: (Date, nil)
  • until_date: (Date, nil)
  • keyword: (String, nil)
  • prefecture: (String, nil)
  • is_expand_group: (Boolean)

Returns:

  • (Hash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/doorkeeper_jp/client.rb', line 149

def event_params(page:, locale:, sort:, since_date:, until_date:, keyword:, prefecture:, is_expand_group:)
  params = {
    page:       page,
    locale:     locale,
    sort:       sort,
    since:      to_ymd(since_date),
    until:      to_ymd(until_date),
    q:          keyword,
    prefecture: prefecture
  }.compact #: Hash[Symbol|String, untyped]

  params["expand[]"] = "group" if is_expand_group

  params
end

#events(page: nil, locale: nil, sort: nil, since_date: nil, until_date: nil, keyword: nil, prefecture: nil, is_expand_group: false) ⇒ Array<DoorkeeperJp::Response>

List all featured events

Parameters:

  • page (Integer) (defaults to: nil)

    The page offset of the results.

  • locale (String) (defaults to: nil)

    The localized text for an event. One of en, ja. Default: ja.

  • sort (String) (defaults to: nil)

    The order of the results. One of published_at, starts_at, updated_at. Default: published_at.

  • since_date (Date) (defaults to: nil)

    Only events taking place during or after this date will be included. Default: Today.

  • until_date (Date) (defaults to: nil)

    Only events taking place during or before this date will be included.

  • keyword (String) (defaults to: nil)

    Keyword to search for from the title or description fields.

  • prefecture (String) (defaults to: nil)

    Only events with an address in prefecture.

  • is_expand_group (Boolean) (defaults to: false)

    Expands the group object.

  • page: (Integer, nil) (defaults to: nil)
  • locale: (String, nil) (defaults to: nil)
  • sort: (String, nil) (defaults to: nil)
  • since_date: (Date, nil) (defaults to: nil)
  • until_date: (Date, nil) (defaults to: nil)
  • keyword: (String, nil) (defaults to: nil)
  • prefecture: (String, nil) (defaults to: nil)
  • is_expand_group: (Boolean) (defaults to: false)

Returns:

See Also:



24
25
26
27
28
29
30
# File 'lib/doorkeeper_jp/client.rb', line 24

def events(page: nil, locale: nil, sort: nil, since_date: nil, until_date: nil, keyword: nil, prefecture: nil, is_expand_group: false)
  get_events(
    path: "events",
    page: page, locale: locale, sort: sort, since_date: since_date, until_date: until_date,
    keyword: keyword, prefecture: prefecture, is_expand_group: is_expand_group,
  )
end

#get_events(path:, page:, locale:, sort:, since_date:, until_date:, keyword:, prefecture:, is_expand_group:) ⇒ Array<DoorkeeperJp::Response>

Parameters:

  • path (String)
  • page (Integer)

    The page offset of the results.

  • locale (String)

    The localized text for an event. One of en, ja. Default: ja.

  • sort (String)

    The order of the results. One of published_at, starts_at, updated_at. Default: published_at.

  • since_date (Date)

    Only events taking place during or after this date will be included. Default: Today.

  • until_date (Date)

    Only events taking place during or before this date will be included.

  • keyword (String)

    Keyword to search for from the title or description fields.

  • prefecture (String)

    Only events with an address in prefecture.

  • is_expand_group (Boolean)

    Expands the group object.

  • path: (String)
  • page: (Integer, nil)
  • locale: (String, nil)
  • sort: (String, nil)
  • since_date: (Date, nil)
  • until_date: (Date, nil)
  • keyword: (String, nil)
  • prefecture: (String, nil)
  • is_expand_group: (Boolean)

Returns:



105
106
107
108
109
110
111
112
113
# File 'lib/doorkeeper_jp/client.rb', line 105

def get_events(path:, page:, locale:, sort:, since_date:, until_date:, keyword:, prefecture:, is_expand_group:)
  params = event_params(
    page: page, locale: locale, sort: sort, since_date: since_date, until_date: until_date,
    keyword: keyword, prefecture: prefecture, is_expand_group: is_expand_group,
  )

  res = connection.get(path, params).body
  res.map(&:event) # steep:ignore
end

#group(group, locale: nil) ⇒ DoorkeeperJp::Response

Show a specific group

Parameters:

  • group (String)
  • locale (String) (defaults to: nil)

    The localized text for an event. One of en, ja. Default: ja.

  • locale: (String, nil) (defaults to: nil)

Returns:

See Also:



63
64
65
66
67
68
69
70
# File 'lib/doorkeeper_jp/client.rb', line 63

def group(group, locale: nil)
  params = {
    locale: locale,
  }.compact

  res = connection.get("groups/#{group}", params).body
  res["group"]
end

#group_events(group, page: nil, locale: nil, sort: nil, since_date: nil, until_date: nil, keyword: nil, prefecture: nil, is_expand_group: false) ⇒ Array<DoorkeeperJp::Response>

List a community's events

Parameters:

  • group (String)
  • page (Integer) (defaults to: nil)

    The page offset of the results.

  • locale (String) (defaults to: nil)

    The localized text for an event. One of en, ja. Default: ja.

  • sort (String) (defaults to: nil)

    The order of the results. One of published_at, starts_at, updated_at. Default: published_at.

  • since_date (Date) (defaults to: nil)

    Only events taking place during or after this date will be included. Default: Today.

  • until_date (Date) (defaults to: nil)

    Only events taking place during or before this date will be included.

  • keyword (String) (defaults to: nil)

    Keyword to search for from the title or description fields.

  • prefecture (String) (defaults to: nil)

    Only events with an address in prefecture.

  • is_expand_group (Boolean) (defaults to: false)

    Expands the group object.

  • page: (Integer, nil) (defaults to: nil)
  • locale: (String, nil) (defaults to: nil)
  • sort: (String, nil) (defaults to: nil)
  • since_date: (Date, nil) (defaults to: nil)
  • until_date: (Date, nil) (defaults to: nil)
  • keyword: (String, nil) (defaults to: nil)
  • prefecture: (String, nil) (defaults to: nil)
  • is_expand_group: (Boolean) (defaults to: false)

Returns:

See Also:



47
48
49
50
51
52
53
# File 'lib/doorkeeper_jp/client.rb', line 47

def group_events(group, page: nil, locale: nil, sort: nil, since_date: nil, until_date: nil, keyword: nil, prefecture: nil, is_expand_group: false)
  get_events(
    path: "groups/#{group}/events",
    page: page, locale: locale, sort: sort, since_date: since_date, until_date: until_date,
    keyword: keyword, prefecture: prefecture, is_expand_group: is_expand_group,
  )
end

#request_headersHash

Returns:

  • (Hash)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/doorkeeper_jp/client.rb', line 127

def request_headers
  headers =
    {
      "User-Agent" => "DoorkeeperJp v#{DoorkeeperJp::VERSION} (https://github.com/sue445/doorkeeper_jp)",
      "Content-Type" => "application/json",
    }

  headers["Authorization"] = "Bearer #{@access_token}" if @access_token

  headers
end

#to_ymd(date) ⇒ String

Parameters:

  • date (Date)

Returns:

  • (String)


168
169
170
# File 'lib/doorkeeper_jp/client.rb', line 168

def to_ymd(date)
  date&.strftime("%Y-%m-%d")
end