Class: Decibel::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapper/wrapper.rb

Constant Summary collapse

@@api_url =
"http://rest.decibel.net/v2/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Wrapper

Returns a new instance of Wrapper.



13
14
15
16
17
# File 'lib/wrapper/wrapper.rb', line 13

def initialize args
	args.each do |k, v|
		instance_variable_set("@#{k}", v) unless v.nil?
	end
end

Instance Attribute Details

#decibel_app_idObject

Returns the value of attribute decibel_app_id.



7
8
9
# File 'lib/wrapper/wrapper.rb', line 7

def decibel_app_id
  @decibel_app_id
end

#decibel_app_keyObject

Returns the value of attribute decibel_app_key.



7
8
9
# File 'lib/wrapper/wrapper.rb', line 7

def decibel_app_key
  @decibel_app_key
end

Class Method Details

.create_query_string(start, params) ⇒ Object

Query String



105
106
107
108
109
110
111
# File 'lib/wrapper/wrapper.rb', line 105

def self.create_query_string(start, params)
	if params.is_a?(Hash)
	start += hash_to_query_string(params).join('&')
else
	start
	end
end

.depth_string(hash) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/wrapper/wrapper.rb', line 129

def self.depth_string(hash)
	string = "depth="
new_array = []
hash.each do |depth_k, depth_v|
	new_array << "#{depth_k}" if depth_v == true
end
string += new_array.join(';')
end

.hash_to_query_string(hash) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/wrapper/wrapper.rb', line 113

def self.hash_to_query_string(hash)
	array = []
	hash.each do |k, v|
	array << query_value(k, v)
end
array
end

.image_size(params) ⇒ Object



45
46
47
48
49
50
# File 'lib/wrapper/wrapper.rb', line 45

def self.image_size(params)
	image_size = "standard"
	image_size = "thumbnail" if params.is_a?(Hash) && params[:thumbnail]
	image_size = "full" if params.is_a?(Hash) && params[:full]
	image_size
end

.parse_response(response) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/wrapper/wrapper.rb', line 155

def self.parse_response(response)
if response.is_a?(Net::HTTPOK)
	json = JSON.parse(response.body) rescue {}
	if json["ResultSet"]
		json["ResultSet"]
	elsif json["Result"]
		json["Result"]
	else
		response.body
	end
	end
end

.query_value(k, v) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/wrapper/wrapper.rb', line 121

def self.query_value(k, v)
	if k.to_s.downcase != "depth" && !v.nil?
	"#{k}=#{URI.encode(v, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
else
	depth_string(v)
end
end

Instance Method Details

#album(params) ⇒ Object



19
20
21
22
# File 'lib/wrapper/wrapper.rb', line 19

def album(params)
	response = self.query(Decibel::Wrapper.create_query_string('album/?', params))
Decibel::Album.new(response) if !response.nil?
end

#albums(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/wrapper/wrapper.rb', line 24

def albums(params)
	response = self.query(Decibel::Wrapper.create_query_string('albums/?', params))
if !response.nil?
	array = []
	response.each do |a|
		array << Decibel::Album.new(a)
	end
	array
end
end

#disctags(params) ⇒ Object



35
36
37
# File 'lib/wrapper/wrapper.rb', line 35

def disctags(params)

end

#image(params) ⇒ Object



39
40
41
42
43
# File 'lib/wrapper/wrapper.rb', line 39

def image(params)
	image_id = "0"
	image_id = params[:id] if params.is_a?(Hash) && params[:id]
	response = self.query("image/#{image_id}/#{Decibel::Wrapper.image_size(params)}")
end

#participant(params) ⇒ Object



52
53
54
55
# File 'lib/wrapper/wrapper.rb', line 52

def participant(params)
	response = self.query(Decibel::Wrapper.create_query_string('participant/?', params))
Decibel::Participant.new(response) if !response.nil?
end

#participants(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/wrapper/wrapper.rb', line 57

def participants(params)
	response = self.query(Decibel::Wrapper.create_query_string('participants/?', params))
if !response.nil?
	array = []
	response.each do |a|
		array << Decibel::Participant.new(a)
	end
	array
end
end

#query(ending) ⇒ Object

Hit Decibel API



139
140
141
142
143
# File 'lib/wrapper/wrapper.rb', line 139

def query(ending)
	query = @@api_url + ending
	response = self.send_query(query)
	Decibel::Wrapper.parse_response(response)
end

#recording(params) ⇒ Object



68
69
70
71
# File 'lib/wrapper/wrapper.rb', line 68

def recording(params)
	response = self.query(Decibel::Wrapper.create_query_string('recording/?', params))
Decibel::Recording.new(response) if !response.nil?
end

#recordings(params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/wrapper/wrapper.rb', line 73

def recordings(params)
	response = self.query(Decibel::Wrapper.create_query_string('recordings/?', params))
if !response.nil?
	array = []
	response.each do |a|
		array << Decibel::Recording.new(a)
	end
	array
end
end

#return_numberObject



84
85
86
# File 'lib/wrapper/wrapper.rb', line 84

def return_number
	self.query(Decibel::Wrapper.create_query_string('return/number', nil))
end

#send_query(query) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/wrapper/wrapper.rb', line 145

def send_query(query)
	uri = URI.parse(query)
	http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header('DecibelAppID' => self.decibel_app_id, 
	'DecibelAppKey' => self.decibel_app_key, 
	'DecibelTimestamp' => Time.now.strftime('%Y%m%d %H:%M:%S'))
  http.request(request)
end

#work(params) ⇒ Object



88
89
90
91
# File 'lib/wrapper/wrapper.rb', line 88

def work(params)
	response = self.query(Decibel::Wrapper.create_query_string('work/?', params))
Decibel::Work.new(response) if !response.nil?
end

#works(params) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/wrapper/wrapper.rb', line 93

def works(params)
	response = self.query(Decibel::Wrapper.create_query_string('works/?', params))
if !response.nil?
	array = []
	response.each do |a|
		array << Decibel::Work.new(a)
	end
	array
end
end