Class: Sakenote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sakenote/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
# File 'lib/sakenote/client.rb', line 12

def initialize(token)
  if token.is_a?(String) && token.strip.length > 0
    @token = token
    @conn  = connection
  else
    raise Sakenote::Error::ConfigurationError
  end
end

Instance Method Details

#connectionObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/sakenote/client.rb', line 73

def connection
  faraday = Faraday.new 'https://www.sakenote.com' do |conn|
    conn.request  :json
    conn.response :json, :content_type => /\bjson$/
    conn.adapter  Faraday.default_adapter
  end

  faraday.headers[:user_agent] = "Sakenote/#{Sakenote::VERSION} (sakenote-rubygems/#{Sakenote::VERSION})"
  faraday
end

#makers(prefecture_code: nil, maker_name: nil, page: 1) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sakenote/client.rb', line 51

def makers(prefecture_code: nil, maker_name: nil, page: 1)
  options = {
    token:           @token,
    prefecture_code: prefecture_code,
    maker_name:      maker_name,
    page:            page
  }.select{ |k, v| !v.nil? }

  response = @conn.get '/api/v1/makers', options

  num_pages = response.body['num_pages'].to_i
  list      = response.body['makers'].map { |s|
    Maker.new(s['maker_name'],
      postcode: s['maker_postcode'],
      address:  s['maker_address'],
      url:      s['maker_url']
    )
  }

  SearchResult.new(list, num_pages)
end

#sakes(prefecture_code: nil, sake_name: nil, maker_name: nil, page: 1) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sakenote/client.rb', line 22

def sakes(prefecture_code: nil, sake_name: nil, maker_name: nil, page: 1)
  options = {
    token:           @token,
    prefecture_code: prefecture_code,
    sake_name:       sake_name,
    maker_name:      maker_name,
    page:            page
  }.select{ |k, v| !v.nil? }

  response = @conn.get '/api/v1/sakes', options

  num_pages = response.body['num_pages'].to_i
  list      = response.body['sakes'].map { |s|
    Sake.new(s['sake_name'],
      identify_code: s['sake_identify_code'],
      furigana:      s['sake_furigana'],
      alphabet:      s['sake_alphabet'],
      maker:         Maker.new(s['maker_name'],
        postcode: s['maker_postcode'],
        address:  s['maker_address'],
        url:      s['maker_url']
      )
    )
  }

  SearchResult.new(list, num_pages)
end