Class: Rflak::Flaker

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

Overview

Utility class for fetching ‘flak’s from the web.

Example:

Flaker.fetch('user') do |flak|
  flak. 'seban'
  flak.limit 2
  flak.tag 'ruby'
end

Constant Summary collapse

FLAK_API_URL =
'http://api.flaker.pl/api'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Flaker

Returns a new instance of Flaker.



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

def initialize(type)
  @perform_url = FLAK_API_URL + "/type:#{ type }"
end

Instance Attribute Details

#perform_urlObject (readonly)

Returns the value of attribute perform_url.



17
18
19
# File 'lib/flaker.rb', line 17

def perform_url
  @perform_url
end

Class Method Details

.auth_connection(user) ⇒ Object

Authorize connetion by user’s credentials (login and api key) and perform instruction passed in block. Raises NotAuthorize exception when user has bad login or api key.

user

User

Raises:



35
36
37
38
39
40
41
42
# File 'lib/flaker.rb', line 35

def self.auth_connection(user)
  user.auth unless user.authorized?
  raise NotAuthorized.new('Not authorized') unless user.authorized?
  Flaker.basic_auth(user., user.api_key)
  response = yield(Flaker)
  Flaker.basic_auth('','')
  return response
end

.fetch(type) {|flak| ... } ⇒ Object

Yields:

  • (flak)


24
25
26
27
28
# File 'lib/flaker.rb', line 24

def self.fetch(type)
  flak = Flaker.new(type)
  yield(flak) if block_given?
  parse_response get(flak.perform_url)
end

.parse_response(response) ⇒ Object



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

def self.parse_response(response)
  response['entries'].map do |entry|
    Entry.new(entry)
  end
end

Instance Method Details

#avatars(value) ⇒ Object



87
88
89
# File 'lib/flaker.rb', line 87

def avatars(value)
  @perform_url += "/avatars:#{ value }"
end

#comments(value) ⇒ Object



117
118
119
# File 'lib/flaker.rb', line 117

def comments(value)
  @perform_url += "/comments:#{ value }"
end

#entry_id(value) ⇒ Object



62
63
64
# File 'lib/flaker.rb', line 62

def entry_id(value)
  @perform_url += "/entry_id:#{ value }"
end

#from(value) ⇒ Object



97
98
99
# File 'lib/flaker.rb', line 97

def from(value)
  @perform_url += "/from:#{ value }"
end

#limit(value) ⇒ Object



92
93
94
# File 'lib/flaker.rb', line 92

def limit(value)
  @perform_url += "/limit:#{ value }"
end

#login(value) ⇒ Object



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

def (value)
  @perform_url += "/login:#{ value }"
end

#mode(value) ⇒ Object



82
83
84
# File 'lib/flaker.rb', line 82

def mode(value)
  @perform_url += "/mode:#{ value }"
end

#since(value) ⇒ Object



107
108
109
# File 'lib/flaker.rb', line 107

def since(value)
  @perform_url += "/since:#{ value }"
end

#sort(value) ⇒ Object



112
113
114
# File 'lib/flaker.rb', line 112

def sort(value)
  @perform_url += "/sort:#{ value }"
end

#source(value) ⇒ Object



72
73
74
# File 'lib/flaker.rb', line 72

def source(value)
  @perform_url += "/source:#{ value }"
end

#start(value) ⇒ Object



102
103
104
# File 'lib/flaker.rb', line 102

def start(value)
  @perform_url += "/start:#{ value }"
end

#story(story_id) ⇒ Object



67
68
69
# File 'lib/flaker.rb', line 67

def story(story_id)
  @perform_url += "/story:#{ story_id }"
end

#tag(value) ⇒ Object



77
78
79
# File 'lib/flaker.rb', line 77

def tag(value)
  @perform_url += "/tag:#{ value }"
end

#url(value) ⇒ Object



57
58
59
# File 'lib/flaker.rb', line 57

def url(value)
  @perform_url += "/url:#{ value }"
end