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
91 92 93 |
# File 'lib/imgur/client.rb', line 91 def self.data @data ||= {} end |
.reset! ⇒ Object
95 96 97 |
# File 'lib/imgur/client.rb', line 95 def self.reset! @data = nil end |
Instance Method Details
#data ⇒ Object
99 100 101 |
# File 'lib/imgur/client.rb', line 99 def data self.class.data end |
#initalize(options = {}) ⇒ Object
103 104 105 |
# File 'lib/imgur/client.rb', line 103 def initalize(={}) @url = [:url] || "https://api.imgur.com" end |
#page(params, collection, object_rool, options = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/imgur/client.rb', line 107 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
139 140 141 142 143 144 145 146 147 |
# File 'lib/imgur/client.rb', line 139 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
153 154 155 |
# File 'lib/imgur/client.rb', line 153 def stringify_keys(hash) hash.inject({}) { |r,(k,v)| r.merge(k.to_s => v) } end |
#url_for(path) ⇒ Object
149 150 151 |
# File 'lib/imgur/client.rb', line 149 def url_for(path) File.join(@url, path) end |
#url_for_page(collection, page_index, page_size, rel) ⇒ Object
135 136 137 |
# File 'lib/imgur/client.rb', line 135 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 |