Class: Interpark::Book::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/interpark/book/client.rb

Constant Summary collapse

APIS =
{
  :search => {:method => "GET", :path => "/api/search.api"}
}

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



49
50
51
# File 'lib/interpark/book/client.rb', line 49

def initialize(attrs = {})
  self.configuration.attributes = attrs
end

Instance Method Details

#configurationObject



57
58
59
# File 'lib/interpark/book/client.rb', line 57

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Yields:



53
54
55
# File 'lib/interpark/book/client.rb', line 53

def configure
  yield configuration if block_given?
end

#get(http, path, data) ⇒ Object



73
74
75
76
77
78
# File 'lib/interpark/book/client.rb', line 73

def get(http, path, data)
  data = URI.encode_www_form(data)
  resp, body = http.send_request("GET", "#{path}?#{data}")

  JSON.parse(resp.body)
end

#post(http, path, data) ⇒ Object



80
81
82
83
84
85
# File 'lib/interpark/book/client.rb', line 80

def post(http, path, data)
  data = URI.encode_www_form(data)
  resp, body = http.send_request("POST", path, data)

  JSON.parse(resp.body)
end

#request(method, path, data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/interpark/book/client.rb', line 61

def request(method, path, data)
  http = configuration.http
  data = configuration.data.merge(data)

  case method
  when "GET"
    get(http, path, data)
  when "POST"
    post(http, path, data)
  end
end

#search(content, options) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/interpark/book/client.rb', line 87

def search(content, options)
  method = APIS[:search][:method]
  path = APIS[:search][:path]

  data = {"query" => content}
  data = data.merge(options)

  request(method, path, data)
end