Class: Chuck::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chuck.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Instance methods ==========================================================



18
19
20
# File 'lib/chuck.rb', line 18

def initialize(options = {})
  options.each{|k, v| self.instance_variable_set(:"@#{k}", v) if self.respond_to?(k)}
end

Instance Attribute Details

#first_nameObject (readonly)

Returns the value of attribute first_name.



14
15
16
# File 'lib/chuck.rb', line 14

def first_name
  @first_name
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/chuck.rb', line 14

def id
  @id
end

#jokesObject (readonly)

Returns the value of attribute jokes.



14
15
16
# File 'lib/chuck.rb', line 14

def jokes
  @jokes
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



14
15
16
# File 'lib/chuck.rb', line 14

def last_name
  @last_name
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



14
15
16
# File 'lib/chuck.rb', line 14

def raw_response
  @raw_response
end

Class Method Details

.categoriesObject

Class methods =============================================================



28
29
30
# File 'lib/chuck.rb', line 28

def self.categories
  get("/categories").parsed_response
end

.retrieve(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chuck.rb', line 32

def self.retrieve(options = {})
  url = "/jokes/random/#{options[:quantity] or 1}"

  query = Hash.new
  query[:firstName] = options[:first_name] if options[:first_name]
  query[:lastName]  = options[:last_name] if options[:last_name]
  query[:limitTo]   = "[#{options[:only].join(',')}]" if options[:only]
  query[:exclude]   = "[#{options[:exclude].join(',')}]" if options[:except]

  result = JSON.parse(get(url, query).parsed_response)

  Chuck::Request.new jokes:        Chuck::Joke.jokes_from_result(result['value']),
                     first_name:   query[:firstName],
                     last_name:    query[:lastName],
                     raw_response: result

end

Instance Method Details

#successObject



22
23
24
# File 'lib/chuck.rb', line 22

def success
  @raw_response['type'] == 'success'
end