Class: Theguardian::Api::Tag

Inherits:
RecursiveOpenStruct
  • Object
show all
Defined in:
lib/theguardian/api/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process(params = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/theguardian/api/tag.rb', line 4

def self.process(params = {})
	connection = Theguardian::Connection.new(resource: "tags")
	params = self.process_params(params)
	response = connection.get(params)

	response["results"].map do |tag_params|
		self.new(tag_params, recurse_over_arrays: true)
	end
end

.process_params(params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/theguardian/api/tag.rb', line 30

def self.process_params(params)
	# Query string and tag type
	theguardian_api_params = { q: params[:q], type: params[:type] }

		# Pagination
		theguardian_api_params.merge!({ "page" => params[:page] }) if params.has_key?(:page)
		theguardian_api_params.merge!({ "page-size" => params[:per_page] }) if params.has_key?(:per_page)

		theguardian_api_params.reject{ |_, value| value.nil? }
end

Instance Method Details

#fetchObject



14
15
16
17
18
# File 'lib/theguardian/api/tag.rb', line 14

def fetch
	connection = Theguardian::Connection.new(resource: URI(self.apiUrl).path)

	self.class.new(connection.get, recurse_over_arrays: true)
end

#itemsObject



20
21
22
23
24
# File 'lib/theguardian/api/tag.rb', line 20

def items
	self.results.map do |item_params|
		Theguardian::Api::Item.new(item_params, recurse_over_arrays: true)
	end
end

#sectionObject



26
27
28
# File 'lib/theguardian/api/tag.rb', line 26

def section
	Theguardian::Api::Section.process(id: self.tag.sectionId)
end