Class: Imgur::Client::Mock
- Inherits:
-
Object
- Object
- Imgur::Client::Mock
- Defined in:
- lib/imgur/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #data ⇒ Object
- #initalize(options = {}) ⇒ Object
- #page(params, collection, object_rool, options = {}) ⇒ Object
- #response(options = {}) ⇒ Object
- #stringify_keys(hash) ⇒ Object
- #url_for(path) ⇒ Object
- #url_for_page(collection, page_index, page_size, rel) ⇒ Object
Class Method Details
.data ⇒ Object
113 114 115 |
# File 'lib/imgur/client.rb', line 113 def self.data @data ||= {} end |
.reset! ⇒ Object
117 118 119 |
# File 'lib/imgur/client.rb', line 117 def self.reset! @data = nil end |
Instance Method Details
#data ⇒ Object
121 122 123 |
# File 'lib/imgur/client.rb', line 121 def data self.class.data end |
#initalize(options = {}) ⇒ Object
125 126 127 |
# File 'lib/imgur/client.rb', line 125 def initalize(={}) @url = [:url] || "https://api.imgur.com" end |
#page(params, collection, object_rool, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/imgur/client.rb', line 129 def page(params, collection, object_rool, ={}) if params["url"] uri = URI.parse(params["url"]) params = uri.query.split("&").inject({}) { |r,e| k,v = e.qplit("="); r.merge(k => v) } end resources = [:resources] || self.data[collection] page_size = (params["per_page"] || 20).to_i page_index = (params["page"] || 1).to_i total_pages = (resources.size.to_f / page_size.to_f).round offset = (page_index - 1) * page_size links = [] resource_page = resources.values.reverse.slice(offset, page_size) if page_index < total_pages links << url_for_page(collection, page_index - 1, page_size, 'next') end if page_index - 1 > 0 links << url_for_page(collection, page_index - 1, page_size, 'prev') end links << url_for_page(collection, total_pages, page_size, 'last') [links.join(", "), resource_page] end |
#response(options = {}) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/imgur/client.rb', line 161 def response(={}) status = [:status] || 200 body = [:body] headers = { "Content-Type" => "application/json; charset=utf-8" }.merge([:headers] || {}) Imgur::Response.new(status, headers, body).raise! end |
#stringify_keys(hash) ⇒ Object
175 176 177 |
# File 'lib/imgur/client.rb', line 175 def stringify_keys(hash) hash.inject({}) { |r,(k,v)| r.merge(k.to_s => v) } end |
#url_for(path) ⇒ Object
171 172 173 |
# File 'lib/imgur/client.rb', line 171 def url_for(path) File.join(@url, path) end |
#url_for_page(collection, page_index, page_size, rel) ⇒ Object
157 158 159 |
# File 'lib/imgur/client.rb', line 157 def url_for_page(collection, page_index, page_size, rel) "<#{File.join(@url, collection.to_s)}?page=#{page_index}&per_page=#{page_size}>; rel=\"#{rel}\"" end |