-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added usage_report graphql type. #260
- Loading branch information
Martin Fenner
committed
May 16, 2019
1 parent
74d2535
commit d5d29ce
Showing
12 changed files
with
376 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class ReportingPeriodType < BaseObject | ||
description "Information about reporting periods" | ||
|
||
field :begin_date, String, null: true, description: "Begin reporting period" | ||
field :end_date, String, null: true, description: "End reporting period" | ||
end |
12 changes: 12 additions & 0 deletions
12
app/graphql/types/usage_report_connection_with_meta_type.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class UsageReportConnectionWithMetaType < BaseConnection | ||
edge_type(UsageReportEdgeType) | ||
field_class GraphQL::Cache::Field | ||
|
||
field :total_count, Integer, null: false, cache: true | ||
|
||
def total_count | ||
UsageReport.query(nil, limit: 0).dig(:meta, "total").to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class UsageReportEdgeType < GraphQL::Types::Relay::BaseEdge | ||
node_type(UsageReportType) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class UsageReportType < BaseObject | ||
description "Information about usage reports" | ||
|
||
field :id, ID, null: false, description: "Usage report ID" | ||
field :reporting_period, ReportingPeriodType, null: false, description: "Time period covered by the report" | ||
field :date_created, String, null: false, description: "Date information was created" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
class UsageReport | ||
# include helper module for PORO models | ||
include Modelable | ||
|
||
def self.find_by_id(id) | ||
url = "https://api.datacite.org/reports/#{id}" | ||
response = Maremma.get(url) | ||
|
||
return {} if response.status != 200 | ||
|
||
message = response.body.dig("data", "report") | ||
data = [parse_message(id: id, message: message)] | ||
|
||
errors = response.body.fetch("errors", nil) | ||
|
||
{ data: data, errors: errors } | ||
end | ||
|
||
def self.query(query, options={}) | ||
number = (options.dig(:page, :number) || 1).to_i | ||
size = (options.dig(:page, :size) || 25).to_i | ||
|
||
url = "https://api.datacite.org/reports?page[size]=#{size}&page[number]=#{number}" | ||
|
||
response = Maremma.get(url) | ||
|
||
return {} if response.status != 200 | ||
|
||
data = response.body.dig("data", "reports").map do |message| | ||
parse_message(id: message['id'], message: message) | ||
end | ||
meta = { "total" => response.body.dig("data", "meta", "total") } | ||
errors = response.body.fetch("errors", nil) | ||
|
||
{ data: data, meta: meta, errors: errors } | ||
end | ||
|
||
def self.parse_message(id: nil, message: nil) | ||
reporting_period = { | ||
begin_date: message.dig("report-header", "reporting-period", "begin-date"), | ||
end_date: message.dig("report-header", "reporting-period", "end-date") | ||
} | ||
|
||
{ | ||
id: id, | ||
reporting_period: reporting_period, | ||
date_modified: message["created"] }.compact | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
spec/fixtures/vcr_cassettes/UsageReport/find_by_id/found.yml
Large diffs are not rendered by default.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
spec/fixtures/vcr_cassettes/UsageReport/find_by_id/not_found.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
|
Oops, something went wrong.